如何替换字符串资源与Android摇篮 [英] How to replace strings resources with Android Gradle

查看:185
本文介绍了如何替换字符串资源与Android摇篮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了摇篮中的Andr​​oid Studio中的一个新的应用程序,现在我需要做大约10个版本,在不同的资源包名称和值。我做了定制的口味作为例子,并希望更换一些字符串在这个定制的口味自定义值。我发现这样的例子:

 过滤器(org.apache.tools.ant.filters.ReplaceTokens,代币:['版本':'2.2'])
 

但我不知道在哪里把它。据我了解,我需要把它放到单独的任务,但如何让这项任务称为IDE?

此外,我需要更换Java类和内容提供商的身份验证里面几个变量,也许我需要复制的文件要做到这一点到flavor1文件夹,让摇篮合并,但它似乎是错误的解决方案来存储文件的多个副本与在一行中的差异。也许我需要用户的一些其他的解决方案,这一切?

下面是build.gradle:

  buildscript {
    库{
        mavenCentral()
    }
    依赖{
        类路径com.android.tools.build:gradle:0.4.2
    }
}
应用插件:'机器人'

依赖{
    编译文件树(导演:库,包括:的* .jar)
    编制项目(:JazzyListView)
    编制项目(:ABS)
    编制项目(:排)
}

安卓{
    compileSdkVersion 17
    buildToolsVersion17.0.0

    defaultConfig {
        版本code 5
        VERSIONNAME3.​​0
        的minSdkVersion 8
        targetSdkVersion 17
    }

    sourceSets {
        主要 {
            manifest.srcFile钢骨混凝土/主/ AndroidManifest.xml中
            java.srcDirs = ['的src / main / java目录]
            res.srcDirs = ['钢骨混凝土/主/ RES']
        }
    }

    productFlavors {
        flavor1 {
            软件包名com.example.flavor1
        }

        flavor2 {
            软件包名com.example.flavor2
        }

    }

}
 

解决方案

我也有类似的问题。我想詹金斯版本号添加到从strings.xml中得到合并的字符串。下面是我的解决方案,作为Android的摇篮插件0.12,+。

  //将版本号为的strings.xml
android.applicationVariants.all {变种 - >
    variant.mergeResources.doLast {
        ext.env = System.getenv()
        高清buildNumber = env.BUILD_NUMBER
        如果(buildNumber!= NULL){
            文件values​​File =文件($ {buildDir} /中间体/ RES / $ {variant.dirName} /values​​/values​​.xml)
            的println(更换版本号中的+ values​​File)
            的println(构建数=+ buildNumber)
            字符串的内容= values​​File.getText(UTF-8)
            内容= content.replaceAll(/ devBuild /,buildNumber)
            values​​File.write(内容,UTF-8)
        }
    }
}
 

您可能要挂接到一个不同的摇篮任务,取决于你想要做什么。看看那些在Android构建的一部分任务,以弄清这一点。

<一个href="http://tools.android.com/tech-docs/new-build-system/user-guide">http://tools.android.com/tech-docs/new-build-system/user-guide

更新:在某些时候,Android的摇篮插件改为遍历应用程序的方式,从的每个的到的所有的变种关键字。我的回答已经更新以反映更改,但尝试切换到的每个的,如果这code不打印任何东西到控制台。

I made a new app with gradle in Android Studio, and now I need to make about 10 versions with different package names and values in resources. I made custom flavors as in example and want to replace some strings in this custom flavors with custom values. I found example like this:

filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: ['version': '2.2'])

But i don't know where to put it. As i understand i need to put it into separate task, but how to make this task called by IDE?

Also i need to replace few variables inside Java classes and Content Provider's auth, maybe i need to do this by copy files into flavor1 folder and let gradle to merge it, but it seems like wrong solution to store many copies of files with difference in one line... Maybe i need to user some other solution for all this?

Here is build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':JazzyListView')
    compile project(':ABS')
    compile project(':Volley')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        versionCode 5
        versionName "3.0"
        minSdkVersion 8
        targetSdkVersion 17
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['src/main/res']
        }
    }

    productFlavors {
        flavor1 {
            packageName "com.example.flavor1"               
        }

        flavor2 {
            packageName "com.example.flavor2"
        }

    }

}

解决方案

I had a similar problem. I wanted to add the Jenkins build number to the strings that get merged from strings.xml. Here's my solution as of Android Gradle plugin 0.12.+.

// Insert the build number into strings.xml
android.applicationVariants.all{ variant ->
    variant.mergeResources.doLast{
        ext.env = System.getenv()
        def buildNumber = env.BUILD_NUMBER
        if (buildNumber != null) {
            File valuesFile = file("${buildDir}/intermediates/res/${variant.dirName}/values/values.xml")
            println("Replacing revision number in " + valuesFile)
            println("Build number = " + buildNumber)
            String content = valuesFile.getText('UTF-8')
            content = content.replaceAll(/devBuild/, buildNumber)
            valuesFile.write(content, 'UTF-8')
        }
    }
}

You might want to hook into a different Gradle task depending on what you want to do. Take a look at the tasks that are part of the Android build to figure that out.

http://tools.android.com/tech-docs/new-build-system/user-guide

UPDATE: At some point, the Android Gradle plugin changed the way to iterate through application variants keyword from each to all. My answer has been updated to reflect the change, but try switching to each if this code doesn't print anything to the console.

这篇关于如何替换字符串资源与Android摇篮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆