前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android Gradle 插件】ProductFlavor 配置 ( ProductFlavor#manifestPlaceholders 清单文件占位符配置 )

【Android Gradle 插件】ProductFlavor 配置 ( ProductFlavor#manifestPlaceholders 清单文件占位符配置 )

作者头像
韩曙亮
发布2023-03-30 13:15:41
8000
发布2023-03-30 13:15:41
举报

文章目录

Android Plugin DSL Reference 参考文档 :

一、ProductFlavor#manifestPlaceholders 清单文件占位符配置


ProductFlavor 参考文档 : com.android.build.gradle.internal.dsl.ProductFlavor.html

ProductFlavor#manifestPlaceholders 配置 , 用于配置 manifest 的占位符 , 该配置项是 Map<String, Object> 类型的 ;

在这里插入图片描述
在这里插入图片描述

在 build.gradle 的 ProductFlavor defaultConfig 配置项中 , 设置清单文件占位符属性 , 为其设置一个 Map<String, Object> 类型的属性 ;

代码示例 :

代码语言:javascript
复制
android {
    defaultConfig {
        manifestPlaceholders = [name: 'Tom']
    }

完整代码 :

代码语言:javascript
复制
plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 31
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.ad_id_test"
        minSdkVersion 18
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        manifestPlaceholders = [name: 'Tom']
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }


}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

清单文件配置 : 在下面的 meta-data 标签中 , 使用了 {name} 引用了 build.gradle 中定义的 manifestPlaceholders = [name: 'Tom'] , 在合并清单文件时 , 会自动使用 Tom 替换上述 {name} 引用 ;

代码语言:javascript
复制
<meta-data android:name="student" android:value="${name}" />

完整 AndroidManifest.xml 清单文件配置 :

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ad_id_test">

    <uses-permission android:name="com.google.android.gms.permission.AD_ID"/>

    <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/Theme.AD_ID_Test">

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

        <activity android:name=".MainActivity"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

将 AndroidManifest.xml 清单文件 切换到 Merged Manifest 模式 , 可以看到合并后的 清单文件 , 其中 <meta-data> 标签 , 由

代码语言:javascript
复制
<meta-data 
	android:name="student" 
	android:value="${name}" />

变成了

代码语言:javascript
复制
<meta-data 
	android:name="student" 
	android:value="Tom" />

样式 , ${name}Tom 替换了 ;

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、ProductFlavor#manifestPlaceholders 清单文件占位符配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档