How to remove all debug logging calls before building the release version of an Android app?

前端 未结 27 1542
有刺的猬
有刺的猬 2020-11-22 07:39

According to Google, I must \"deactivate any calls to Log methods in the source code\" before publishing my Android app to Google Play. Extract from section 3 of th

27条回答
  •  攒了一身酷
    2020-11-22 08:19

    If you can run a global replace (once), and after that preserve some coding convention, you can follow the pattern often used in Android framework.

    Instead of writing

    Log.d(TAG, string1 + string2 + arg3.toString());
    

    have it as

    if (BuildConfig.DEBUG) Log.d(TAG, string1 + String.format("%.2f", arg2) + arg3.toString());
    

    Now proguard can remove the StringBuilder and all strings and methods it uses on the way, from optimized release DEX. Use proguard-android-optimize.txt and you don't need to worry about android.util.Log in your proguard-rules.pro:

    android {
      …
      buildTypes {
        release {
          minifyEnabled true
          proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
      }
    }
    

    With Android Studio gradle plugin, BuildConfig.DEBUG is quite reliable, so you don't need extra constants to control the stripping.

提交回复
热议问题