前言
最近写了一个坑爹的方便其他方集成的微信,支付宝分享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'
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
第二种:
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",
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
第三种:
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")
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
二.AndroidMainfest.xml中使用
第一种:直接使用
比如极光receiver
android:name=".jpush.MyReceiver" android:enabled="true">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
比如微信支付
android:name=".wxapi.WXPayEntryActivity" android:exported="true" android:launchMode="singleTop">
1
2
3
4
5
6
7
8
9
10
11
三.在Java类中获取[在service,receiver,Activity,Application中获取值]
原理:通过androidMainfest.xml把值反射到对应类标签,设置value的key值,在java类中通过key取值得到value
第一种:Activity类中获取build.gradle申明变量
AndroidMainfest.xml中代码
package="xxx.xxxx.xxxxx"> android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Activity中代码
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.reach.doooly.utils.StringUtlis;
import com.reachdoooly.pay.utils.ReachLogs;
/**
* Created by Albert on 2018/5/22.
*/
public class PayTestActivity extends Activity {
private Button ali_pay;//阿里支付
private Button weachat_pay;//微信支付
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.pay_test );
ActivityInfo activityInfo = null;
String value ="";
try {
activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
value=activityInfo.metaData.getString("test");
} catch (PackageManager.NameNotFoundException e) {
}
if(!StringUtlis.isEmpty ( value )){
ReachLogs.e ("fuqinming","appId:"+value);
}
weachat_pay = (Button) findViewById ( R.id.wechat_pay );
weachat_pay.setOnClickListener ( new View.OnClickListener () {
@Override
public void onClick(View v) {
//new NewClass().createNewPay(PayTestActivity.this);
// Toast.makeText ( PayTestActivity.this, PayUtils.getAppPackageName ( PayTestActivity.this ), Toast.LENGTH_SHORT ).show ();
}
} );
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
第二种:Application类中获取build.gradle申明变量
AndroidMainfest.xml中代码
package="xxx.xxxx.xxxxx"> android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在Application中代码
private void getWeatchAppID(){
String value ="";
try {
ApplicationInfo applicationInfo=getPackageManager().getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);
value=applicationInfo.metaData.getString("test");
} catch(Exception e) {
value="";
}
}
1
2
3
4
5
6
7
8
9
第三种:在SERVICE,RECEIVER中获取
这两种基本上差不多就不分开写了,就只写一种,另外一种赵淼画瓢即可。
在AndroidMainfest.xml中代码
android:name=".jpush.MyReceiver" android:enabled="true">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
在receiver类中代码
private void getWeatchAppID(){
String value ="";
try {
ApplicationInfo applicationInfo=activity.getPackageManager().getServiceInfo(ComponentName,PackageManager.GET_META_DATA);
ApplicationInfo applicationInfo=activity.getPackageManager().getReceiverInfo(ComponentName,PackageManager.GET_META_DATA);
//只有这里不同,亲们注意哈getReceiverInfo,getServiceInfo
value=applicationInfo.metaData.getString("test");
} catch(Exception e) {
value="";
}
}
1
2
3
4
5
6
7
8
9
10
11
4.特别需要注意
因为在我的项目中,是在core包中写入appID与appSercert等信息,就需要特别注意,我的代码是在core包中有一个父Activity,如何在父activity中获取appid,需要特别注意
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.widget.Toast;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
/**
* 微信支付base类
*/
public class WeatchPayBaseActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI api;
private PayReq payReq;//微信支付
public void initWXAPI(Activity context) {
String APP_ID ="";
ReachLogs.e ("fuqinming","packagename:"+getPackageName ());
-----第一种写法----
try {
ApplicationInfo activityInfo =this.getPackageManager().getApplicationInfo(this.getPackageName (), PackageManager.GET_META_DATA);
APP_ID=activityInfo.metaData.getString("WEATCG_APPID");
} catch (Exception e) {
}
if(null == APP_ID || "".equals ( APP_ID.trim () ) || "null".equals ( APP_ID.trim () )){
ReachLogs.e ("fuqinming","appId:");
}else{
ReachLogs.e ("fuqinming","appId:"+APP_ID);
}
----第二种写法----
try {
ApplicationInfo appInfo = getPackageManager ().getApplicationInfo ( getPackageName (),
PackageManager.GET_META_DATA );
APP_ID=appInfo.metaData.getString("WEATCH_APPID");
}catch (Exception e){
}
ReachLogs.e ("fuqinming","APP_ID:"+APP_ID);
payReq = new PayReq ();
if (null == APP_ID || "".equals ( APP_ID.trim () ) || "null".equals ( APP_ID.trim () )) {
throw new NullPointerException ();
}
// 初始化分享
api = WXAPIFactory.createWXAPI ( context, APP_ID, true );
api.handleIntent ( context.getIntent (), this );
api.registerApp ( APP_ID );
}
@Override
public void onReq(BaseReq baseReq) {
}
/***
* 微信支付[NATIVIE TO HTML]
*
* @param weachPayVo
* @add 2017-10-10
*/
public void wechatPay(WeachPayBeanVo weachPayVo) {
if(payReq==null){
throw new NullPointerException ("payReq The object is not set");
}
if (payReq != null && weachPayVo != null && !WeacthConsts.isEmpty(weachPayVo.getAppid()) && !WeacthConsts.isEmpty(weachPayVo.getNoncestr())
&& !WeacthConsts.isEmpty(weachPayVo.getPackageValue()) && !WeacthConsts.isEmpty(weachPayVo.getPartnerid())
&& !WeacthConsts.isEmpty(weachPayVo.getPrepayid()) && !WeacthConsts.isEmpty(weachPayVo.getSign())
&& !WeacthConsts.isEmpty(weachPayVo.getTimestamp())) {
payReq.appId = weachPayVo.getAppid();
payReq.partnerId = weachPayVo.getPartnerid();// 微信支付分配的商户号
payReq.prepayId = weachPayVo.getPrepayid();// 微信返回的支付交易会话ID
payReq.packageValue = weachPayVo.getPackageValue();// 扩展字段占时填固定
payReq.nonceStr = weachPayVo.getNoncestr();// 随机字符串
payReq.timeStamp = weachPayVo.getTimestamp();// 时间戳
payReq.sign = weachPayVo.getSign();// 签名
api.sendReq(payReq);
} else {
Toast.makeText (this,getString(R.string.error_order_msg),Toast.LENGTH_SHORT);
}
}
// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
public void onResp(BaseResp resp) {
}
}
---------------------
作者:CherryChen88
来源:CSDN
原文:https://blog.csdn.net/ONLYMETAGAIN/article/details/80497789
版权声明:本文为博主原创文章,转载请附上博文链接!