Search code examples
androidandroid-studioandroid-productflavorsamazon-appstore

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.


Solution

  • 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'
    }