AndroidGradle为我们提供了非常便捷的方式让我们来替换AndroidManifest文件中的内容

116 阅读1分钟

AndroidGradle为我们提供了非常便捷的方式让我们来替换AndroidManifest文件中的内容-> manifestPlacholder、Manifest占位符。

flavorDimensions += listOf( "mode")
 productFlavors {
     create("env_dev") {
         // Assigns this product flavor to the "mode" flavor dimension.
         dimension = "mode"
     /*    manifestPlaceholders["app_name"] = "小米"  两种写法*/
         manifestPlaceholders.put("app_name","小米")
     }
     create("env_pre") {
         dimension = "mode"
           manifestPlaceholders.put("app_name","华为")
     }

     create("env_prod") {
         dimension = "mode"
    //*    versionNameSuffix = "-minApi24"*//*


     }


 }

xml 文件里 写法 <meta-data android:name="app_name" android:value="${app_name}" />

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:targetApi="31">

    <meta-data android:name="app_name" android:value="${app_name}" />

    <activity

        android:name=".MainActivity"
        android:exported="true"
        android:label="@string/app_name"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

java 代码 打印 com.asi.composingbuild D initView: 小米

var appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Log.d("oopp", "initView: "+appInfo?.metaData?.getString("app_name"))

也可以替换xml label android:label="${app_name}" 也可以在activity 里添加lable

image.png

image.png