Android Gradle manifestPlaceholders 預留位置詳解

來源:互聯網
上載者:User

標籤:pre   instr   字元   conf   總結   ring   指定位置   com   使用   

Android Gradle manifestPlaceholders 預留位置詳解

在實際項目中,AndroidManifest裡十幾個地方的值是需要動態改變(產生apk檔案的時候).如果每次去改也可以,但是累啊,在我之前他們打包是用手動替換,但我覺得這是是在沒辦法的辦法,但是有了manifestPlaceholders預留位置後就簡單的多了,只需要改一個地方就行了.

1. 概括

下面介紹下manifestPlaceholders預留位置的使用,其實很好理解,你可以認為它可以在 build.gradle檔案中定義字串並將值對應到 AndroidManifest資訊清單檔的指定位置.

下面說下使用方法:

2. 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>

總結如下:

${你定義名稱}

當然${}的前後面直接添加字串.

例如:

android:value="${test_value_activity}"android:value="headword${test_value}append_word"
3. 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 中的測試值"]    }

總結如下:

manifestPlaceholders = [symbol1: "value1", symbol2:"value2"]

多個值時用,分隔開.

4. 在Java代碼擷取預留位置的值

在擷取值的時候是區分佔位符在 Activity標籤,Application標籤,service標籤,receiver標籤下,所以有兩種擷取方式:

Activity標籤下:

                ActivityInfo activityInfo = null;                try {                    activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);                } catch (PackageManager.NameNotFoundException e) {                    e.printStackTrace();                }                if (activityInfo == null)return;                String value = activityInfo.metaData.getString("nameActivity");

在 Application標籤下:

                ApplicationInfo applicationInfo = null;                try {                    applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);                } catch (PackageManager.NameNotFoundException e) {                    e.printStackTrace();                }                if (applicationInfo == null)return;                String value = applicationInfo.metaData.getString("nameApplication");

同理,在service標籤,receiver標籤下分別為:

String value=  MainActivity.this.getPackageManager().getServiceInfo(ComponentName,PackageManager.GET_META_DATA).metaData.getString("symbol");               String value=  MainActivity.this.getPackageManager().getReceiverInfo(ComponentName,PackageManager.GET_META_DATA).metaData.getString("symbol");

Android Gradle manifestPlaceholders 預留位置詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.