有时根据项目需要,AndroidManifest.xml中的meta-data的值分测试和正式

为了能自动地更换meta-data值,就需要用到manifestPlaceholders

语法:manifestPlaceholders = [FieldName: FieldValue]

示例如下

build.gradle (Module: app)

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.bu_ish.debug_release_test"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildConfigField "String", "TextToShow", "\"当前处于发布模式\""
manifestPlaceholders = [ManifestField: "发布模式下的清单字段"]
}
debug {
buildConfigField "String", "TextToShow", "\"当前处于调试模式\""
manifestPlaceholders = [ManifestField: "调试模式下的清单字段"]
}
}
} dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bu_ish.debug_release_test"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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="ManifestField"
android:value="${ManifestField}" />
</application> </manifest>

上例中,名为ManifestField的meta-data的值为"发布模式下的清单字段"(Release模式下)或"调试模式下的清单字段"(Debug模式下)

获取该meta-data的示例如下

                try {
String manifestField = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData.getString("ManifestField");
new AlertDialog.Builder(MainActivity.this).setMessage(manifestField).show();
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, null, ex);
}

最新文章

  1. [原创]MYSQL的简单入门
  2. Nginx配置upstream实现负载均衡
  3. 洛谷 P1967 货车运输 Label: 倍增LCA &amp;&amp; 最小瓶颈路
  4. golang github.com/go-sql-driver/mysql 遇到的数据库,设置库设计不合理的解决方法
  5. Java web小记
  6. Android progressbar 详解
  7. poj1274 二分匹配
  8. Wireshark - ICMP 报文分析
  9. PHP5 session 详解
  10. 观【史上最牛linux视频教程】整理笔记,持续更新……
  11. hdu 2544
  12. 提取所有mtk机型的线刷包
  13. Google日历添加农历、节日和天气插件(步骤)
  14. netty中级篇(2)
  15. logback学习二
  16. 3,列表的 深 浅 copy
  17. maven下载其源代码包并关联
  18. kubernetes 安装手册(成功版)
  19. Linux 文件夹相关常用命令
  20. php处理数据分组问题

热门文章

  1. WebRTC VideoEngine综合应用示例(一)——视频通话的基本流程(转)
  2. 转 使用putty从linux主机上面往windows主机下面拷贝文件
  3. ssh的简单介绍
  4. python学习之-- 故障记录汇总
  5. python3 - pop 接收邮件/ smtp 发送邮件
  6. 2016-2017 ACM-ICPC, South Pacific Regional Contest (SPPC 16)
  7. Python通用编程
  8. jenkins发布普通项目、配置自动上线自动部署
  9. Andriod PopupWindow 键盘冲突
  10. Fresco的使用&lt;一&gt;