[Android Gradle plugin] ProductFlavor configuration ( ProductFlavor#buildConfigField method | task of compiling and generating BuildConfig class separately)

Android Plugin DSL Reference reference documentation:





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;

insert image description here

向生成的 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>

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/123642963