Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate slightly different APKs from the same code

I developed a small game that uses Parse and push notifications (GCM, Google Cloud Messaging). However when I try to upload my app to the Amazon Store, they say that it will be not published in certain devices because it’s using GCM.

So, the solution is easy, just remove notifications. The problem I have is that I will like to automate the apk generation as much as I can. Under iOS (I'm iOS developer, new to Android), I just create 2 targets and using a DEFINE I can modify code according to the target. I would like to have something similar, so I use another manifest in the amazon version, and of course I remove the push code.

How can I do that when using Android Studio? I suspect I have to use gradle or maybe the concept of module, because when creating a signed apk in Android Studio I have to select “app” module.

Do you have any idea, suggestion or link? Thanks.

like image 521
Ricardo Avatar asked Aug 29 '26 02:08

Ricardo


2 Answers

You can accomplish this using product flavors. Each product flavor can have its own AndroidManifest, and each flavor's manifest will get merged with the main applications manifest at compile time. See the build variants section of the documentation.

As an example, you can define the following product flavors

productFlavors {
    amazon
    googleplay
}

Then you can set up the following manifest files

src/main/AndroidManifest.xml
src/amazon/AndroidManifest.xml
src/googleplay/AndroidManifest.xml <-- put GCM permissions here

You can also put all of your Google Play Services code and dependencies into your googleplay flavor, for example:

dependencies {
    googleplayCompile 'com.google.android.gms:play-services-location:7.0.0'
}
like image 172
Alex Baker Avatar answered Aug 30 '26 19:08

Alex Baker


How can I do that when using Android Studio?

This is a job for product flavors. Create a google flavor and an amazon flavor. Then:

  • Use googleCompile, instead of compile, for your Play Services SDK dependencies.

  • Have the Play Services metadata only in your google flavor's edition of the manifest.

  • Have the code that depends upon the Play Services SDK be in the google flavor's sourceset. Note that you may need to abstract some hooks to that code out to a NotificationStrategy class that you implement in each flavor, where the google one does the Play Services work and the amazon flavor does something else (or nothing). Your code in main can then reference NotificationStrategy and get the right implementation for each flavor.

like image 28
CommonsWare Avatar answered Aug 30 '26 21:08

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!