gpt4 book ai didi

java - 从发布版本的 android 项目中删除代码行

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:55 26 4
gpt4 key购买 nike

我创建了一个 Logger 类,在其中放置了一个字符串和日志级别。该行看起来像这样:

  Logger.log("The error is" + err , Logger.DEBUG);

我想删除所有在发布版本中使用 Logger.DEBUG 日志级别的 Logger.log 消息。目前我还没有找到使用 gradle 执行此操作的技巧。我也尝试使用 proguard -assumenosideeffects 但我找不到为所有项目提交它的确切规则。混淆器说:

You can let ProGuard remove logging code. The trick is to specify that the logging methods don't have side-effects — even though they actually do, since they write to the console or to a log file. ProGuard will take your word for it and remove the invocations (in the optimization step) and if possible the logging classes and methods themselves (in the shrinking step).

最佳答案

我通过写一个gradle脚本解决了这个问题:

task deleteJavaDebugLogs << {
description "This function will delete all SDK DEBUG log level from the project, be careful with it!"
FileTree javaFiles = fileTree('src/main/java/com/"your-name"') {
include '**/*.java'
}
String regex = "Logger.log[^,]+[^L]+(.*)Logger.SDK_DEBUG[^;];"
javaFiles.each { File javaFile ->
println "Start replacing regex on $javaFile.name"
String content = javaFile.getText()
content = content.replaceAll(regex, "")
javaFile.setText(content)
}
}

每次发布​​前我都会运行这个 gradle 脚本。使用 Regex 它只会从项目中删除有问题的行。我建议使用 Jenkins 或其他构建工具将其添加到您的构建过程中

在没有备份的项目上运行它要小心,因为它会从你的代码中删除所有带有上述正则表达式的行。

关于java - 从发布版本的 android 项目中删除代码行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29327264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com