如果您需要将变量插入在 build.gradle
文件中定义的 AndroidManifest.xml
文件,可以使用 manifestPlaceholders
属性执行此操作。此属性采用键值对的映射,如下所示:
android {
defaultConfig {
manifestPlaceholders = [hostName:"www.example.com"]
}
...
}
然后,您可以将某个占位符作为属性值插入清单文件,如下所示:
<intent-filter ... >
<data android:scheme="https" android:host="${hostName}" ... />
...
</intent-filter>
默认情况下,构建工具还会在 ${applicationId}
占位符中提供应用的应用 ID。该值始终与当前构建的最终应用 ID(包括构建变体的应用 ID 更改)一致。当您要对标识符(如 intent 操作)使用唯一的命名空间时,这很有用,即使要求在构建变体之间保持唯一性,这也很有用。
例如,如果您的 build.gradle
文件如下所示:
android {
defaultConfig {
applicationId "com.example.myapp"
}
productFlavors {
free {
applicationIdSuffix ".free"
}
pro {
applicationIdSuffix ".pro"
}
}
}
那么,您可以按如下方式将应用 ID 插入清单:
<intent-filter ... >
<action android:name="${applicationId}.TRANSMOGRIFY" />
...
</intent-filter>
当您构建“free”产品变种时,清单结果如下所示:
<intent-filter ... >
<action android:name="com.example.myapp.free.TRANSMOGRIFY" />
...
</intent-filter>
如需了解详情,请阅读设置应用 ID。