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
Try turning off more optimizations that might be blowing up Dalvik. Upon further inspection it looks like you're disabling all optimizations actually.
Also try preserving these..
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.
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
andfalse
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:
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.
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
methodIf I just remove the
addView()
andremoveAllViews()
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.
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:
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.