前言
最近写了一个坑爹的方便其他方集成的微信,支付宝分享SDK,其中的appid等参数,本人使用的配置化,就遇到了在Gradle manifestPlaceholders自定义变量取值问题
一.声明变量值
申明变量的原理
看过源码,其实就是一个HashMap的对象,我们在build.gradle中写入,然后映射到AndroidMainfest.xml中,HashMap对象放置在activityInfo.metaData中,我们可以通过activityInfo.metaData.keyset()查看所有设置的值
首先了解,在build.gradle中如何添加变量可以写在如下的位置:
第一种:
defaultConfig {
applicationId "xxx.xxxxxx.xxxxxx"
minSdkVersion 20
targetSdkVersion 18
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
manifestPlaceholders = [
"WEATCH_APPID": "--------------",
]
}
buildTypes {
debug {
signingConfig signingConfigs.debug
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
manifestPlaceholders = [
"WEATCH_APPID": "--------------",
]
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
第二种:
compileSdkVersion 18
buildToolsVersion '27.0.2'
defaultConfig {
applicationId "xxx.xxxxx.xxxxx"
minSdkVersion 20
targetSdkVersion 18
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
manifestPlaceholders = [
"WEATCH_APPID": "dddddddd",
]
}
第三种:
compileSdkVersion 18
buildToolsVersion '27.0.2'
defaultConfig {
applicationId "xxx.xxxxxx.xxxxxxx"
minSdkVersion 20
targetSdkVersion 18
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
productFlavors {
google {
manifestPlaceholders.put("UMENG_CHANNEL","google")
}
baidu {
manifestPlaceholders.put("UMENG_CHANNEL","baidu")
}
}
}
二.AndroidMainfest.xml中使用
第一种:直接使用
比如极光receiver
<receiver
android:name=".jpush.MyReceiver"
android:enabled="true">
<intent-filter>
<!--Required 用户注册SDK的intent-->
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<!--Required 用户接收SDK消息的intent-->
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<!--Required 用户接收SDK通知栏信息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<!--Required 用户打开自定义通知栏的intent-->
<action android:name