preface

Recently, I have been learning about Android Gradle. Today, I happened to see ProductFlavor. ProductFlavor represents ProductFlavor. You can see Android developers ProductFlavor in the relevant documents of Google. The word “ProductFlavor” is quite interesting. At first glance, I don’t understand what it is. Wouldn’t the buildTypes in my Gradle file be enough to differentiate packaging, so I took a moment to review it, According to my understanding, ProductFlavor is basically useless if you are a small and medium-sized project that does not involve differentiating different packages for users in different regions. However, if you want to differentiate the domestic and foreign versions in your project, and even add the paid and free versions according to whether the users are VIP members or not, In this case, there will be domestic charge, free foreign charge, free version, in the extreme point, I charge and free version on the same page or even display the UI layout and icon icon resources are different, this situation and how to deal with it, ProductFlavor is a very friendly way for us developers to solve this problem.

productFlavors

ProductFlavors is an easy-to-use option that can be packaged with multiple options. You can try productFlavors directly on the Android closure. In my case, I tried productFlavors as a paid and free version. So I need to define at 2 my “flavor”, that is, I will pack the channels (fees and free version of the two different channels of APP), pay attention to in the present AS if you just define the code 2, then 3 is bound to be quoted in the build error, not defined product latitude, so in order to solve this problem, We define a “pay or not pay” latitude in code 1.

As shown in the figure, once we have defined the paid latitude and paid and free product flavors, you can see that we now have four build variants, namely the Debug and Release variants under the free and charge flavors, and now we have two different flavors. What if I want to install two different APKs on my phone? First of all, our package names cannot be the same, defined in the free and charge closure:

productFlavors{

    free{
        applicationId 'com.example.gradle.free'

    }

    charge{
        applicationId 'com.example.gradle.charge'}}Copy the code

According to the above configuration, there are two APKs running on the mobile phone at last, which I have verified, but it still does not reflect the alienation of travel. Let’s change the logic of the code next, and modify their APP name and home page

Look at our red box, I have changed the name of their app, how to deal with?

productFlavors{

    free{
        applicationId 'com.example.gradle.free'
        resValue "string".'appName'.'Free version'
    }

    charge{
        applicationId 'com.example.gradle.charge'
        resValue "string".'appName'.'Paid edition'}}Copy the code

First of all, I modified the productFlavors code and defined a resValue, which can be interpreted as defining labels for different flavors. For example, I defined resValue labels for free and charge flavors in the code. This tag can then be referenced when building their variants

As shown in the picture, I have also newly defined two folders “free” and “charge”. If you want to make differentiation, you must define the folders according to the product flavor. Here, because the name of the application is configured in the list file, we have copied each one. I understand is anyway since is differentiated from the main copy all the files will have difference in change, of course inside the package name, path, or to be consistent, and talk about this in fact you also will understand, if you want to do in a different APP page differentiation, then according to this formula, So “modify the home page (MainActivity)” task is over to you, hands-on experience.

FlavorDimensions more latitude

In the figure above I also define a nation latitude. Note that if you define a latitude then you must use it. Under each product flavor, use dimension to determine which latitude your flavor will use. IsPaying is used for free and charge, and Nation is used for China and France. The purpose of so many latitudes is to differentiate your APP ina more subtle way. For example, I can package different apps for different countries in this way, isn’t it flexible? Well, at the end of the day we can come up with a formula [number of product flavors at Latitude 1] * number of product flavors at Latitude 2] * number of buildTypes = number of final APK variants.