Introduction
To build an app on Android we may want to have different outputs based on specific Modifiable values, for example when implementing In-App Purchase for different markets (Google Play, Myket, etc.), with the Gradle tool for Android, this operation will be easy and there is no need to keep multiple sources for each application.
In this document, we’ll explain the In-App Purchase for Google Play and Myket to get familiar with how this type of operation works.
gradle.build
First we need to change the geadle.build file:
- Add a variable
def marketApplicationId
- Add different flavors to the android element
android { productFlavors { googlePlay { } myket { } } }
In each flavor, we need to specify the value:
- Variable for use in-app codes
- Variable for use in the application manifest h file
- Specifying the value of the defined variable
googlePlay { marketApplicationId = "com.android.vending" buildConfigField "String", "MARKET_APPLICATION_ID", "\"${marketApplicationId}\"" storePermission = "com.android.vending.BILLING" } myket { marketApplicationId = "ir.mservices.market" buildConfigField "String", "MARKET_APPLICATION_ID", "\"${marketApplicationId}\"" storePermission = "ir.mservices.market.BILLING" }
Note that the value of the storePermission variable must be found in the Intended App Store In-App-Purchase Documentation. For example, this value is equal to “ir.mservices.market.BILLING” for Myket and “com.android.vending.BILLING” for Google play.
Application manifest
<uses-permission android:name="${storePermission}" />
IabHelper.java
Intent serviceIntent = new Intent(BuildConfig.MARKET_APPLICATION_ID + ".InAppBillingService.BIND"); serviceIntent.setPackage(BuildConfig.MARKET_APPLICATION_ID);
Publish the app with a flavor
To select which of the ingredients to run, in the BuildVariants section of Android Studio, you can select different flavors and automatically change all the variables to the desired flavor.