【问题标题】:Logs are not removed even after using proguard即使在使用 proguard 后也不会删除日志
【发布时间】:2013-01-02 12:41:52
【问题描述】:

这是我的 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.myApp.mobileApp.**
-keep public class com.myApp.mobileApp.** {
  *;
}

-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.actionbarsherlock.app.SherlockActivity{*;}
-keep public class * extends com.actionbarsherlock.app.SherlockFragmentActivity{*;}
-keep public class * extends com.actionbarsherlock.app.SherlockMapActivity{*;}
-keep public class * extends com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay<OverlayItem>{*;}
-keep public class * extends com.actionbarsherlock.app.SherlockFragment{*;}

-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 {
    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(...);
}

# 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.**

我的想法是删除所有日志,即使混淆或任何其他优化不起作用。

bin 文件夹中的.apk 文件大小为 1.82mb,而我在 Expoting 后得到的文件大小为 1.42mb。所以,我认为proguard完成了它的工作。但是当我在模拟器上安装.apk文件并测试时,所有的日志都还在。

我做错了吗?

PS:应用程序崩溃说:

java.lang.RuntimeException: java.lang.NoSuchMethodException: a(Activity,int)

因此我在 proguard-project.txt 中添加了这两行:

  • -保持公共类 com.myApp.mobileApp.**
  • -保持公共类 com.myApp.mobileApp.** { *; }

现在它可以工作了,但日志仍然显示在 Logcat 中。

完成错误日志:

01-02 13:18:54.711: E/AndroidRuntime(585): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myApp.mobileApp/com.myApp.mobileApp.LandingActivity}: java.lang.RuntimeException: java.lang.NoSuchMethodException: a(Activity,int)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.os.Looper.loop(Looper.java:130)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-02 13:18:54.711: E/AndroidRuntime(585):  at java.lang.reflect.Method.invokeNative(Native Method)
01-02 13:18:54.711: E/AndroidRuntime(585):  at java.lang.reflect.Method.invoke(Method.java:507)
01-02 13:18:54.711: E/AndroidRuntime(585):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-02 13:18:54.711: E/AndroidRuntime(585):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-02 13:18:54.711: E/AndroidRuntime(585):  at dalvik.system.NativeStart.main(Native Method)
01-02 13:18:54.711: E/AndroidRuntime(585): Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodException: a(Activity,int)
01-02 13:18:54.711: E/AndroidRuntime(585):  at com.actionbarsherlock.a.a(Unknown Source)
01-02 13:18:54.711: E/AndroidRuntime(585):  at com.actionbarsherlock.app.SherlockActivity.a(Unknown Source)
01-02 13:18:54.711: E/AndroidRuntime(585):  at com.actionbarsherlock.app.SherlockActivity.setContentView(Unknown Source)
01-02 13:18:54.711: E/AndroidRuntime(585):  at com.myApp.mobileApp.LandingActivity.onCreate(Unknown Source)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-02 13:18:54.711: E/AndroidRuntime(585):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-02 13:18:54.711: E/AndroidRuntime(585):  ... 11 more
01-02 13:18:54.711: E/AndroidRuntime(585): Caused by: java.lang.NoSuchMethodException: a(Activity,int)
01-02 13:18:54.711: E/AndroidRuntime(585):  at java.lang.Class.getMatchingConstructor(Class.java:643)
01-02 13:18:54.711: E/AndroidRuntime(585):  at java.lang.Class.getConstructor(Class.java:472)
01-02 13:18:54.711: E/AndroidRuntime(585):  ... 17 more

谢谢

【问题讨论】:

  • 提供完整的堆栈跟踪以查看调用方法 a 的位置
  • 是的,我添加了完整的日志错误

标签: android proguard


【解决方案1】:

您只保留 SherlockActivity 类,而不保留其内部类。

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

来自https://stackoverflow.com/a/11151367/693752

【讨论】:

  • 那么,除了这 4 行之外,不需要任何与 sherlock 相关的内容吗?并删除我为避免应用崩溃而添加的 2 行?
  • 好的,我正在尝试。但是为什么 apk 文件缩小了我的 400kb。如果它没有混淆或删除任何日志,它应该与 bin 文件夹中的大小相同,对吗?因为我尝试禁用 proguard 然后导出,所以我得到了与 bin 中相同的大小。知道它跳过了什么吗?
  • 不知道,也不是很懂。但如果问题已解决,请考虑接受答案并提出新问题。
  • 我做了第 1 条评论中提到的更改:删除了我用来避免崩溃的 2 行,并删除了所有与 sherlock 相关的行,并添加了您发布的 4 行。该应用程序没有崩溃,非常感谢,但日志仍然存在:(
  • 将日志语句替换为 -assumenosideeffects class android.util.Log { public * ; }。来自stackoverflow.com/questions/6408574/…
【解决方案2】:

你使用过:-dontoptimize

所以 proguard 不会优化代码,所以 log.x 不会被删除(优化)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    相关资源
    最近更新 更多