Article directory
Android Plugin DSL Reference reference documentation:
-
Documentation homepage: https://google.github.io/android-gradle-dsl/2.3/
-
AppExtension ( build.gradle#android 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.AppExtension.html
-
build.gradle#android module configuration documentation: android-gradle-dsl/2.3/com.android.build.gradle.AppExtension.html
-
ProductFlavor ( build.gradle#android#defaultConfig 配置 ) 文档 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html
-
ProductFlavor#externalNativeBuild 配置 ( build.gradle#android#defaultConfig#externalNativeBuild 配置 ) : com.android.build.gradle.internal.dsl.ProductFlavor:externalNativeBuild
-
ExternalNativeBuildOptions ( build.gradle#android#defaultConfig#externalNativeBuild 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.ExternalNativeBuildOptions.html
-
NdkBuildOptions ( build.gradle#android#defaultConfig#externalNativeBuild#ndkBuild 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.NdkBuildOptions.html
-
CmakeOptions ( build.gradle#android#defaultConfig#externalNativeBuild#cmake 配置 ) 文档位置 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.CmakeOptions.html
-
JackOptions ( build.gradle#android#defaultConfig#jackOptions 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.JackOptions.html
-
AnnotationProcessorOptions (annotation processor configuration) (build.gradle#android#defaultConfig#javaCompileOptions configuration) Documentation location: android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.AnnotationProcessorOptions.html
-
NdkOptions ( build.gradle#android#defaultConfig#ndk 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.NdkOptions.html
-
VectorDrawablesOptions ( build.gradle#android#defaultConfig#vectorDrawables 配置 ) 文档位置 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.VectorDrawablesOptions.html
-
NDK reference documentation: Add C and C++ Code to Your Project.
1. ProductFlavor#buildConfigField method
ProductFlavor ( build.gradle#android#defaultConfig 配置 ) 文档 : android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html
Most of the methods in ProductFlavor assign values to the properties of ProductFlavor;
The ProductFlavor#buildConfigField method is used to specify the generated fields in the BuildConfig class at compile time;
向生成的 BuildConfig 类添加一个新字段。
该字段生成为:<type><name>=<value>;
这意味着每一个都必须有有效的Java内容。如果类型是字符串,则该值应包含引号。
call here
void buildConfigField(String type, String name, String value)
method, add a new field to the generated BuildConfig class, the generated field style is
<type> <name> = <value>;
It should be noted here that the above 3 33 strings are replaced intact,
If it is a string, you need to use the following style declaration, double quotes outside the string, and you need to manually use the transfer string to generate ;
buildConfigField("String", "market", "\"${market}\"")
Declare the BuildConfig field code in build.gradle:
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.classloader_demo"
minSdkVersion 18
targetSdkVersion 30
buildConfigField("boolean", "isGooglePlay", "true")
buildConfigField("String", "market", '"GooglePlay"')
}
}
The BuildConfig class generated after compilation is complete:
package com.example.classloader_demo;
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.example.classloader_demo";
public static final String BUILD_TYPE = "debug";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Field from default config.
public static final boolean isGooglePlay = true;
// Field from default config.
public static final String market = "GooglePlay";
}
Actual usage: Refer to [Android Gradle Plugin] to configure compilation parameters in gradle.properties and call the parameters in Java code BuildConfig blog;
2. Execute the task of compiling BuildConfig with Gradle alone
In the Android Gradle plugin, a Gradle task for compiling the BuildConfig class alone is provided, that is, the generateDebugBuildConfig task;
From the Android Studio command line, you can execute
gradlew generateDebugBuildConfig
command, compile and generate BuildConfig class separately;
Results of the :
Y:\002_WorkSpace\001_AS\SVG>gradlew generateDebugBuildConfig
BUILD SUCCESSFUL in 4s
3 actionable tasks: 2 executed, 1 up-to-date
Y:\002_WorkSpace\001_AS\SVG>