在構建時刪除跟蹤日誌記錄(和其他)語句

如果要刪除對某些方法的呼叫,假設它們返回 void 並且沒有副作用(例如,呼叫它們不會更改任何系統值,引用引數,靜態等),那麼你可以讓 ProGuard 將它們從構建完成後的輸出。

例如,我發現這對於刪除在除錯中有用的除錯/詳細日誌記錄語句很有用,但在生產中不需要為它們生成字串。

# Remove the debug and verbose level Logging statements.
# That means the code to generate the arguments to these methods will also not be called.
# ONLY WORKS IF -dontoptimize IS _NOT_ USED in any ProGuard configs
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

注意:如果在任何 ProGuard 配置中使用 -dontoptimize,以便它不會縮小/刪除未使用的程式碼,那麼這將不會刪除語句。 (但誰不想刪除未使用的程式碼,對吧?)

注意 2:此呼叫將刪除對日誌的呼叫,但不會保護你的程式碼。字串實際上將保留在生成的 apk 中。詳情請閱讀這篇文章