简体   繁体   English

Android Studio构建gradle

[英]Android Studio build gradle

I want to replace a string token(version) in a html file from assets folder. 我想替换资产文件夹中html文件中的字符串标记(版本)。 My question is how I can accomplish this task with build gradle? 我的问题是如何通过build gradle完成此任务?

Thank you. 谢谢。

THe first thing you need is to know how to replace your token with the desired value with command line (with sed or anything else you like). 您需要做的第一件事就是知道如何用命令行(用sed或其他您喜欢的东西)将令牌替换为所需的值。

Then you can add task in your gradle build logic : 然后,您可以在gradle构建逻辑中添加任务:

task replaceToken {
    commandline "your command line"
}

Then you add dependance on this task : 然后添加对此任务的依赖:

tasks.withType(JavaCompile) {
    compileTask -> compileTask.depensOn replaceToken
}

In this scenario, after each build your asset file will have the version name in it. 在这种情况下,每次构建后,您的资产文件将具有版本名称。 So your command line should be able to insert the token value even if there is already a value (you will have to use regexp to replace <balise>whatever</balise> by <balise>token value</balise> . 因此,即使已有值,您的命令行也应该能够插入令牌值(您必须使用regexp将<balise>whatever</balise>替换为<balise>token value</balise>

I put this in build.gradle and works 我把它放在build.gradle中并工作

    applicationVariants.all{ variant ->
        variant.mergeResources.doFirst{
            def buildNumber = env.BUILD_NUMBER

            if (null != buildNumber) {
                File valuesFile = file("${buildDir}/intermediates/assets/${variant.dirName}/about.html")

                String content = valuesFile.getText('UTF-8')
                content = content.replaceAll('@VERSION@', buildNumber)
                valuesFile.write(content, 'UTF-8')
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM