Proguard 与 Roboguice

Proguard with Roboguice

我在应用程序中打开了 proguard 我在应用程序中有一个警告 onCreate

RoboGuice.injectMembers(_appContext, this);

异常:

nalizableReferenceQueue﹕ Could not load Finalizer in its own class loader. Loading Finalizer in the current class loader instead. As a result, you will not be able to garbage collect this class loader. To support reclaiming this class loader, either resolve the underlying issue, or move Google Collections to your system class path.
    java.io.FileNotFoundException: com/google/inject/internal/util/$Finalizer.class
            at com.google.inject.internal.util.$FinalizableReferenceQueue$DecoupledLoader.getBaseUrl(SourceFile:269)
            at com.google.inject.internal.util.$FinalizableReferenceQueue$DecoupledLoader.loadFinalizer(SourceFile:253)
            at com.google.inject.internal.util.$FinalizableReferenceQueue.loadFinalizer(SourceFile:175)
            at com.google.inject.internal.util.$FinalizableReferenceQueue.<clinit>(SourceFile:100)
            at com.google.inject.internal.util.$MapMaker$QueueHolder.<clinit>(SourceFile:787)
            at com.google.inject.internal.util.$MapMaker$WeakEntry.<init>(SourceFile:946)
            at com.google.inject.internal.util.$MapMaker$Strength.newEntry(SourceFile:312)
            at com.google.inject.internal.util.$MapMaker$StrategyImpl.newEntry(SourceFile:498)
            at com.google.inject.internal.util.$MapMaker$StrategyImpl.newEntry(SourceFile:419)
            at com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(SourceFile:2029)
            at com.google.inject.internal.Annotations$AnnotationChecker.hasAnnotations(SourceFile:116)
            at com.google.inject.internal.Annotations.isBindingAnnotation(SourceFile:180)
            at com.google.inject.Key.ensureIsBindingAnnotation(SourceFile:366)
            at com.google.inject.Key.strategyFor(SourceFile:338)
            at com.google.inject.Key.get(SourceFile:272)
            at com.google.inject.internal.AbstractBindingBuilder.annotatedWithInternal(SourceFile:82)
            at com.google.inject.internal.ConstantBindingBuilderImpl.annotatedWith(SourceFile:49)
            at roboguice.config.DefaultRoboModule.configure(SourceFile:117)
            at com.google.inject.AbstractModule.configure(SourceFile:59)
            at com.google.inject.spi.Elements$RecordingBinder.install(SourceFile:223)
            at com.google.inject.spi.Elements.getElements(SourceFile:101)
            at com.google.inject.spi.Elements.getElements(SourceFile:78)
            at roboguice.RoboGuice.setBaseApplicationInjector(SourceFile:83)
            at roboguice.RoboGuice.setBaseApplicationInjector(SourceFile:139)
            at roboguice.RoboGuice.getBaseApplicationInjector(SourceFile:59)
            at roboguice.RoboGuice.getInjector(SourceFile:149)
            at roboguice.RoboGuice.injectMembers(SourceFile:156)
            at com.package.MyApplication.x(SourceFile:847)
            at com.package.MyApplication.onCreate(SourceFile:243)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4712)
            at android.app.ActivityThread.-wrap1(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5422)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我在我的 proguard 文件中包含:

-keep public class * extends android.app.Application
-keep class com.google.inject.** { *; }
-keep class javax.inject.** { *; }
-keep class javax.annotation.** { *; }
-keep class roboguice.** { *; }

我还应该做什么?

请在您的文件中添加以下行,这将解决您的问题。

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

        # There's no way to keep all @Observes methods, so use the On*Event convention to 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 com.google.inject.internal.util.$Finalizer { *; }

这是根据官方文档here

添加到你的混淆文件

-keep class com.google.inject.internal.util.$Finalizer { *; }