Android Gradle manifestplaceholders Placeholder Explanation
In the actual project, the AndroidManifest
value in more than 10 places is required to change dynamically (when the APK file is generated). If you can change it every time, but tired, before I they pack is manually replaced, but I think it is manifestPlaceholders
in no way, but with the After the placeholder is much simpler, just need to change a place on the line.
1. Summary
Here manifestPlaceholders
's how to use the next placeholder, which is a good understanding, and you can think of it as defining a string in a build.gradle
file and mapping the value to a AndroidManifest
specified location in the manifest file.
Here's how to use it:
2. androidmanifest file Definition Placeholder
Some of the code is as follows:
<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>
Summarized as follows:
${你定义名称}
${}
the front and back sides of course add strings directly.
For example:
android:value="${test_value_activity}"android:value="headword${test_value}append_word"
3. Assigning values to placeholders by Build.gradle
Some of the code is as follows:
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 中的测试值"] }
Summarized as follows:
manifestPlaceholders = [symbol1: "value1", symbol2:"value2"]
Multiple values are separated by a ,
partition.
4. Get the value of the placeholder in Java code
There are two ways to obtain a value when the placeholder is distinguished from the Activity标签,Application标签,service标签,receiver标签
next:
In 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");
In 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");
In the same vein, the service标签,receiver标签
following are:
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 Placeholder Explanation