Android studio專案中的gradle.properties詳解
Android studio專案中的gradle.properties詳解
在使用Android Studio新建Android專案之後,在專案根目錄下會預設生成一個gradle.properties檔案,我們可以在裡面做一些Gradle檔案的全域性性的配置,也可以將比較私密的資訊放在裡面,防止洩露。
下面我們就來分析下IDE自動生成的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=-Xmx1536m # 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
gradle.properties註釋的翻譯
專案級別的Gradle配置檔案
親愛的Android Studio開發者們:
在IDE中指定的的配置可以覆蓋這個檔案中相應的配置。
如果想了解更多如何配置編譯環境,訪問http://www.gradle.org/docs/current/userguide/build_environment.html
在各個模組的build.gradle裡面直接引用
gradle.properties裡面定義的屬性是全域性的,可以在各個模組的build.gradle裡面直接引用.
在gradle.properties檔案中新增下面屬性:
COMPILE_SDK_VERSION=28 MIN_SDK_VERSION=15 TARGET_SDK_VERSION=28 SUPPORT_APPCOMPAT_V7_VERSION=28.0.0
注意:在gradle.properties中定義的屬性預設是String型別的,如果需要int型別,需要新增XXX as int
字尾。
在根目錄的settings.gradle中引用:
// 輸出Gradle物件的一些資訊
def printGradleInfoInRoot(){
println "COMPILE_SDK_VERSION:" + COMPILE_SDK_VERSION
}
printGradleInfoInRoot()
在app/build.gradle檔案中引用:
android { compileSdkVersion COMPILE_SDK_VERSION as int defaultConfig { applicationId "com.tinytongtong.gradle" minSdkVersion MIN_SDK_VERSION as int targetSdkVersion TARGET_SDK_VERSION as int ... } ... } dependencies { ... implementation "com.android.support:appcompat-v7:${SUPPORT_APPCOMPAT_V7_VERSION}" ... }
另外,在gradle.properties中配置的屬性,我們可以在執行gradle properties
後看到,結果如下:
...
> Task :properties
------------------------------------------------------------
Root project
------------------------------------------------------------
COMPILE_SDK_VERSION: 28
INTERNAL__CHECKED_MINIMUM_PLUGIN_VERSIONS: true
MIN_SDK_VERSION: 15
SUPPORT_APPCOMPAT_V7_VERSION: 28.0.0
TARGET_SDK_VERSION: 28
...
在Java程式碼或者xml佈局檔案中使用
1、在gradle.properties中新增配置:
# Java 程式碼使用
DEFAULT_NICK_NAME=maolegemi
DEFAULT_NUMBER=10086
# xml檔案呼叫
USER_NAME=wangdandan
TEXT_SIZE=20sp
TEXT_COLOR=#ef5350
2、在app/build.gradle中讀取屬性,對這些屬性進行轉化:
android {
...
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug{
// Java程式碼呼叫
buildConfigField "String", "defaultNickName", "\"${DEFAULT_NICK_NAME}\""
buildConfigField "Integer", "defaultNumber", DEFAULT_NUMBER
// xml佈局檔案呼叫
resValue "string", "user_name", "${USER_NAME}"
resValue "dimen", "text_size", "${TEXT_SIZE}"
resValue "color", "text_color", "${TEXT_COLOR}"
}
}
}
上述程式碼中的buildConfigField方法的是BuildType#buildConfigField,包含三個引數:型別、變數名、變數值。resValue方法定義在BuildType#resValue中,也包含三個引數:型別、變數名、變數值。
3、在Java中呼叫:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("defaultNickName:" + BuildConfig.defaultNickName);
System.out.println("defaultNickName.type:" + BuildConfig.defaultNickName.getClass().getSimpleName());
System.out.println("defaultNumber:" + BuildConfig.defaultNumber);
System.out.println("defaultNumber.type:" + BuildConfig.defaultNumber.getClass().getSimpleName());
}
輸出結果:符合預期。
2018-12-07 13:59:38.703 9854-9854/com.tinytongtong.gradle I/System.out: defaultNickName:maolegemi
2018-12-07 13:59:38.703 9854-9854/com.tinytongtong.gradle I/System.out: defaultNickName.type:String
2018-12-07 13:59:38.704 9854-9854/com.tinytongtong.gradle I/System.out: defaultNumber:10086
2018-12-07 13:59:38.704 9854-9854/com.tinytongtong.gradle I/System.out: defaultNumber.type:Integer
4、在xml佈局檔案中呼叫:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="@dimen/text_size"
android:textColor="@color/text_color"
/>
執行結果: