开发者

Filter strings.xml with gradle-android-plugin?

开发者 https://www.devze.com 2023-03-08 02:21 出处:网络
How would I filter my res/values/strings.xml file? Something like: <resources> <string name=\"version_name\">${version}<开发者_如何转开发/string>

How would I filter my res/values/strings.xml file?

Something like:

<resources>
    <string name="version_name">${version}<开发者_如何转开发/string>
...

The ${version} would be expanded to the value of version from the build.gradle file


You can use ReplaceTokens with filter method if you have your token in the following format: @token@

So if your file looks like this:

<resources>
   <string name="version_name">@version@</string>
...

then you can do:

import org.apache.tools.ant.filters.ReplaceTokens

def version = '1.0'

processResources{
 filter(ReplaceTokens, tokens: ['version': version])
}

You can do this for any task that supports filter methods (which looks like any task that extends AbstractCopyTask).

I am not sure how you do it if you want to keep the ${token} format. You might be able to use a different filter or set the filter on ReplaceTokens. Looks like in ant you could set the begin and end tokens.


With groovy closure, if you prefer without ant external:

filter {
    line -> line.replace('${version}', version)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号