Injected view null with roboguice 3 when minifiedEnabled = true

304 views Asked by At

I am using RoboGuice 3 in my application and when minifiedEnabled is set to false in debug mode everything is working fine. If I however, set minifiedEnabled to true the injected views seem to be null:

NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference 

My ProGuard config looks as follows:

-dontobfuscate
-dontoptimize
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-allowaccessmodification
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''

-dontwarn rx.internal.**
-dontwarn retrofit.appengine.UrlFetchClient
-dontwarn roboguice.**
-dontwarn org.roboguice.**
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-dontwarn org.joda.convert.**

-keepattributes *Annotation*
-keepattributes Signature
-keepattributes Exceptions
-keepattributes InnerClasses
-keep class roboguice.**
-keep class com.google.inject.** { *; }
-keep class javax.inject.** { *; }
-keep class javax.annotation.** { *; }
-keep class retrofit.** { *; }
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.** { *; }
-keep class com.squareup.okhttp.** { *; }
-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 com.google.inject.Binder
-keep class com.afollestad.materialdialogs.GravityEnum
-keep class org.rtschat.views.tab.SlidingTabLayout$TabColorizer
-dontnote com.android.vending.licensing.ILicensingService

-keepclassmembers class ** {
    public void onEvent*(**);
}

-keepclassmembers class * {
    @com.google.inject.Inject <init>(...);
}

-keepclassmembers class * {
    void *(**On*Event);
}

-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private 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();
}

-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 class **.R$* {
  public static <fields>;
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep public class * {
    public protected *;
}

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

-keep class sun.misc.Unsafe { *; }

-keep class com.google.gson.examples.android.model.** { *; }
2

There are 2 answers

0
ReneWeb On BEST ANSWER

I could fix it by adding this to my proguard conf:

-keepclassmembers class * {
    ...
    @com.google.inject.InjectResource <init>(...);
    @com.google.inject.Inject <fields>;
    @com.google.inject.InjectView <fields>;
}
0
马梓轩 On

I fixed it by adding this to my proguard.txt,hope can help you

-keepclassmembers class * {
@roboguice.inject.InjectView <fields>;

}