zoukankan      html  css  js  c++  java
  • Android笔记之manifestPlaceholders

    有时根据项目需要,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);
                    }
  • 相关阅读:
    Android学习---- 十月
    第17章 使用PHP和MySQL实现身份验证
    第13章 MySQL高级编程
    第12章 MySQL高级管理
    第11章 使用PHP从Web访问MySQL数据库
    第10章 使用MySQL数据库
    第9章 创建Web数据库
    第5章 代码重用与函数编写
    NOIP模拟赛-奶牛晒衣服(dry)
    BZOJ1008 /乘法原理+快速幂 解题报告
  • 原文地址:https://www.cnblogs.com/buyishi/p/10523812.html
Copyright © 2011-2022 走看看