android 怎样优雅的使用配置文件中的定义变量

1.在gradle.properties中定义变量

2.在build.gradle中定义变量

 

1.在gradle.properties中定义变量,例如:

APP_VERSION_CODE=622
APP_VERSION_NAME=6.2.0

使用方式:(需要注意的是,gradle.properties中定义的都是string变量,需要通过.toInteger 或者.toBoolean转换类型)

android 怎样优雅的使用配置文件中的定义变量

android 怎样优雅的使用配置文件中的定义变量

 

2.在项目根目录的build.gradle中定义变量

ext {
    vars = [
            // SDK And Tools
            minSdkVersion        : 18,
            targetSdkVersion     : 27,
            compileSdkVersion    : 27,
            buildToolsVersion    : '27.0.3',

            //Dependencies
            supportLibraryVersion: '27.0.2'
    ]

}

使用方式:

android 怎样优雅的使用配置文件中的定义变量

或者

android 怎样优雅的使用配置文件中的定义变量

 

3.在congfig.gradle中定义变量

ext {
    android = [
            compileSdkVersion: 28,
            minSdkVersion    : 21,
            targetSdkVersion : 28,
            versionCode      : 2019080100,
            versionName      : "1.0.0",
            dbVersion        : 2
    ]
    dependVersion = [
            appcompat: "1.1.0-alpha04",
            rxjava   : "2.2.8"
    ]

    dataDependencies = [
            rxjava: "io.reactivex.rxjava2:rxjava:${dependVersion.rxjava}",
    ]

    domainDependencies = [
            rxjava: "io.reactivex.rxjava2:rxjava:${dependVersion.rxjava}"
    ]

    dependencies = [
            appcompat: "androidx.appcompat:appcompat:${dependVersion.appcompat}"
    ]

   
}

 

使用:

android 怎样优雅的使用配置文件中的定义变量

 

android 怎样优雅的使用配置文件中的定义变量

4.在local.properties中定义变量

这个文件一般不会上传至远程git ,所以用来存放一些本地要用的常量 sdk ndk路径,签名文件信息等

    ndk.dir=D\:\\soft\\android-ndk-r10e

    sdk.dir=D\:\\soft\\SDKandroidStudio

    key.file=D\:\\work\\Key.jks

    keyAlias=hahaha

    keyPassword=1234567890

    storePassword=1234567

具体使用:

android 怎样优雅的使用配置文件中的定义变量