Android开发Android开发Android技术知识

Android笔记(1) 如何使用gradle.properti

2019-07-11  本文已影响4人  帅气的帽子

build.gradle使用gradle.properties配置相关信息

APPLICATION_ID=com.example.multi
VERSION_CODE=2
VERSION_NAME=1.1

然后在build.gradle中直接引用即可

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId APPLICATION_ID
        minSdkVersion 17
        targetSdkVersion 28
        versionCode VERSION_CODE as int
        versionName VERSION_NAME
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

ok,就这么简单。通过代码动态获取packageName,versionName,versionCode很简单就不用多说了。

APPLICATION_ID=com.example.multi
VERSION_CODE=2
VERSION_NAME=1.1

# 应用base_url
BASE_API="http://my-api.test.com/"
# 渠道
CHANNEL="xiaomi"
# log开关
LOG_SWITCH=true

然后,在build.gradle中去自定义这些配置

defaultConfig {
        applicationId APPLICATION_ID
        minSdkVersion 17
        targetSdkVersion 28
        versionCode VERSION_CODE as int
        versionName VERSION_NAME

        // 通过buildConfigField自定义配置信息
        buildConfigField("String", "BASE_API", "${BASE_API}")
        buildConfigField("String", "CHANNEL", "${CHANNEL}")
        buildConfigField("boolean", "LOG_SWITCH", "${LOG_SWITCH}")
    }

好了,现在可以尽情的在代码中使用这些配置了

String url = BuildConfig.BASE_API;
String channel = BuildConfig.CHANNEL;
boolean logSwitch = BuildConfig.LOG_SWITCH;

这个时候我们可能想到了,既然gradle.properties中的配置信息可以在build.gradle中运用,那是否可以在AndroidManifest.xml中引用呢?答案是肯定的。

# 渠道
CHANNEL="xiaomi"
# app名字
APP_NAME=配置demo

然后需要在build.gradle中进行声明

defaultConfig {
        // 在清单文件中需要引用的参数
        manifestPlaceholders = [
                APP_NAME      : APP_NAME,
                CHANNEL       : CHANNEL]
}

之后就是在AndroidManifest.xml中引用

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="${APP_NAME}"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data android:name="CHANNEL"
            android:value="${CHANNEL}"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>

最后,在Java代码中使用

ApplicationInfo appInfo = this.getPackageManager()
                    .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
String channel = appInfo.metaData.getString("CHANNEL");

但是测试阶段呢,每个环境(测试、预生产、生产)以及bug开启的、关闭的,不同渠道的,甚至不同应用包名、应用名字的、不同版本号的包。要疯?这么多种情况一个一个打吗?
一般公司都有自己的beta服务器,有的公司是使用Jenkins自定义多渠道打包的,这个时候就派上用场了,只需要通过脚本(当然脚本会的话可以自己写)在打包的的时候动态修改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

APPLICATION_ID=com.example.multi
VERSION_CODE=2
VERSION_NAME=1.1

# 应用base_url
BASE_API="http://my-api.test.com/"
# log开关
LOG_SWITCH=true
# 渠道
CHANNEL="xiaomi"
# app名字
APP_NAME=配置demo

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId APPLICATION_ID
        minSdkVersion 17
        targetSdkVersion 28
        versionCode VERSION_CODE as int
        versionName VERSION_NAME

        // 通过buildConfigField自定义配置信息
        buildConfigField("String", "BASE_API", "${BASE_API}")
        buildConfigField("String", "CHANNEL", "${CHANNEL}")
        buildConfigField("boolean", "LOG_SWITCH", "${LOG_SWITCH}")

        // 在清单文件中需要引用的参数
        manifestPlaceholders = [
                APP_NAME      : APP_NAME,
                CHANNEL       : CHANNEL]
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.multi_channel">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="${APP_NAME}"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <meta-data android:name="CHANNEL"
            android:value="${CHANNEL}"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
上一篇下一篇

猜你喜欢

热点阅读