【Android Gradle 插件】gradle.properties 中配置编译参数并在 Java 代码 BuildConfig 中调用该参数
生活随笔
收集整理的這篇文章主要介紹了
【Android Gradle 插件】gradle.properties 中配置编译参数并在 Java 代码 BuildConfig 中调用该参数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一、gradle.properties 中配置編譯參數(shù)
- 二、在 build.gradle 中配置 BuildConfig.java 生成信息
- 三、編譯后生成的 BuildConfig 類
Android Plugin DSL Reference 參考文檔 :
- 文檔主頁(yè) : https://google.github.io/android-gradle-dsl/2.3/
- android 模塊配置文檔 : https://google.github.io/android-gradle-dsl/2.3/com.android.build.gradle.AppExtension.html
- ProductFlavor 文檔 : https://google.github.io/android-gradle-dsl/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html
一、gradle.properties 中配置編譯參數(shù)
gradle.properties 中配置編譯參數(shù) , 注意等號(hào)兩邊不要有空格 ;
# 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 # 配置當(dāng)前的應(yīng)用市場(chǎng) market=GooglePlay二、在 build.gradle 中配置 BuildConfig.java 生成信息
這里調(diào)用
void buildConfigField(String type, String name, String value)方法 , 向 生成的 BuildConfig 類中添加新的字段 , 生成的字段樣式為
<type> <name> = <value>;這里需要注意 , 上述 333 個(gè)字符串原封不動(dòng)的替換 ,
如果是字符串 , 需要使用如下樣式聲明 , 字符串外部的雙引號(hào) , 也需要手動(dòng)使用轉(zhuǎn)移字符串生成 ;
buildConfigField("String", "market", "\"${market}\"")聲明 BuildConfig 字段代碼 :
android {compileSdkVersion 30buildToolsVersion "30.0.3"defaultConfig {applicationId "com.example.classloader_demo"minSdkVersion 18targetSdkVersion 30// 應(yīng)用是否在 Google Play 上架buildConfigField("boolean", "isGooglePlay", isGooglePlay)// 當(dāng)前的應(yīng)用市場(chǎng)buildConfigField("String", "market", "\"${market}\"")} }參考文檔 : android-gradle-dsl-gh-pages/2.3/com.android.build.gradle.internal.dsl.ProductFlavor.html
三、編譯后生成的 BuildConfig 類
選擇 " 菜單欄 / Build / Make Project " 選項(xiàng) 編譯整個(gè)工程 , 或者使用 Ctrl + F9 快捷鍵 ;
編譯完成后生成的 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"; }總結(jié)
以上是生活随笔為你收集整理的【Android Gradle 插件】gradle.properties 中配置编译参数并在 Java 代码 BuildConfig 中调用该参数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【错误记录】Android Gradle
- 下一篇: 【数字信号处理】傅里叶变换性质 ( 序列