question

lsli88 avatar image
lsli88 asked

Using single codebase in Android Studio

I currently have an Android app in development and am starting to look into putting the app on the Amazon app store. I want to keep a single codebase, i.e. a single Android Studio project to manage my code/configuration for Android and FireOS, rather than have to manage multiple Studio projects. Is this possible? In particular, can this be accomplished by adding a separate Amazon FireOS product flavor into my Gradle build file (build.gradle)? I'm assuming no because I need to change the values of the compileSdkVersion and buildToolsVersion in the android section: https://developer.amazon.com/public/solutions/devices/fire-phone/docs/setup#Using%20the%20Android%20Gradle%20Plugin Obviously, this probably means I would have to create a second build.gradle file? I'm still very new to Android Studio and especially Gradle. How would you suggest I set up my Android Studio project to allow for a single codebase for Android and FireOS. I would prefer if I would make as few project changes as possible, decrease the amount of duplicate code/configuration, etc.
fire tablet
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Nick Gardner avatar image
Nick Gardner answered
It depends on what changes you are making for FireOS. If you are not using any Amazon APIs, there really doesn't need to be any changes in your code itself, so you are unlikely to need separate build.gradle configurations. If you are using Amazon APIs, you should be able to use product flavors for each different build. -Nick
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

lsli88 avatar image
lsli88 answered
I will be using Amazon Maps v2 API for my Google Maps v2 API replacement, as well as a barcode scanner library. Moreover, I have some custom code I will need to write to replace usage of the Google Maps Location API to get my user's location. I don't see how I would be able to use product flavors for each of my builds - i.e. one for regular Android and the other for FireOS... I would need separate build.gradle files, correct? And with that, that would require separate modules, correct - and thus duplication of code? I don't see how I could share the same app build.gradle file... Could you elaborate on how I could share the same build.gradle file, e.g. the compileSdkVersion, different dependencies, etc.?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

lsli88 avatar image
lsli88 answered
I still need help on this issue... Can someone answer the questions in my previous message?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Nick Gardner avatar image
Nick Gardner answered
Hi, There's a few options here. The easiest is probably to just include both and decide at runtime which to use by checking whether you are on an Amazon device or not. The build.gradle flavors is the other option. You should be able to include both flavors in a single file, and you can then change certain things for each build like particular source directories for the maps option. For more information, see the gradle docs: https://developer.android.com/tools/building/configuring-gradle.html#workBuildVariants. -Nick
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

lsli88 avatar image
lsli88 answered
Hi Nick, I still don't see how I would make some of the changes you suggested in your previous email. Perhaps an example will help. And I think I was confused when I looked at the documentation for the Fire Phone. I was hoping to port my application to the Fire Phone as well. I know the Fire Phone is essentially discontinued, but if it takes little effort to port it, then I'll do it. Here's a snippet of my current build.gradle file: android { compileSdkVersion 23 buildToolsVersion "23.0.0" So I'm compiling against Android Marshmallow (I want to make sure that it will take advantage of the improvements in the permissions). Will that be an issue in the first place, i.e. using a compile SDK version for Marshmallow? Second, I'm still confused how I would handle the dependencies section... How do I handle dependencies with build variants? I've actually reviewed the link you provided before, but it gives no ideas how one can have a different dependency per build variant/build type/product flavor... Third, I guess this involves the Fire Phone. The Fire Phone docs suggest the following for the build.gradle file: android { compileSdkVersion " Amazon.com:Amazon Fire Phone SDK Addon:17" buildToolsVersion "21.1.2" defaultConfig { minSdkVersion 17 targetSdkVersion 17 } } So how would this work if I wanted to keep a single build.gradle file?
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Nick Gardner avatar image
Nick Gardner answered
Fore even more detailed information there's a whole section of the gradle docs: https://docs.gradle.org/current/userguide/dependency_management.html, specifically section 52.5. You should be able to create two configurations with totally different dependencies and just use flavors to compile against one of the configurations or the other. As for the Marshmallow, it likely won't cause problems but I can't guarantee, you'd have to test to be sure. New Android platform releases have thousands of changes and it is impossible to say which ones may affect your app. -Nick
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

lsli88 avatar image
lsli88 answered
Hi Nick, thanks for the answers. That answers 2 out of the 3 questions I had. What about the compileSdkVersion issue? I found this link that makes me think this is currently not possible on Android Studio: https://code.google.com/p/android/issues/detail?id=64985 Will this be helped (esp. for the Fire Phone) by the new Android manifest merging as defined here?: https://developer.amazon.com/appsandservices/support/submitting-your-app/tech-docs/targeting-amazon-devices-with-your-android-manifest
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Nick Gardner avatar image
Nick Gardner answered
Hi, From that, it looks like you might need to do that part manually. -Nick
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.