就在几周前,我遇到了以下问题:How to replace a string for a buildvariant with gradle?
我也自己回答了这个问题。
到目前为止一切正常:我刚刚意识到我的复制任务不再有用了。我花了几个小时来解决这个问题,直到我意识到这取决于Gradle Android插件版本:直到0.5.4的一切正常。对于较高版本,我不会进入我的复制任务。
这是控制台输出:
// gradle android plugin version: 0.5.6 and 0.5.5 --> copy tas doesn't work
:etscanner:prepareFlavor1Flavor1ReviewDependencies
:etscanner:compileFlavor1Flavor1ReviewAidl
:etscanner:generateFlavor1Flavor1ReviewBuildConfig
:etscanner:mergeFlavor1Flavor1ReviewAssets
:etscanner:compileFlavor1Flavor1ReviewRenderscript
:etscanner:mergeFlavor1Flavor1ReviewResources
:etscanner:processFlavor1Flavor1ReviewManifest
:etscanner:processFlavor1Flavor1ReviewResources
:etscanner:compileFlavor1Flavor1ReviewNote: Some input files use or override a d
eprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: <path>.DetailAdapter
.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:etscanner:dexFlavor1Flavor1Review
:etscanner:processFlavor1Flavor1ReviewJavaRes UP-TO-DATE
:etscanner:validateFlavor1Flavor1Signing
:etscanner:packageFlavor1Flavor1Review
:etscanner:zipalignFlavor1Flavor1Review
// gradle android plugin version: 0.5.4 --> copy task work
:etscanner:prepareFlavor1Flavor1ReviewDependencies
:etscanner:compileFlavor1Flavor1ReviewAidl
:etscanner:generateFlavor1Flavor1ReviewBuildConfig
:etscanner:mergeFlavor1Flavor1ReviewAssets
:etscanner:compileFlavor1Flavor1ReviewRenderscript
:etscanner:mergeFlavor1Flavor1ReviewResources
:etscanner:processFlavor1Flavor1ReviewManifest
...hey you are in the copy task!
:etscanner:processFlavor1Flavor1ReviewResources
:etscanner:compileFlavor1Flavor1ReviewNote: Some input files use or override a d
eprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: <path>DetailAdapter
.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:etscanner:dexFlavor1Flavor1Review
:etscanner:processFlavor1Flavor1ReviewJavaRes UP-TO-DATE
:etscanner:validateFlavor1Flavor1Signing
:etscanner:packageFlavor1Flavor1Review
:etscanner:zipalignFlavor1Flavor1Review
:etscanner:assembleFlavor1Flavor1Review
这真是一件奇怪的事...... 有谁知道如何解决这个问题?
更新1 2013-08-23
我的build.gradle文件:
buildscript {
repositories {
// maven central repo doesn't work with gradle android plugin version 0.5.+
// error message is describe in this post:
// https://plus.google.com/117954254390243608387/posts/RVBjoDMkLV5
//mavenCentral()
maven {
url 'http://nexus/content/groups/public/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.4'
// copy task doesn't work for following versions:
//classpath 'com.android.tools.build:gradle:0.5.5'
//classpath 'com.android.tools.build:gradle:0.5.6'
//classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.android.support:support-v4:13.0.+' // support lib
//compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile project(':libraries:actionbarsherlock')
compile project(':libraries:google-play-services_lib')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 17
versionName = "2.3"
versionCode = 4
}
// SIGN CONFIGS
signingConfigs {
flavor1 {
storeFile file("keystore/myKeystore.keystore")
storePassword = "store_password"
keyAlias = "alias"
keyPassword = "key_password"
}
flavor2 {
storeFile file("keystore/myKeystore.keystore")
storePassword = "store_password"
keyAlias = "alias"
keyPassword = "key_password"
}
debug {
storeFile file("keystore/debug.keystore")
storePassword = "android"
keyAlias = "androiddebugkey"
keyPassword = "android"
}
}
// FLAVORS
productFlavors {
flavor1 {
packageName 'myPackageName'
signingConfig signingConfigs.flavor1
}
flavor2 {
packageName 'myPackageName'
signingConfig signingConfigs.flavor2
}
}
// BUILDTYPES
buildTypes {
falvor1Review {
versionNameSuffix = versionNameSuffixOfReviewVersion
signingConfig signingConfigs.flavor1
}
flavor2Review {
versionNameSuffix = versionNameSuffixOfReviewVersion
signingConfig signingConfigs.flavor2
}
debug {
packageNameSuffix ".debug"
versionNameSuffix = versionNameSuffixOfReviewVersion
signingConfig signingConfigs.debug
}
}
// Override Data in Manifest
android.applicationVariants.each { variant ->
variant.processManifest.doLast {
copy {
// *** SET COPY PATHS ***
try {
from("${buildDir}/manifests") {
//println "from: ${buildDir}/manifests"
include "${variant.dirName}/AndroidManifest.xml"
//println "included: ${variant.dirName}/AndroidManifest.xml"
}
} catch (e) {
println "error: " + e
}
into("${buildDir}/manifests/${variant.name}")
//println "into (neues Manifest): ${buildDir}/manifests/${variant.name}"
// *** DEFINE VARS ***
def brandVersion = variant.buildType.name
def brandVersionString = brandVersion.toString()
def appName = "empty"
// *** SET APP NAME ***
if (brandVersionString.contains("flavor1")) {
appName = "my app name for flavor 1"
} else if (brandVersionString.contains("flavor2")) {
appName = "my app name for flavor 2"
}
println "...hey you are in the copy task"
// *** REPLACE LINES IN MANIFEST ***
// --- add appName
filter {
String line ->
line.replaceAll("<application android:allowBackup=\"true\" android:icon=\"@drawable/ic_launcher\" android:label=\"todo\" android:theme=\"@style/AppTheme\">",
"<application android:allowBackup=\"true\" android:icon=\"@drawable/ic_launcher\" android:label=\"" + appName + "\" android:theme=\"@style/AppTheme\">");
}
}
}
// *** SET PATH TO NEW MANIFEST ***
variant.processResources.manifestFile = file("${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml")
//println "newManifest: ${buildDir}/manifests/${variant.name}/${variant.dirName}/AndroidManifest.xml"
}
}
更新2 2013-08-23
昨天我有一个AS 0.2.5的另一个奇怪的行为,它做了一些非常奇怪的构建: 正如您在我的过滤器中看到的,我在Manifest中的先前appName是“todo”:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="todo"
android:theme="@style/AppTheme">
当我进行构建时,应用程序中的appName是正确的。但是在Application Launcher和Settings / Apps中,“todo”显示为appName。
在AS 0.2.0中构建项目后,一切正常。
答案 0 :(得分:6)
我遇到了和你一样的问题,并通过0.5.5的发行说明阅读了答案。
“访问变体容器不会强制创建任务。
这意味着在评估阶段android.[application|Library|Test]Variants
将为空。要使用它,请使用.all
代替.each
“
答案 1 :(得分:2)
如果您想尝试Alécio的解决方案并在较新版本的gradle上发布弃用警告,例如
警告不推荐使用[Project :::] variant.getProcessManifest()。呼叫 它取决于variant.getOutputs()中的一个。
您需要使用'variant.outputs.each {output - &gt;'迭代每个输出,请参阅下面的代码
applicationVariants.all { variant ->
def flavor = variant.productFlavors[0].name
def flavorPackageName = variant.productFlavors[0].applicationId
variant.outputs.each { output ->
output.processManifest.doLast {
println("configuring AndroidManifest.xml for " + flavor);
copy {
from("${buildDir}/intermediates/manifests") {
include "${variant.dirName}/AndroidManifest.xml"
}
into("${buildDir}/intermediates/filtered_manifests")
}
def manifestFile = new File("${buildDir}/intermediates/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
def content = manifestFile.getText()
def updatedContent = content.replaceAll("STRING_TO_BE_REPLACED", "NEW_STRING")
manifestFile.write(updatedContent)
}
output.processResources.manifestFile = new File("${buildDir}/intermediates/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
}
}
答案 2 :(得分:0)
以下代码可以放在您的gradle构建中的 android 块中。
android {
...
buildTypes {
...
applicationVariants.all { variant ->
def flavor = variant.productFlavors[0].name
def flavorPackageName = variant.productFlavors[0].applicationId
variant.processManifest.doLast {
println("configuring AndroidManifest.xml for " + flavor);
copy {
from("${buildDir}/intermediates/manifests") {
include "${variant.dirName}/AndroidManifest.xml"
}
into("${buildDir}/intermediates/filtered_manifests")
}
def manifestFile = new File("${buildDir}/intermediates/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
def content = manifestFile.getText()
def updatedContent = content.replaceAll("STRING_TO_BE_REPLACED", "NEW_STRING")
manifestFile.write(updatedContent)
}
variant.processResources.manifestFile = new File("${buildDir}/intermediates/filtered_manifests/${variant.dirName}/AndroidManifest.xml")
}
}