buildConfigField可以定义我们需要的变量,并在代码中引用。
gradle配置如下:
applicationId "com.flyscale.gradleflavor.b" //applicationId是Gradle定义好的变量
versionName "version-b-1.0" //versionName是Gradle定义好的变量
buildConfigField("String", "PRODUCT_NAME", "\"b\""); //新定义String类型变量PRODUCT_NAME,值为"b"
buildConfigField("boolean", "LOG_DEBUG", "false"); //新定义boolean类型变量LOG_DEBUG,值为false
执行sync
后会生成如下代码:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.flyscale.gradleflavor.b";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "productB";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "version-b-1.0";
// Fields from product flavor: productB
public static final boolean LOG_DEBUG = false;
public static final String PRODUCT_NAME = "b";
}
这时我们就可以在代码中调用了:
Log.d(TAG, "APPLICATION_ID=" + BuildConfig.APPLICATION_ID);
Log.d(TAG, "LOG_DEBUG=" + BuildConfig.LOG_DEBUG);
Log.d(TAG, "PRODUCT_NAME=" + BuildConfig.PRODUCT_NAME);
网友评论