Gradle learning: Build Variant and gradle build variant
I. Build type
You can define the build type in the buildTypes code block. The following is the standard buildTypes code block of the Build File Created by AndroidStudio:
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
In your project, the release build type is not the only build type created. By default, each module has a debug build type. It is set to the default build type, but you can include it to the buildTypes code block and overwrite any attribute configuration you want to change.
1. Create a build type
When the default settings are not enough, we can easily create custom build types. For the new build type, you only need to add an object in the buildTypes code block.
android { buildTypes { staging { versionNameSuffix "-staging" buildConfigFiled "String","API_URL","https://xxx.xxx.xx" } }}
The staging build type defines a new suffix for applicationID, so that it is different from the applicationID of debug and release versions. Assume that your build type is default and the staging build type is added, the applicationID of Different build types should be as follows:
Debug com. package release com. package staging com. package. staging
This means that you will be able to install both staging and release versions on devices of the same color without any conflict. The staging build type also has a version name suffix. It is important to differentiate multiple application versions on the same device.
When creating a new build type, you can use another property of the build type to initialize the build type:
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } staging.initWith(buildTypes.debug) staging { debuggable = false }}
The initWith () method creates a new build type and copies all attributes of an existing build type to the new build type. We can overwrite or define additional properties by simply defining them in new build type objects.
2. Dependency
Gradle automatically creates new dependency configurations for each build type. If you just want to add a appcompat-v7 In the debug build. So you can do this:
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) debugCompile 'com.android.support:appcompat-v7:25.3.1'}
In this way, you can combine any build type of the build configuration to make the dependency more specific.
2. product flavor
Unlike different build types used to configure the same App or library, productflavor is used to create different versions. A typical example is that an application has a free version and a paid version.
If you are not sure whether you need a new build type or a new productflavor, you should ask yourself if you want to create an internal application, or a new APK that will be released to GooglePlay. If you need a brand new App independent of your existing App release, then productflavor is what you need. Otherwise, you should stick to the build type.
1. Create product flavor
productFlavors { red { manifestPlaceholders = [ APP_NAME : "@string/app_name"] } blue { manifestPlaceholders = [ APP_NAME : "@string/app_name_dev"] }}2. Source set
Similar to the build type, productflavor can have its own source Set directory. Creating a folder for a special flavor is as easy as creating a folder with a flavor name. You can even create a folder for a combination of a specific build type and flavor. The folder name is flavor name + build type name. For example, if you want to have a different application icon for the release version of blueflavor, the folder will be called blueRelease.
Merging folders has a higher priority than building folders and productflavor folders.
3. Multidimensional
In some cases, you may want to go further and create a combination of productflavor. For example, both Customer A and Customer B want free and paid versions in their apps, and they are based on the same code and different brands. Creating four different flavor means you need to set multiple copies like this, so this is not the best practice. The use of the flavor dimension is an effective combination of flavor, as shown below:
android { flavorDimensions "color", "price" productFlavors { red { flavorDimension "color" } blue { flavorDimension "color" } free { flavorDimension "price" } paid { flavorDimension "price" } } }
After you add a dimension for flavor, Gradle wants you to add a dimension for each flavor. If you forget, you will receive a build error with an accepted error.
The order of dimensions is very important. When two flavor instances are combined, they may define the same attributes or resources. In this case, the order of the flavor Dimension Array determines which flavor configuration will be overwritten. In the previous example, the color dimension overwrites the price dimension. This Order also determines the name of the build variant.
Assuming that the default build configurations are debug and release build types, the flavor defined in the preceding example will generate the following build variant:
BlueFreeDebug and blueFreeRelease bluePaidDebug and bluePaidRelease redFreeDebug and redFreeRelease redPaidDebug and redPaidRelease3, build variant
If no productflavor exists, variant will only contain the build type. Without any build types, it is impossible. Even if you do not define any build types, the Gradle Android plug-in will create a debug build type for your App or library.
3.1 Task
The Android plug-in of Gradle will create a task for each build variant configured for you. By default, a new Android Application has two build types: debug and release. Therefore, you can use assembleDebug and javaserelease to build two APKs respectively, that is, use a single command assemble to create two APKs.
Once you start to add flavor, the entire new task series will be created, because each build type task will be combined with each productflavor.
Only one BuildType + Product Flavor. You can get:
AssembleBlue: Use blue flavor to configure and assemble BlueRelease and BlueDebug. AssembleDebug: Build the configuration type using debug and assemble a debug version for each productflavor. AssembleBlueDebug: combines the build configuration type with the flavor configuration, and the flavor setting overwrites the build type setting. 3.2 source set
Build variant. It is a combination of a build type and one or more productflavor. It can have its own source set folder. For example, variant created by the debug build types blueflavor and freeflavor can have its own source set src/blueFreeDebug/java /. It can overwrite the folder location by using the sourceSets code block.
The introduction of the source set increases the complexity of the build process. Before packaging the application, the Android plug-in of Gradle combines the main source set and the build type source set. In addition, the library project can provide additional resources, which also need to be merged. This applies to manifest files.
Priorities of resources and manifest:
BuildType> Flavor> Main> Dependencies
If resources are declared in both the flavor and main source sets, the resources in flavor are given a higher priority. In this case, resources in the flavor source set will be packaged. Resources stated in the library project generally have the lowest priority.
This means that even if the appModule and the Library have resources of the same name, the appModule will overwrite the Library resources.
3.3 variant Filter
In your build, you can ignore some variant completely. In this way, you can use the assemble command to speed up the construction of all variant processes, and your task list will not be contaminated by any unnecessary tasks.
The variant filter also ensures that the filtered build variant is not displayed in the build variant switch of AndroidStudio.
You can use the following code to filter variants in the build. gradle file under the App or library root directory:
android.variantFilter { variant -> if(variant.buildType.name.equals('release')) { variant.getFlavors().each() { flavor -> if(flavor.name.equals('blue')) { variant.setIgnore(true); } } }}
In this example, we first check whether the build type of variant contains release, and then remove all Productflavor with this name. If the flavor dimension is not used, only one Productflavor is contained in the flavor array. Once you start using the flavor dimension, the flavor array will hold the same flavor as the dimension. In the sample script, we checked blueproductflavor and told the build script to ignore this variant.
3.4 signature configuration
The Android plug-in uses a general keystore and a known password to automatically create a debug configuration, so there is no need to create a signature configuration for this build type:
android { signingConfigs { staging.initWith(signingConfigs.debug) release { storeFilefile("release.keystore") storePassword "secretpassword" keyAlias "gradleforandroid" keyPassword "secretpassword" } }}
The release configuration uses storeFile to specify the path of the keystore file, and then defines the key alias and two passwords.
After defining the signature configuration, you need to apply them to your build type or flavor. Both the build type and flavor have a property called signingConfi, as shown below:
buildTypes { release { signingConfigsigningConfigs.release }}
This example uses the build type, but if you want to use different creden, for each flavor you create, you need to create different signature configurations, however, you can define them in the same way:
productFlavors { blue { signingConfigsigningConfigs.release } }
Using signature configuration may cause many problems. When a configuration is assigned to flavor, it actually overwrites the signature configuration of the build type. If you do not want to, you should assign different keys to each flavor of each build type when using flavor.