2019-03-30 Android Gradle manifestPlaceholders自定義變量取值

前言

最近寫了一個坑爹的方便其他方集成的微信,支付寶分享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

<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="cn.jpush.android.intent.NOTIFICATION_OPENED" />

? ? ? ? ? ? ? ? <!-- 接收網絡變化 連接/斷開 since 1.6.3 -->

? ? ? ? ? ? ? ? <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

? ? ? ? ? ? ? ? <!--<action android:name="cn.jpush.android.intent.CONNECTION" />-->

? ? ? ? ? ? ? ? <category android:name="${JPUSH_PKGNAME}" />

? ? ? ? ? ? </intent-filter>

? ? ? ? </receiver>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

比如微信支付

? ? ? ? <!-- 支付回調頁面 -->

? ? ? ? <activity

? ? ? ? ? ? android:name=".wxapi.WXPayEntryActivity"

? ? ? ? ? ? android:exported="true"

? ? ? ? ? ? android:launchMode="singleTop">

? ? ? ? ? ? <intent-filter>

? ? ? ? ? ? ? ? <action android:name="android.intent.action.VIEW" />

? ? ? ? ? ? ? ? <category android:name="android.intent.category.DEFAULT" />

? ? ? ? ? ? ? ? <data android:scheme="${WEATCH_APPID}" />

? ? ? ? ? ? </intent-filter>

? ? ? ? </activity>

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中代碼

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

? ? package="xxx.xxxx.xxxxx">

? ? <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

? ? <application

? ? ? ? android:allowBackup="true"

? ? ? ? android:icon="@mipmap/ic_launcher"

? ? ? ? android:label="@string/app_name"

? ? ? ? android:supportsRtl="true"

? ? ? ? android:theme="@style/AppTheme">

? ? ? ? <activity android:name=".PayTestActivity">

? ? ? ? ? ? <intent-filter>

? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />

? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />

? ? ? ? ? ? </intent-filter>

? ? ? ? ? <meta-data android:name="test" android:value="${WEATCH_APPID}"/>//這一句起到至關重要作用

? ? ? ? </activity>

? ? </application>

</manifest>

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中代碼

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

? ? package="xxx.xxxx.xxxxx">

? ? <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

? ? <application

? ? ? ? android:allowBackup="true"

? ? ? ? android:icon="@mipmap/ic_launcher"

? ? ? ? android:label="@string/app_name"

? ? ? ? android:supportsRtl="true"

? ? ? ? android:theme="@style/AppTheme">

? ? ? ? <activity android:name=".PayTestActivity">

? ? ? ? ? ? <intent-filter>

? ? ? ? ? ? ? ? <action android:name="android.intent.action.MAIN" />

? ? ? ? ? ? ? ? <category android:name="android.intent.category.LAUNCHER" />

? ? ? ? ? ? </intent-filter>

? ? ? ? </activity>

? ? ? ? <meta-data android:name="test" android:value="${WEATCH_APPID}"/>//這一句起到至關重要作用

? ? </application>

</manifest>

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中代碼

<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="cn.jpush.android.intent.NOTIFICATION_OPENED" />

? ? ? ? ? ? ? ? <!-- 接收網絡變化 連接/斷開 since 1.6.3 -->

? ? ? ? ? ? ? ? <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

? ? ? ? ? ? ? ? <!--<action android:name="cn.jpush.android.intent.CONNECTION" />-->

? ? ? ? ? ? ? ? <category android:name="${JPUSH_PKGNAME}" />

? ? ? ? ? ? </intent-filter>

? ? ? ? ? ? <meta-data android:name="test" android:value="${WEATCH_APPID}"/>//這一句起到至關重要作用

? ? ? ? </receiver>

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

版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,510評論 0 17
  • 本文重點介紹應用程序的啟動過程,應用程序的啟動過程實際上就是應用程序中的默認Activity的啟動過程,本文將詳細...
    天宇sonny閱讀 411評論 1 0
  • ¥開啟¥ 【加群QQ聊天源碼】 〖2017-08-25 15:24:36〗 《import "android.ne...
    小菜c閱讀 4,283評論 0 5
  • 1 進程啟動過程 Android應用程序框架層創建的應用程序進程具有兩個特點,一是進程的入口函數是Activit...
    Kevin_Junbaozi閱讀 3,893評論 0 23
  • 這組照片是我非常喜歡的 很多人都說婚紗照在婚禮上用過一次就壓箱底了 但是我們拍攝婚紗照的原意是讓這份幸福的記錄永遠...
    Sunshine六姐閱讀 236評論 0 1