1、调试gradle脚本
可以使用println(“hello world”)来打印变量值
参考文档(gradle断点,我没尝试过):
https://stackoverflow.com/questions/28437636/how-to-debug-a-gradle-build-gradle-file-in-a-debugger-with-breakpoints
2、根据Debug、release等信息来设置差别的变量/参数
- android.applicationVariants.all { variant -> def buildTypeName = variant.buildType.name def flavorName = variant.flavorName String time = null; if ("debug".equalsIgnoreCase(buildTypeName)) { time = buildTime(date, timeForDebugFileFormater) } else { time = buildTime(date, timeForReleaseFileFormater) } variant.outputs.each { output -> def outputFile = output.outputFile def fileName = "XXX_v${defaultConfig.versionName}_${flavorName}_${buildTypeName}_${time}.apk" output.outputFile = new File(outputFile.parent, fileName) }}
复制代码
这里需要注意的是:
1、以上脚本只适用于app module,即com.android.application,如果是com.android.library的话,需要改成如下:
- android.libraryVariants.all { variant ->....}
复制代码 2、这段代码不会立刻执行。看下面的例子
- def version = "1.0"android.applicationVariants.all { variant -> def buildTypeName = variant.buildType.name if ("debug".equalsIgnoreCase(buildTypeName)) { println(“debug。。。“) ` version = "1.0-debug" } else { println(“release。。。”) ` version = "1.0-release" }println("version:"+version)
复制代码 最终输出的是:
version:1.0
debug
release
也就是说想通过这个方法来改变,顺序执行过程中的gradle变量是不可行的。改变在android编译构建时的变量是OK的
参考资料
https://blog.csdn.net/qq_35780104/article/details/107698388?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.not_use_machine_learn_pai&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.not_use_machine_learn_pai
https://blog.csdn.net/bunnycoffer/article/details/78852658
3、gradle读取情况变量
场景:在工作中需要使用流水线编译时,需要从外部设置情况变量来达到控制台根据差别的用户输入来编译差别的版本(差别于productFlavor)
- String getEnvValue(String key,String def){ def val = System.getenv(key) if(null != val){ return val } return def}android.defaultConfig.versionName = getEnvValue("VERSION_NAME","1.2")android.defaultConfig.versionCode = getEnvValue("VERSION_CODE","1.2").toInteger()
复制代码 以上代码可以通过外部变量来覆写build.gradle中的版本号。
来源:https://blog.csdn.net/wutianyin222/article/details/112037031
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |