启用Proguard后,“应用程序停止工作”

14

这是一个工作中的应用程序,可以在模拟器和测试设备上运行。

但是现在,我启用了Proguard并使用Android-Tools > 导出已签名应用程序导出了签名的应用程序。

然后,我将此.apk复制到SD卡上,并尝试在设备上安装。但是该应用程序停止工作,如果我尝试打开它,则会要求强制关闭。

如果从Eclipse运行,则它在设备上正常工作。

因为说:

只有在导出发布的.apk文件时,Proguard才会生效。我检查了一下,在bin文件夹中的apk文件大小为1.8mb,而导出后得到的是1.4mb。看起来Proguard已经完成了它的工作。但是应用程序无法工作。可悲的是我无法看到任何日志等...

编辑:

如果我禁用Proguard,然后导出,发布的apk文件现在可以正常工作。所以,Proguard存在问题吗?

我的文件夹结构

enter image description here

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


-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 *** v(...);
    public static *** i(...);
    public static *** d(...);
    public static *** w(...);
    public static *** e(...);
}

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames 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错误:
01-02 13:18:54.711: E/AndroidRuntime(585): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xApp.app/com.xApp.app.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.xApp.app.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

Thank You


1
是的,我已经尝试了3-4次,但都没有成功。实际上,禁用Proguard后问题就解决了。因此,我认为问题出在Proguard上,而不是导出方面。 - Archie.bpgc
与大多数未经解释的崩溃/强制关闭一样,除非您发布崩溃时的logcat错误,否则其他人无法帮助您。这将提供关键线索,以确定在proguard操作中出现了什么问题,因此需要更改proguard配置。 - Chris Stratton
1
@ChrisStratton 但是我该如何获取日志呢?我应该从配置文件中删除与日志相关的代码,然后在模拟器上安装apk吗? - Archie.bpgc
我在你的文件中没有看到任何“-libraryjars”条目,我记得我也需要插入它们,尝试像这样添加它们“-libraryjars libs/android-support-v4.jar”。 - Tomer Mor
我不确定,所以请先尝试参考文献。 - Tomer Mor
显示剩余7条评论
1个回答

31

堆栈跟踪显示ActionBarSherlock在使用反射时找不到构造函数"a(Activity,int)"。其文档确实指定了每个ActionBarSherlock扩展都应该具有此构造函数。由于该构造函数在代码中似乎未使用,ProGuard无法知道这一点并已将其删除。因此,您需要明确告诉ProGuard保留它:

-keepclassmembers class * extends com.actionbarsherlock.ActionBarSherlock {
    <init>(android.app.Activity, int);
}

一般来说,如果您在代码或其库中遇到与反射相关的堆栈跟踪,您需要添加相应的配置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接