Android 中使用gradle.properties

一、为什么使用gradle.properties?

  1. 对于项目而言,有时候需要配置某些敏感信息。比如密码,帐号等。而这些信息需要被很多类共同使用,所以必须有一个全局的配置

  2. 当需要把项目push到git上时,我们不希望别人看到我们项目的key,token等。我们可以将这些信息设置在gradle.properties中。

  3. 使用jenkins打包时,需要配置版本号等全局信息。

# 获取App版本号
APP_VERSION_STR=`grep "APP_VERSION=" gradle.properties`
APP_VERSION_CODE_STR=`grep "APP_VERSION_CODE=" gradle.properties`

二、如何使用gradle.properties?

  1. 在gradle.properties文件中进行变量初始化

《Android 中使用gradle.properties》 image.png

  1. 在build.gradle(module app)中进行变量的重定义,即将配置内容转化成java能够使用的形式
android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.test.daggerdemo"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode Integer.parseInt(APP_VERSION_CODE)
        versionName APP_VERSION_CODE
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField 'String', 'APP_NAME', "\"${APPNAME}\""
            resValue 'string', 'app_key', "${APPKEY}"
            resValue 'string', 'app_token', "${APPTOKEN}"
            resValue 'string', 'account', "${ACCOUNT}"
            resValue 'string', 'password', "${PASSWOED}"
        }

        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            applicationIdSuffix ".debug"
            versionNameSuffix "-debug"
            buildConfigField 'String', 'APP_NAME', "\"${APPNAME}\""
            resValue 'string', 'app_key', "${APPKEY}"
            resValue 'string', 'app_token', "${APPTOKEN}"
            resValue 'string', 'account', "${ACCOUNT}"
            resValue 'string', 'password', "${PASSWOED}"
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

《Android 中使用gradle.properties》 image.png

其中:
buildConfigField定义的方式是:buildConfigField 类型,变量名,值

 buildConfigField 'String', 'APP_NAME', "\"${APPNAME}\""

resValue定义的方式是:resValue XML中的类型,变量名,值

resValue 'string', 'app_key', "${APPKEY}"

注意

resValue中项目类型必须是 string 不能 String 否则会报错
项目名称不能包含空格 (作为正常资源名称)
  1. 在xml布局文件中的使用:
    通过build.gradle中的配置,可以直接使用@string 访问
<TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="@string/app_token"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

《Android 中使用gradle.properties》 image.png

  1. 在java中使用:
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //使用BuildConfig可以直接读取配置信息
        Toast.makeText(this, BuildConfig.APP_NAME, Toast.LENGTH_LONG).show();
        //resValue的使用
        getResources().getString(R.string.account);
    }
点赞