文章目录

Android Plugin DSL Reference 参考文档 :





一、gradle.properties 中配置编译参数



gradle.properties 中配置编译参数 , 注意等号两边不要有空格 ;

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true

# 配置是否在 Google Play 上架
isGooglePlay=true
# 配置当前的应用市场
market=GooglePlay






二、在 build.gradle 中配置 BuildConfig.java 生成信息



这里调用

void buildConfigField(String type, String name, String value)

方法 , 向 生成的 BuildConfig 类中添加新的字段 , 生成的字段样式为

<type> <name> = <value>;

这里需要注意 , 上述 3 3 3 个字符串原封不动的替换 ,

如果是字符串 , 需要使用如下样式声明 , 字符串外部的双引号 , 也需要手动使用转移字符串生成 ;

buildConfigField("String", "market", "\"${market}\"")



声明 BuildConfig 字段代码 :

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
applicationId "com.example.classloader_demo"
minSdkVersion 18
targetSdkVersion 30

// 应用是否在 Google Play 上架
buildConfigField("boolean", "isGooglePlay", isGooglePlay)
// 当前的应用市场
buildConfigField("String", "market", "\"${market}\"")
}
}



参考文档 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html






三、编译后生成的 BuildConfig 类



选择 " 菜单栏 / Build / Make Project " 选项 编译整个工程 , 或者使用 Ctrl + F9 快捷键 ;

【Android Gradle 插件】gradle.properties 中配置编译参数并在 Java 代码 BuildConfig 中调用该参数_BuildConfig

编译完成后生成的 BuildConfig 类 :

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";
}

【Android Gradle 插件】gradle.properties 中配置编译参数并在 Java 代码 BuildConfig 中调用该参数_build.gradle_02