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

前端 未结 27 1814
有刺的猬
有刺的猬 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:16

    I would like to add some precisions about using Proguard with Android Studio and gradle, since I had lots of problems to remove log lines from the final binary.

    In order to make assumenosideeffects in Proguard works, there is a prerequisite.

    In your gradle file, you have to specify the usage of the proguard-android-optimize.txt as default file.

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    
            // With the file below, it does not work!
            //proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    

    Actually, in the default proguard-android.txt file, optimization is disabled with the two flags:

    -dontoptimize
    -dontpreverify
    

    The proguard-android-optimize.txt file does not add those lines, so now assumenosideeffects can work.

    Then, personnally, I use SLF4J, all the more when I develop some libraries that are distributed to others. The advantage is that by default there is no output. And if the integrator wants some log outputs, he can uses Logback for Android and activate the logs, so logs can be redirected to a file or to LogCat.

    If I really need to strip the logs from the final library, I then add to my Proguard file (after having enabled the proguard-android-optimize.txt file of course):

    -assumenosideeffects class * implements org.slf4j.Logger {
        public *** trace(...);
        public *** debug(...);
        public *** info(...);
        public *** warn(...);
        public *** error(...);
    }
    

提交回复
热议问题