我使用的摇篮与产品的口味,其中i为每一个设置不同的包名。 productFlavors {
appone {
软件包名com.dg.app1
}
apptwo {
软件包名com.dg.app2
}
appthree {
软件包名com.dg.app3
}
appfour {
软件包名com.dg.app4
}
}
我需要能够更换包名内的清单进行各对应的应用程序
我的表现有这样的:
<接收器的Android版本:NAME =com.parse.GcmBroadcastReceiver
机器人:权限=com.google.android.c2dm.permission.SEND>
<意向滤光器>
<作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
<作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>
<类机器人:名称=com.dg.example/>
&所述; /意图滤光器>
< /接收器>
所以,我需要更换com.dg.example为每个应用程序风味的包名。什么是做到这一点的最好方法是什么?
解决方案摇篮插件v0.12和更高:
使用 $ {的applicationID}
而不是 $ {的packageName}
。
摇篮插件v0.11和更高:
由于v0.11中,你不再需要指定不使用旧清单合并。
摇篮插件v0.10和更高:
假设你正在使用0.10或更高版本,这是目前正式支持:
buildscript {
库{
mavenCentral()
}
依赖{
//确保这至少是0.10。+
类路径com.android.tools.build:gradle:0.10.+
}
}
由于v0.10的,你还必须手动启用新的清单合并,但我预计这一要求走开的版本或两个每当新的并购将成为默认的:
安卓{
useOldManifestMerger假
}
然后,只需使用 $ {的packageName}
在的AndroidManifest.xml
的任何地方,你通常会很难code中的包名。例如:
<类机器人:名称=my.package.name/>
将成为
<类别的Android版本:NAME =$ {}的packageName/>
摇篮插件V0.9及以下:
所以,参考这个帖子,看来这还不是正式通过支持摇篮。简单的解决方法如下:
在使用自定义标签替换包的名称(例如:<类机器人:名称=my.package.name/>
变成 <类机器人:名称=_ PACKAGENAME _/>
在下面添加到您的 build.gradle
的 Android的下
范围:
applicationVariants.all {变种 - >
//处理清单后,更换你的标记的所有实例
//与变异的实际包名称。
variant.processManifest<< {
高清manifestOutFile = variant.processManifest.manifestOutputFile
高清newFileContents = manifestOutFile.getText(UTF-8)。代替(_ PACKAGENAME_,variant.packageName)
manifestOutFile.write(newFileContents,UTF-8)
}
}
I am using gradle with Product flavors where i set a different package name for each one. productFlavors {
appone {
packageName "com.dg.app1"
}
apptwo {
packageName "com.dg.app2"
}
appthree {
packageName "com.dg.app3"
}
appfour {
packageName "com.dg.app4"
}
}
i need to be able to replace the package name inside the manifest for each corresponding app.
My manifest has this:
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.dg.example" />
</intent-filter>
</receiver>
So i need to replace com.dg.example for each app flavor's package name. What is the best way to do this?
解决方案Gradle Plugin v0.12 and higher:
Use ${applicationId}
instead of ${packageName}
.
Gradle Plugin v0.11 and higher:
As of v0.11, you no longer need to specify not to use the old manifest merger.
Gradle Plugin v0.10 and higher:
Assuming you're using version 0.10 or higher, this is now officially supported:
buildscript {
repositories {
mavenCentral()
}
dependencies {
// Make sure this is at least 0.10.+
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
As of v0.10, you'll also have to manually enable the new manifest merger, although I'd expect that requirement to go away in a version or two whenever the new merger becomes the default:
android {
useOldManifestMerger false
}
Then, just use ${packageName}
anywhere in AndroidManifest.xml
that you would normally hardcode the package name. For example:
<category android:name="my.package.name"/>
would become
<category android:name="${packageName}"/>
Gradle Plugin v0.9 and below:
So, referencing this post, it appears this is not yet officially supported through Gradle. A simple workaround is the following:
Replace the package name with a custom tag (e.g.<category android:name="my.package.name"/>
becomes <category android:name="_PACKAGENAME_"/>
Add the following to your build.gradle
, under the android
scope:
applicationVariants.all { variant ->
// After processing the manifest, replace all instances of your tag
// with the variant's actual package name.
variant.processManifest << {
def manifestOutFile = variant.processManifest.manifestOutputFile
def newFileContents = manifestOutFile.getText('UTF-8').replace("_PACKAGENAME_", variant.packageName)
manifestOutFile.write(newFileContents, 'UTF-8')
}
}