android - Proguard breaks parameterized constructor injection -
i set test project see how can use roboguice , proguard injected parameterized constructor.
this proguard-rules.pro file:
-keep class roboguice.** { *; } -keep interface roboguice.** { *; } -dontwarn roboguice.** -keep class org.roboguice.** { *; } -keep interface org.roboguice.** { *; } -dontwarn org.roboguice.** -keepattributes ** -keep class com.example.vladfatu.roboguicetest.application.testmodule -keep class com.google.inject.binder -keep class com.google.inject.module -keep public class com.google.inject.** { *; } # keeps fields , constructors @inject -keepclassmembers,allowobfuscation class * { @com.google.inject.inject <fields>; @com.google.inject.inject <init>(...); }
this testmodule:
public class testmodule implements module { public testmodule() { super(); } @override public void configure(binder binder) { binder.bind(stateprovider.class).to(roboteststateprovider.class); } }
this stateprovider :
public interface stateprovider { string getstate(); }
this roboteststateprovider :
public class roboteststateprovider implements stateprovider { private context context; @inject public roboteststateprovider(context context) { this.context = context; } @override public string getstate() { return "state"; } }
and application class:
public class robotestapplication extends application { @override public void oncreate() { super.oncreate(); roboguice.setuseannotationdatabases(false); configureinjection(); } protected void configureinjection() { roboguice.getorcreatebaseapplicationinjector(this, roboguice.default_stage, roboguice.newdefaultrobomodule(this), new testmodule()); } }
when tries create application injector(in application class) fails error:
could not find suitable constructor in com.example.vladfatu.roboguicetest.monitor.roboteststateprovider. classes must have either 1 (and one) constructor annotated @inject or zero-argument constructor not private.
i thought "@com.google.inject.inject (...);" line proguard rules should have fixed this, hasn't. tried decompiling apk sure @inject annotation there(before constructor) , is, still doesn't work.
obviously, without proguard works fine... roboguice version use 3.0
if want context can either:
- @inject property
- use
provider<context>
argument constructor
and include below in proguard-rules.pro file:
-keep class com.google.inject.binder -keepclassmembers class * { @com.google.inject.inject <init>(...); } -keepclassmembers class * { @javax.inject.inject <init>(...); } # there's no way keep @observes methods, use orn*event convention identify event handlers -keepclassmembers class * { void *(**on*event); } -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*(...); } -keep public class roboguice.** -keep class roboguice.** { *; } -keep class org.roboguice.** { *; } -keep public class javax.** -keep public class javax.annotation.** -keep public @interface javax.annotation.** -keep public interface javax.annotation.** -keep public class com.actionbarsherlock.** -keep public class android.** -keep public class android.** { *; } -keep public class android.content.res.resources.theme -keep public class android.content.res.resources.theme { *; } -keep public class android.content.res.resources -keep public class android.content.res.resources { *; } -keep public class com.google.** { *; } -keep public class com.google.** -keep public class android.support.** -keep public interface android.support.** # # added work # #-dontwarn roboguice.activity.robosherlockpreferenceactivity -dontwarn com.actionbarsherlock.** -dontwarn com.google.android.maps.** -dontwarn javax.annotation.nullable -dontwarn roboguice.activity.robomapactivity -dontwarn roboguice.activity.sherlockaccountauthenticatoractivity -dontwarn javax.annotation.checkreturnvalue -dontwarn javax.annotation.concurrent.guardedby -dontwarn javax.annotation.parametersarenonnullbydefault -dontwarn sun.misc.unsafe -dontwarn javax.annotation.checkfornull -ignorewarnings
Comments
Post a Comment