【问题标题】:How to config my proguard-project.txt file to remove just Logs如何配置我的 proguard-project.txt 文件以仅删除日志
【发布时间】:2013-01-02 14:33:48
【问题描述】:

这是我现在在 proguard-project.txt 中使用的代码

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*

-keep public class com.google.vending.licensing.ILicensingService{*;}
-keep public class com.android.vending.licensing.ILicensingService{*;}
-keep public class * extends android.app.Application{*;}
-keep public class * extends android.app.Activity{*;}
-keep public class * extends android.app.MapActivity{*;}
-keep public class * extends android.app.PreferenceActivity{*;}
-keep public class * extends android.view.View{*;}
-keep public class * extends android.widget.BaseAdapter{*;}
-keep public class * extends android.app.Service{*;}
-keep public class * extends android.content.BroadcastReceiver{*;}
-keep public class * implements android.view.View.OnTouchListener{*;}
-keep public class * implements android.view.View.OnClickListener{*;}
-keep public class * extends com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay<OverlayItem>{*;}

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }

-libraryjars libs/android-support-v4.jar
-libraryjars libs/apache-mime4j-0.6.jar
-libraryjars libs/httpmime-4.0.1.jar
-libraryjars libs/libGoogleAnalyticsV2.jar

-assumenosideeffects class android.util.Log {*;}

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembers class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**
-dontwarn org.apache.**

但是日志仍然出现在 logcat 中。

所以,我只想知道代码必须是什么,以便只删除日志。不需要其他优化。

谢谢

【问题讨论】:

    标签: android proguard


    【解决方案1】:

    ProGuard 配置文件分为几个部分(从 Android SDK r20 开始),在 project.properties 中指定:

      proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
    

    如果没有禁用优化,则只能删除日志记录,这需要不同的全局配置文件:

      proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt
    

    文件 proguard-project.txt 只需要包含项目特定的配置。您的文件似乎包含太多内容,但这些是删除日志记录的设置:

    -assumenosideeffects class android.util.Log {
        public static boolean isLoggable(java.lang.String, int);
        public static int v(...);
        public static int i(...);
        public static int w(...);
        public static int d(...);
        public static int e(...);
    }
    

    类似的问题和答案:

    【讨论】:

    • 那么,从链接中您的意思是说 Proguard 不适用于 Log.d("answer = " + answer, "something = " + something);? ?
    • 另外,proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt 在我的 project.properties并且我的 proguard-project.txt 中的 删除日志代码 会起作用吗?
    • @Snicolas 同意,但是 proguard 对我来说是全新的,我没有时间彻底研究它:(。无论如何,非常感谢,它现在工作正常。希望它不会给以后有什么问题。
    • 有没有办法从原生库中删除日志记录?
    • @Giuseppe ProGuard 只处理字节码,不处理本机代码。对于原生代码,您可以在源代码中使用传统的预处理指令和宏定义。
    【解决方案2】:

    如果您在 Android Studio 上遇到此问题,则必须将 Eric 的解决方案应用于您的 build.grade 文件(在应用级别)。替换:

    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    

    与:

    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    

    【讨论】:

    • 是否可以仅通过混淆而不是优化来删除日志记录?
    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多