I just developed my application and want to release but before that i want to remove the logs and other unnecessary things. The best way i find that to use Proguard. I am using command line so i use the command ant release and my application is built successfully and the new apk file which is generated in /bin directory is <app-name>-release.apk. I installed that apk in my device but after few screens its crashed and when i tried with restating my application and moved to other tabs then its crashed at some points.
Don't know why its happened. so this is the contain of my project.properties file
target=android-15
proguard.config=proguard-project.txt
And this is my proguard-project.txt
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-libraryjars smack.jar
-libraryjars libphonenumber-5.0v1.5.jar
#-keep class com.** { *; }
#-keep class org.** { *; }
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
Error Log:
10-08 07:25:18.356: E/AndroidRuntime(1733): FATAL EXCEPTION: Contact_sync_Service
10-08 07:25:18.356: E/AndroidRuntime(1733): java.lang.NoClassDefFoundError: com.google.i18n.phonenumbers.PhoneNumberUtil
10-08 07:25:18.356: E/AndroidRuntime(1733): at org.sipchat.sipua.ui.ai.run(Unknown
Source)
10-08 07:25:18.356: E/AndroidRuntime(1733): at java.lang.Thread.run(Thread.java:856)
10-08 07:26:26.756: E/dalvikvm(1751): Could not find class 'org.jivesoftware.smack.filter.MessageTypeFilter', referenced from method org.sipchat.sipua.ui.ConnectionService.a
10-08 07:26:26.786: E/dalvikvm(1751): Could not find class 'org.jivesoftware.smack.ConnectionConfiguration', referenced from method org.sipchat.sipua.ui.ConnectionService.onStartCommand
10-08 07:26:26.806: E/dalvikvm(1751): Could not find class 'org.jivesoftware.smack.ConnectionConfiguration', referenced from method org.sipchat.sipua.ui.af.run
10-08 07:26:26.956: E/AndroidRuntime(1751): FATAL EXCEPTION: main
10-08 07:26:26.956: E/AndroidRuntime(1751): java.lang.NoClassDefFoundError: org.jivesoftware.smack.ConnectionConfiguration
10-08 07:26:26.956: E/AndroidRuntime(1751): at org.sipchat.sipua.ui.ConnectionService.onStartCommand(Unknown Source)
10-08 07:26:26.956: E/AndroidRuntime(1751): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
10-08 07:26:26.956: E/AndroidRuntime(1751): at android.app.ActivityThread.access$1900(ActivityThread.java:123)
10-08 07:26:26.956: E/AndroidRuntime(1751): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
10-08 07:26:26.956: E/AndroidRuntime(1751): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 07:26:26.956: E/AndroidRuntime(1751): at android.os.Looper.loop(Looper.java:137)
10-08 07:26:26.956: E/AndroidRuntime(1751): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-08 07:26:26.956: E/AndroidRuntime(1751): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 07:26:26.956: E/AndroidRuntime(1751): at java.lang.reflect.Method.invoke(Method.java:511)
10-08 07:26:26.956: E/AndroidRuntime(1751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-08 07:26:26.956: E/AndroidRuntime(1751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-08 07:26:26.956: E/AndroidRuntime(1751): at dalvik.system.NativeStart.main(Native Method)
Update :
The packages which are mentioned in error are the packages of jar files which i added already in proguard-project.txt
Please make me clear if i went wrong anywhere?Thanks
Did you gave Dexguard a try? As it says on the proguard website
ProGuard now has a sibling optimizer and obfuscator for Android:
DexGuard. It is compatible with ProGuard, and it provides additional
features like string encryption, class encryption, and hiding access
to sensitive APIs. It directly targets Dalvik bytecode and streamlines
the Android build process.
http://www.saikoa.com/dexguard
Related
I am getting my app crashed with this error, in the released version of app.
Its working fine in debug mode
Process: com.app, PID: 13162
java.lang.AssertionError: java.lang.NoSuchMethodException: <init> [interface twitter4j.conf.Configuration, interface twitter4j.a.b]
at twitter4j.n.<clinit>(Unknown Source)
at com.app.activities.RegistrationActivity.g(Unknown Source)
at com.app.activities.RegistrationActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NoSuchMethodException: <init> [interface twitter4j.conf.Configuration, interface twitter4j.a.b]
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getDeclaredConstructor(Class.java:510)
at twitter4j.n.<clinit>(Unknown Source)
at com.app.activities.RegistrationActivity.g(Unknown Source)
at com.app.activities.RegistrationActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
The error seems to be generated because of proguard file
Here is the proguard-rules.pro as follows -
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/adisoft2/android/android-sdk-linux/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
#-injars bin/classes
#-injars libs
#-outjars bin/classes-processed.jar
#-libraryjars /usr/local/java/android-sdk/platforms/android-9/android.jar
-keep class twitter4j.conf.**
-keep class twitter4j.TwitterImpl
-keep class twitter4j.conf.PropertyConfigurationFactory
-dontwarn com.squareup.okhttp.**
-dontwarn com.google.appengine.api.urlfetch.**
-dontwarn rx.**
-dontwarn retrofit.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
#retrofit.http.* <methods>;
}
-dontwarn twitter4j.**
-dontwarn com.google.android.gms.**
-dontwarn okio.**
# 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
# 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);
}
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator 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.**
Please let me know, what am I doing wrong, and how to fix.
Adding this in proguard-rules.pro worked for me.
-keep class twitter4j.** { *; }
Yes I accept the above (NarendraJi) answer. But in my case, it was not showing which library or class creating the issue.
It is showing like
Caused by: java.lang.NoSuchMethodException: <init>
at com.a.f
From the above information we cannot understand the problem. So I used the "app/build/outputs/release/mapping.txt" file.
mapping.txt file having clear information about all classes,methods and packages. So we can easily solve this type of problems
I'm using the newest ActionBarSherlock release (4.4) and many of my Android 2 users experience a fatal crash while the sherlock library tries to load the activity icon.
I tried cleaning the project but the bug keeps appearing (I suspect the generated R file for the library is incorrect?). I found some topics covering this crash but no working solutions.
My project exists out of the ActionBarSherlock library which is used by another library project containing some Activities. A few other Android projects use the library project with the Activities in it. I don't think this could be the problem, could it?
Important thing to notice is that the app works fine on a emulator running Android 2.x. I'm also using proguard.
Could the crash have something to do with the loadLogoFromManifest method?
https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock/src/com/actionbarsherlock/internal/ResourcesCompat.java#L112
Devices and Android versions affected so far
Optimus L3 (e0)
Galaxy Pocket (GT-S5300)
Galaxy Y (GT-S5360)
Android 2.3.3 - 2.3.7
Proguard
-keepattributes SourceFile,LineNumberTable
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
-keepattributes *Annotation*
-keep public class * extends com.actionbarsherlock.app.SherlockListFragment
-keep public class * extends com.actionbarsherlock.app.SherlockFragment
-keep public class * extends com.actionbarsherlock.app.SherlockDialogFragment
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
Stacktrace
java.lang.RuntimeException: Unable to start activity ComponentInfo{packagehere/activityhere}: android.view.InflateException: Binary XML file line #32: Error inflating class com.actionbarsherlock.internal.widget.ActionBarView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class com.actionbarsherlock.internal.widget.ActionBarView
at android.view.LayoutInflater.createView(LayoutInflater.java:518)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.generateLayout(ActionBarSherlockCompat.java:1010)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.installDecor(ActionBarSherlockCompat.java:902)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:836)
at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:261)
at someactivity.onCreate(BibleMain.java:128)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.view.LayoutInflater.createView(LayoutInflater.java:505)
... 24 more
Caused by: android.content.res.Resources$NotFoundException: File from drawable resource ID #0x7f020091
at android.content.res.Resources.loadDrawable(Resources.java:1714)
at android.content.res.Resources.getDrawable(Resources.java:581)
at com.actionbarsherlock.internal.widget.ActionBarView.(ActionBarView.java:191)
... 27 more
Caused by: java.io.FileNotFoundException:
at android.content.res.AssetManager.openNonAssetNative(Native Method)
at android.content.res.AssetManager.openNonAsset(AssetManager.java:406)
at android.content.res.Resources.loadDrawable(Resources.java:1706)
... 29 more
In Android, I have proguard with the following settings.
-dontpreverify
# Hold onto the mapping.text file, it can be used to unobfuscate stack traces in the developer console using the retrace tool
-printmapping mapping.txt
# Keep line numbers so they appear in the stack trace of the develeper console
-keepattributes SourceFile,LineNumberTable
# The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle.
-optimizations !code/simplification/arithmetic
# Activities, services and broadcast receivers are specified in the manifest file so they won't be automatically included
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
# Custom view components might be accessed from your layout files
-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
# event handlers can be specified in the layout files e.g. android:onClick="nextButton_onClick", I borrowed this method name notation from .NET
-keepclassmembers class * extends android.app.Activity {
public void *_*(android.view.View);
}
# Parcelable implementations are accessed by introspection
-keepclassmembers class * implements android.os.Parcelable {
static android.os.Parcelable$Creator CREATOR;
}
# You might want to keep your annotations
-keepattributes *Annotation*
# I use Google Guava in my app
# see http://code.google.com/p/guava-libraries/wiki/UsingProGuardWithGuava
-libraryjars libs/google/jsr305-1.3.9.jar;libs/pinyin4j/pinyin4j-2.5.0.jar
-dontwarn sun.misc.Unsafe
-keepclasseswithmembers class com.google.common.base.internal.Finalizer{
<methods>;
}
Some of my library, are directly imported from Java SE (Contains JApplet for example)
How I can exclude them from proguard? Note, I have pinyin4j-2.5.0.jar library in -libraryjars. I thought that's the way to tell proguard, "Hey, this is a library. Don't do anything on it, OK?" But, seems like proguard still trying to process pinyin4j-2.5.0.jar
I'm getting the following errors.
Note: there were 125 duplicate class definitions.
Warning: demo.Pinyin4jAppletDemo: can't find superclass or interface javax.swing.JApplet
Warning: demo.Pinyin4jAppletDemo$1: can't find superclass or interface java.awt.event.WindowAdapter
Warning: demo.Pinyin4jAppletDemo$2: can't find superclass or interface java.awt.event.ActionListener
Warning: demo.Pinyin4jAppletDemo$3: can't find superclass or interface java.awt.event.ActionListener
Warning: org.jasypt.encryption.pbe.PBEBigDecimalCleanablePasswordEncryptor: can't find superclass or interface org.jasypt.encryption.pbe.PBEBigDecimalEncryptor
Warning: org.jasypt.encryption.pbe.PBEBigIntegerCleanablePasswordEncryptor: can't find superclass or interface org.jasypt.encryption.pbe.PBEBigIntegerEncryptor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyDescriptor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyDescriptor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditorManager
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyDescriptor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.IntrospectionException
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyEditor
Warning: au.com.bytecode.opencsv.bean.CsvToBean: can't find referenced class java.beans.PropertyDescriptor
...
...
Warning: org.jasypt.normalization.Normalizer: can't find referenced class com.ibm.icu.text.Normalizer$Mode
You should check if you need to specify additional program jars.
Warning: there were 333 unresolved references to classes or interfaces.
You may need to specify additional library jars (using '-libraryjars').
Warning: there were 6 unresolved references to program class members.
Your input classes appear to be inconsistent.
You may need to recompile them and try again.
Alternatively, you may have to specify the option
'-dontskipnonpubliclibraryclassmembers'.
Error: Please correct the above warnings first.
Some of you may comment that for library jars which contain Java SE only methods (like Applet, Swing, ...) cannot be used in Android. Nope. In fact, they run perfectly fine, as long as you consume their non-Java SE-only methods.
The full error log can be downloaded from here : https://www.dropbox.com/s/dns62f7gp6unusg/error-log.txt
If you are sure that these Java SE classes are not used, you can indeed ignore the warnings (as you have found in your own answer). An easier way to specify this:
-dontwarn java.beans.**
-dontwarn java.awt.**
-dontwarn javax.swing.**
See ProGuard manual > Troubleshooting > Warning: can't find referenced class
Similar questions with always the same answer:
Using Proguard to Obfuscate Android App with Dropbox.com Libraries
Mobclix and Proguard
Using ProGuard with Android
Proguard tells me 'Please correct the above warnings first.
proguard hell - can't find referenced class
ProGuard with maven-android-plugin
...
Example to keep java runtime (rt.jar)
<libraryjar file="${java.home}/lib/rt.jar" />
It seems that your line
-libraryjars libs/google/jsr305-1.3.9.jar;libs/pinyin4j/pinyin4j-2.5.0.jar
is not complete.
Update
But since the needed classes do not exist on android, you have to ignore these warnings.
But do not generally ignore obfuscation warnings, we had a serious bug (using an obfuscation.map in file), because we ignored all warnings.
I merely avoid the error by
-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn javax.swing.**
-dontwarn java.awt.**
-dontwarn org.jasypt.encryption.pbe.**
-dontwarn java.beans.**
-dontwarn org.joda.time.**
-dontwarn com.google.android.gms.**
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn com.ibm.icu.text.**
-dontwarn demo.**
Here's the complete proguard configuration
-optimizationpasses 1
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
#-dontobfuscate
-dontwarn sun.misc.Unsafe
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn javax.swing.**
-dontwarn java.awt.**
-dontwarn org.jasypt.encryption.pbe.**
-dontwarn java.beans.**
-dontwarn org.joda.time.**
-dontwarn com.google.android.gms.**
-dontwarn org.w3c.dom.bootstrap.**
-dontwarn com.ibm.icu.text.**
-dontwarn demo.**
# Hold onto the mapping.text file, it can be used to unobfuscate stack traces in the developer console using the retrace tool
-printmapping mapping.txt
# Keep line numbers so they appear in the stack trace of the develeper console
-keepattributes *Annotation*,SourceFile,LineNumberTable
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
# https://sourceforge.net/p/proguard/discussion/182456/thread/e4d73acf
-keep class org.codehaus.** { *; }
-assumenosideeffects class android.util.Log {
public static int d(...);
public static int i(...);
public static int e(...);
public static int v(...);
}
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
public static *** i(...);
}
# I use Google Guava in my app
# see http://code.google.com/p/guava-libraries/wiki/UsingProGuardWithGuava
-libraryjars libs/google/jsr305-1.3.9.jar
-keepclasseswithmembers class com.google.common.base.internal.Finalizer{
<methods>;
}
It doesn't work completely out of box yet as I still experience crash in "after proguard generated" APK.
Is pretty hard for me to find out why it crashes, as the code is obfuscate.
If I specific dontobfuscate, I will get another set of problem during generation. A message "Conversion to Dalvik format failed with error 1" pop up without additional information. But, that's another set of different problem.
My application has references to HoloEverywhere and SherlockActionBar, both which are in-workspace projects. When I enabled Proguard for the application only, it crashes giving these errors:
11-15 11:50:11.090: E/AndroidRuntime(24823): Caused by: java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.app.Activity, int]
11-15 11:50:11.090: E/AndroidRuntime(24823): at com.actionbarsherlock.a.a(Unknown Source)
11-15 11:50:11.090: E/AndroidRuntime(24823): at org.holoeverywhere.a.a.l(Unknown Source)
11-15 11:50:11.090: E/AndroidRuntime(24823): at org.holoeverywhere.a.a.setContentView(Unknown Source)
Is this because I only enabled Proguard for the app and not the other in-workspace projects HE and ABS?
I am using the default proguard-project.txt in the sdk and did not add any additional rules.
No you do not.
Use these rules to keep the referenced classes un-obfuscated.
-keep class com.actionbarsherlock.** {*;}
-keep class org.holoeverywhere.** {*;}
this also will help
## ActionBarSherlock 4.4.0 specific rules ##
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
-keepattributes *Annotation*
## hack for Actionbarsherlock 4.4.0, see https://github.com/JakeWharton/ActionBarSherlock/issues/1001 ##
-dontwarn com.actionbarsherlock.internal.**
I get an exception when I istall my app on the phone. It works without proguard. What is wrong in my configuration? Thank you very much! The config is a default android config i found somwhere. Dont know whats going on
D/AndroidRuntime(15388): Shutting down VM
W/dalvikvm(15388): threadid=1: thread exiting with uncaught exception (group=0x40c3a1f8)
E/AndroidRuntime(15388): FATAL EXCEPTION: main
E/AndroidRuntime(15388): java.lang.NullPointerException
E/AndroidRuntime(15388): at aV.setViewValue(SourceFile:163)
E/AndroidRuntime(15388): at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:168)
E/AndroidRuntime(15388): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
E/AndroidRuntime(15388): at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
E/AndroidRuntime(15388): at android.widget.AbsListView.obtainView(AbsListView.java:2214)
E/AndroidRuntime(15388): at android.widget.ListView.makeAndAddView(ListView.java:1774)
E/AndroidRuntime(15388): at android.widget.ListView.fillDown(ListView.java:672)
E/AndroidRuntime(15388): at android.widget.ListView.fillFromTop(ListView.java:732)
E/AndroidRuntime(15388): at android.widget.ListView.layoutChildren(ListView.java:1625)
E/AndroidRuntime(15388): at android.widget.AbsListView.onLayout(AbsListView.java:2044)
E/AndroidRuntime(15388): at android.view.View.layout(View.java:11418)
E/AndroidRuntime(15388): at android.view.ViewGroup.layout(ViewGroup.java:4224)
E/AndroidRuntime(15388): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
E/AndroidRuntime(15388): at android.view.View.layout(View.java:11418)
E/AndroidRuntime(15388): at android.view.ViewGroup.layout(ViewGroup.java:4224)
E/AndroidRuntime(15388): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
E/AndroidRuntime(15388): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
E/AndroidRuntime(15388): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
E/AndroidRuntime(15388): at android.view.View.layout(View.java:11418)
E/AndroidRuntime(15388): at android.view.ViewGroup.layout(ViewGroup.java:4224)
E/AndroidRuntime(15388): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
E/AndroidRuntime(15388): at android.view.View.layout(View.java:11418)
E/AndroidRuntime(15388): at android.view.ViewGroup.layout(ViewGroup.java:4224)
E/AndroidRuntime(15388): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1628)
E/AndroidRuntime(15388): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
E/AndroidRuntime(15388): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(15388): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(15388): at android.app.ActivityThread.main(ActivityThread.java:4507)
E/AndroidRuntime(15388): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(15388): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(15388): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
E/AndroidRuntime(15388): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
E/AndroidRuntime(15388): at dalvik.system.NativeStart.main(Native Method)
here is my proguard config
-injars bin/classes
-injars libs
-outjars bin/classes-processed.jar
-libraryjars /libs/android-support-v13.jar
#
# This ProGuard configuration file illustrates how to process Android
# applications.
# Usage:
# java -jar proguard.jar #android.pro
#
# If you're using the Android SDK (version 2.3 or higher), the android tool
# already creates a file like this in your project, called proguard.cfg.
# It should contain the settings of this file, minus the input and output paths
# (-injars, -outjars, -libraryjars, -printmapping, and -printseeds).
# The generated Ant build file automatically sets these paths.
# Specify the input jars, output jars, and library jars.
# Note that ProGuard works with Java bytecode (.class),
# before the dex compiler converts it into Dalvik code (.dex).
# Save the obfuscation mapping to a file, so you can de-obfuscate any stack
# traces later on.
-printmapping bin/classes-processed.map
# You can print out the seeds that are matching the keep options below.
#-printseeds bin/classes-processed.seeds
# Preverification is irrelevant for the dex compiler and the Dalvik VM.
-dontpreverify
# Reduce the size of the output some more.
-repackageclasses ''
-allowaccessmodification
# Switch off some optimizations that trip older versions of the Dalvik VM.
-optimizations !code/simplification/arithmetic
# Keep a fixed source file attribute and all line number tables to get line
# numbers in the stack traces.
# You can comment this out if you're not interested in stack traces.
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
# RemoteViews might need annotations.
-keepattributes *Annotation*
# Preserve all fundamental application classes.
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
# Preserve all View implementations, their special context constructors, and
# their setters.
-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}
# Preserve all classes that have special context constructors, and the
# constructors themselves.
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
# Preserve all classes that have special context constructors, and the
# constructors themselves.
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
# Preserve the special fields of all Parcelable implementations.
-keepclassmembers class * implements android.os.Parcelable {
static android.os.Parcelable$Creator CREATOR;
}
# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* {
public static <fields>;
}
# Preserve the required interface from the License Verification Library
# (but don't nag the developer if the library is not used at all).
#-keep public interface com.android.vending.licensing.ILicensingService
#-dontnote com.android.vending.licensing.ILicensingService
# The Android Compatibility library references some classes that may not be
# present in all versions of the API, but we know that's ok.
-dontwarn android.support.**
# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
native <methods>;
}
# Preserve the special static methods that are required in all enumeration
# classes.
-keepclassmembers class * extends java.lang.Enum {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
# You can comment this out if your application doesn't use serialization.
# If your code contains serializable classes that have to be backward
# compatible, please refer to the manual.
#-keepclassmembers class * implements java.io.Serializable {
# static final long serialVersionUID;
# static final java.io.ObjectStreamField[] serialPersistentFields;
# private void writeObject(java.io.ObjectOutputStream);
# private void readObject(java.io.ObjectInputStream);
# java.lang.Object writeReplace();
# java.lang.Object readResolve();
#}
# Your application may contain more items that need to be preserved;
# typically classes that are dynamically created using Class.forName:
# -keep public class mypackage.MyClass
# -keep public interface mypackage.MyInterface
# -keep public class * implements mypackage.MyInterface
# Needed by google-api-client to keep generic types and #Key annotations accessed via reflection
-keepclassmembers class * {
#com.google.api.client.util.Key <fields>;
}
-keepattributes Signature,RuntimeVisibleAnnotations,AnnotationDefault,*Annotation*
-dontwarn sun.misc.Unsafe
-keepattributes
-dontoptimize
#-dontshrink
Setting the Scene
I currently have the exact same problem. I have perfectly working code with a custom adapter with layout inflater using the format with parent and false as 2nd and 3rd argument.
Since the code runs fine without obfuscation I feel confident enough to ignore all the questions and responses on stackoverflow about null views being returned due to wrong use of the Android API.
If you also have the issue without obfuscation ignore this answer and question and move on! Check these questions
I'll do my best to report my findings in this answer. This answer might grow over time. Skip to Conclusion at the bottom if you're eager to know the latest.
The Quick and Dirty Solution
Straight forward solution here is to update your proguard configuration to exclude the class your code fails on after obfuscation.
That should look something like this in your proguard config file:
-keep class * extends your.company.secret.package
For more information on proguard start looking here. Also make sure you have at least the minimal required proguard config file for an Android project. Check this example.
Mapping Back to your Source Code
When looking into this you might be interested in what the original name of that class was before obfuscation. That should make it a lot easier. You can find this mapping in the bin folder of your project in a proguard folder in the mapping.txt file.
/bin/proguard/mapping.txt
Pinned Down to the following lines of code
In my case I was able to pin down the error on the following lines of codes. These lines were only present in the adapter that failed while other code was common with the working adapters. All of below code is part of the getView(final int position, View convertView, final ViewGroup parent):View method
holder.layout.removeAllViews();
TextView tView;
for (final String text : mItems.getTextList()) {
tView = new TextView(mContext);
tView.setTextSize(14);
tView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tView.setText(text);
holder.layout.addView(tView);
}
If I just remove the addView() and removeAllViews() calls than all goes well.
Android Source Code
My first idea was to check the source code for that line of code. I found the following point in the stacktrace.
11-20 14:19:40.709: E/AndroidRuntime(22236): FATAL EXCEPTION: main
11-20 14:19:40.709: E/AndroidRuntime(22236): Process: your.company.secret.package, PID: 22236
11-20 14:19:40.709: E/AndroidRuntime(22236): java.lang.NullPointerException
11-20 14:19:40.709: E/AndroidRuntime(22236): at android.widget.AbsListView.obtainView(AbsListView.java:2265)
The Source for AbsListView can be found on grepcode.However looking at that code didn't give me a lot of courage of finding a solution there. Instead I turned to other resources. Might come back to this later.
I executed this code on an Android 4.4 device so the line number didn't even match.
tested on a 4.3 device
Correct line of code is 2177. Matches with this line:
child = mAdapter.getView(position, null, this);
Sample Project
See https://github.com/hanscappelle/so-10822397 for a sample project. You'll need Android Studio (Beta) 0.8.2 and latest build tools. I also had to manually fetch the proguard-android.txt file and put it in place.
You can comment out the lines in the adapter that work on the layout field of the static ViewHolder class. Those are the ones that fail once proguard is enabled and the app runs on a device.
Things I plan to do next
(DONE) I have other adapters in code that work fine even after obfuscation. I'll check these for differences with the once that fail
(DONE) I really have to check for a 4.3 device so I can look into Android Source code properly.
(DONE) Create sample project
TODO complete analysis with sample project
Conclusion
Seems to be related to dynamically adding views in the getView() method of the custom adapter. That in combination with code obfuscation.
Try turning off more optimizations that might be blowing up Dalvik. Upon further inspection it looks like you're disabling all optimizations actually.
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
Also try preserving these..
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
EDIT: Here is my proguard file, wonder if just replacing it will work. Its the default file generated by by ADT so nothing special here.
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembernames class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}