1. Basic knowledge of gradle
Gradle is based on the Groovy language. An Android Project project basically generates three gradle files: module gradle, project gradle and setting gradle
- module gradle A module has one, as shown in the figure above app.gradle and firstflavor.gradle. The gradle is to configure the information of this module
- project gradle as shown in QQView, the gradle configures the total information of the project
- setting gradle as shown in Settings, specify the module used by the project
Second, use gradle to hide sensitive signature information
- Create a keystore.properties file in the root directory and configure the signature information in the file
- Load the signature information in the properties file in the gradle file of the main module
Use the properties instance instead of the original signature in the signature configuration
3. Use buildType and flavor
BuildType and flavor can configure most of the mudule, and they are also the key to dynamic dependencies.
In the upper left corner of AS, File->Project Structure-> can find the quick configuration of buildType and flavor,
including many common parameters
Among them, SigningConfig can configure the signature information in Singning. VersionName Suffix and ApplicationId Stuffix respectively represent the suffix name of the version information and the suffix name of the package name .
Some parameters can be configured in both buildTypes and Flavor, but if they are repeated, they will be replaced by buildTypes, such as buildConfigField, which can configure constants and then use Build.IBS references in the code.
It can be seen that buildTypes has debug, realease, and many other configurations. The same is true for flavors, that is, there are a total of buildtypes × flavor combinations, which can be used for multi-channel packaging, compilation and customization.
differentiated packaging
Right-click in the src directory of the mudule directory to create a new JavaFolder file, there will be multiple options to choose from, corresponding to debug, release, and other combinations. After the file is built, the same xml resource file as the main file can be used in it, and the original main resource file will be overwritten when compiling and packaging, thereby realizing dynamic packaging. For example, you can change the icon in the mipmap file, and different packages will have different application icons.
Differentiated Packaging II
Create a new module with the same name as a defined flavor. After configuring the version information in the gradle file of the module, also change the resource file to be replaced. However, you need to delete the file with the same name in the main file, otherwise it will be invalid. This is different from the one above .
Differential compilation three
Differentiated packaging using compile
By default, compile is used first, testCompile is for unit testing, and xxxCompile means that it is compiled according to flavor preferences. When using module as a dependent library, you only need to compile project ':library' or xxxCompile project ':library'. When using module as flavor for dynamic compilation (the above method is packaged differently), the module and flavor need to have the same name, and You need to use xxxCompile project
(xxx is the flavor name), and you need to delete the file with the same name in the main module.
About using variables
In the manifest, you can use key-value pairs for Application or Acticity, and then access them in code.
Among them, the use of EL expressions for ${APP_NAME_THIS} is also similar to the usage of strings in kotlin. The value configuration of the ${xx} variable in the manifest is expressed as flavor and buildTypes.
manifestPlaceholder = [APP_NAME:"name"(value)]
When ${xx} is a variable in gradle, for example, compile'xxx' needs to be changed to compile "xxxx${xx}", and then configure the value in the project
This is roughly the case. I am still relatively unfamiliar with gradle, so I am taking notes and making persistent efforts.
Reference study article
[1]: android studio packaging and gradle configuration construction
[2]: Android essential and easily overlooked knowledge Advanced usage of Gradle