来个栗子
apply plugin: 'com.android.application'
def app_version_code = 520
def app_version_name = '3.03'
android {
compileSdkVersion ANDROID_BUILD_SDK_VERSION as int
buildToolsVersion ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion ANDROID_BUILD_MIN_SDK_VERSION as int
targetSdkVersion ANDROID_BUILD_TARGET_SDK_VERSION
versionCode app_version_code
versionName app_version_name
ndk {
abiFilters "armeabi"
}
multiDexEnabled = true
manifestPlaceholders = [
GETUI_APP_ID : "8qFifaUOZb8tf1A8aKHzY9",
GETUI_APP_KEY : "90Dlp9PRdo7m65WLsr0lq8",
GETUI_APP_SECRET: "ZMtzPirZhS9Xvtz1eKoqi7"
]
}
signingConfigs {
xx_sign {
keyAlias 'xxxxxx'
keyPassword 'xxxxxx'
storeFile file('../xxxxxx')
storePassword 'xxxxxx'
}
yy_sign {
keyAlias 'yyyyyy'
keyPassword 'yyyyyy'
storeFile file('../yyyyyy')
storePassword 'yyyyyy'
}
}
buildTypes {
release {
minifyEnabled true
//BuildConfig.java文件中生产JUPM变量
buildConfigField("String", "JUMP", "\"0\"")
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
debug {
minifyEnabled false
buildConfigField("String", "JUMP", "\"1\"")
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'org/bouncycastle/x509/CertPathReviewerMessages_de.properties'
exclude 'org/bouncycastle/x509/CertPathReviewerMessages.properties'
}
lintOptions {
abortOnError false
}
dexOptions {
jumboMode = true
}
productFlavors {
xx {
dimension "name"
//自定义applicationId
applicationId "com.xxx"
//String资源文件中添加自定义键值对
resValue "string", "app_name", "XXXX"
//AndroidManifest文件中android:icon="${app_icon}"
manifestPlaceholders = [app_icon: "@drawable/icon"]
//BuildConfig.java文件中生产CHANNELID变量
buildConfigField("String", "CHANNELID", "\"1\"")
buildConfigField("String", "XDAPPID", "\"cn.bmbs\"")
//指定上文定义的签名
signingConfig signingConfigs.xx_sign
}
yy {
dimension "name"
applicationId 'com.yyyy'
resValue "string", "app_name", "YYYYYY"
manifestPlaceholders = [app_icon: "@drawable/icon_jd"]
buildConfigField("String", "CHANNELID", "\"2\"")
buildConfigField("String", "XDAPPID", "\"cn.bjds\"")
signingConfig signingConfigs.yy_sign
}
extra {
dimension "build"
buildConfigField("String", "BASE_URL", "\"${url_test}\"")
//gradle.properties文件中定义键值对res_url_test = http://101.204.7.14:802/pmbs/
buildConfigField("String", "RESOURCE_URL", "\"${res_url_test}\"")
buildConfigField("String", "COLLECT_URL", "\"${collect_url_test}\"")
buildConfigField("String", "MTDSERVERURL", "\"${mtdServerUrl}\"")
buildConfigField("String", "VIRUSURL", "\"${virusURL}\"")
}
}
android.applicationVariants.all {
variant ->
variant.outputs.all {
if (variant.flavorName == "jdExtra") {
outputFileName = "XXXX" + "_v_${versionName}.apk"
} else {
outputFileName = "YYYY" + "_v_${versionName}.apk"
}
}
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
api fileTree(dir: 'libs', include: ['*.jar','*.aar'])
implementation project(':common-base')
implementation 'com.android.support:design:28.0.0'
}
说明
manifestPlaceholders
它可以在 build.gradle文件中定义字符串并将值映射到 AndroidManifest清单文件的指定位置
AndroidManifest 文件定义占位符
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
// 这是在Activity 标签下
<meta-data android:name="nameActivity" android:value="${test_value_activity}"/>
</activity>
// 这是在Activity 标签下
<meta-data android:name="nameApplication" android:value="headword${test_value}append_word"/>
</application>
build.gradle 给占位符赋值
defaultConfig {
applicationId "com.didikee.wififriend"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
manifestPlaceholders = [test_value: "这是测试值",test_value_activity:"Activity 中的测试值"]
}
在Java代码获取占位符的值
ActivityInfo activityInfo = null;
ApplicationInfo applicationInfo = null;
ServiceInfo serviceInfo = null;
ActivityInfo receiverInfo = null;
try {
activityInfo = context.getPackageManager().getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
applicationInfo = context.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
serviceInfo = context.getPackageManager().getServiceInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
receiverInfo = context.getPackageManager().getReceiverInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
activityInfo.metaData.getString("nameActivity");
applicationInfo.metaData.getString("nameApplication") ;