How to avoid duplicate code when using similar SDKs

Spiderman identifying a *cough* duplicate bug

Duplicating code for nearly identical SDKs

  • AmazonMap instead of GoogleMap
  • AmazonMapOptions instead of GoogleMapOptions
  • import package names of com.amazon.geo.mapsv2 instead of com.google.android.gms.maps for classes like LatLng, etc.

The solution — Gradle to the rescue

  1. Copy the file to the Amazon directory (src/amazon/java/org/onebusaway/android/map/googlemapsv2)
  2. Replace all Google import and class namespaces (com.google.android.gms.maps) in the Amazon file with the Amazon version (com.amazon.geo.mapsv2)
  3. Replace all references to GoogleMap with AmazonMap
  4. Add a comment block header to each class (from a templateAmazonMapsFileHeader.txt) that says DO NOT EDIT, etc., with an explanation of how those classes are generated (to avoid someone else unfamiliar with the build process from making changes that would get erased)
  5. Exclude a file named ProprietaryMapHelpV2.java from this process. This is a utility class that contains small implementation differences between each Maps API, such as a method isMapsInstalled() that uses a different process to verify if the Maps API library is available on the device. We want these classes to be different for each flavor, so we don’t copy it.

Copying libraries — Android Maps SDK v3 beta

  • import package names changed from com.google.android.gms.maps to com.google.android.libraries.maps
  • Different dependencies in build.gradle
  • the -sources.jar file was empty for the v2 and v3 beta product flavors. Apparently the sourcesJar Gradle task is hardcoded to the /main source set and cannot changed per flavor.
  • the -javadoc.jar file only created docs for one flavor, seemingly also related to limitations of the Gradle task
  • automated publishing code for the generated artifacts became very complex

…and back to apps

Tips and tricks

Source of truth

SDK APIs don’t have to be exactly 1:1

Conclusions

  • between build variants in apps to share resources across the variants
  • between modules in libraries to simplify the release process

Acknowledgements

  • Thanks to Chris Arriola for implementing the Gradle copy task for Android Maps Utils, KTX libraries, and Google Maps Android Samples projects!

--

--

--

Improving the world, one byte at a time. @sjbarbeau, https://github.com/barbeau, https://www.linkedin.com/in/seanbarbeau/. I work @CUTRUSF. Posts are my own.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Struct And Interface Embedding

Automation with Ansible

Best Hosting for your Websites in 2020: Hostgator

Interview with Brent Vatne, App.js conference speaker

Android — Role of “UI Automator” in UI Testing of Native apps involving WebViews using ‘Espresso’

Circular View Layout

Elixir — Firestore Database Integration

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sean Barbeau

Sean Barbeau

Improving the world, one byte at a time. @sjbarbeau, https://github.com/barbeau, https://www.linkedin.com/in/seanbarbeau/. I work @CUTRUSF. Posts are my own.

More from Medium

A Curious Tale of Late Initialisation Options — Part III

Using Secrets in Circle Ci and Local

Debugging Android Unit Tests from the Gradle Command Line

Watch files on folder with Kotlin