Dagger + Proguard混淆,错误创建对象图

Mar*_*ues 3 android proguard dagger

运行我的应用程序的模糊版本会引发以下堆栈跟踪

java.lang.RuntimeException: Unable to create service com.mycompany.myapp.async.alarms.AlarmIntentService: java.lang.IllegalStateException: Errors creating object graph:
    dagger.Lazy could not be bound with key dagger.Lazy required by dagger.Lazy  com.mycompany.scheduler.c.mNotificationDisplayer
Run Code Online (Sandbox Code Playgroud)

如果我添加-dontobfuscate,它会顺利运行

这是包含该字段的类

public abstract class AbstractAlarmSchedulerService extends IntentService {

  @Inject
  Lazy<AbstractAlarmSchedulerNotificationDisplayer> mNotificationDisplayer;
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中从这个类扩展,但它属于一个外部库.

这些是我的匕首计划规则,复制自/sf/answers/1272424401/

#Dagger
-keepattributes *Annotation*

-keepclassmembers,allowobfuscation class * {
    @javax.inject.* *;
    @dagger.* *;
    <init>();
}

-keep class * extends dagger.internal.Binding
-keep class * extends dagger.internal.ModuleAdapter

-keep class **$$ModuleAdapter
-keep class **$$InjectAdapter
-keep class **$$StaticInjection

-keep class dagger.* { *; }

-keep class javax.inject.* { *; }
-keep class * extends dagger.internal.Binding
-keep class * extends dagger.internal.ModuleAdapter
-keep class * extends dagger.internal.StaticInjection

-keep !abstract class com.mycompany.** { *; }

-keepnames class dagger.Lazy
Run Code Online (Sandbox Code Playgroud)

我已经尝试让所有类和所有成员看看是否修复了什么,但错误仍然存​​在

-keep class * { *; }
Run Code Online (Sandbox Code Playgroud)

com.mycompany.scheduler是一个外部库,而com.mycompany.myapp包含实际应用程序的源.

如果需要,这是我正在使用的模块

@Module(injects = {AlarmIntentService.class, ReminderNotificationDisplayer.class, AlarmsBroadcastReceiver.class})
public class AndroidModule {
  private final AbstractMyApplication application;

  public AndroidModule(AbstractMyApplication application) {
    this.application = application;
  }

  /**
   * Allow the application context to be injected
   */
  @Provides @Singleton
  Context provideApplicationContext() {
    return application;
  }

  @Provides
  public AlarmManager provideAlarmManager(Context context){
    return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
  }

  @Provides
  @Singleton
  public NotificationManager provideNotificationManager(Context context){
    return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  }

  @Provides
  @Singleton
  public AbstractAlarmSchedulerNotificationDisplayer provideNotificationDisplayer() {
    return new ReminderNotificationDisplayer();
  }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用dagger&dagger-compiler 1.2.+依赖项

谢谢!

Chr*_*ber 9

Dagger 1.x存在混淆问题.它可以使用适当的-keep语句进行代码收缩,但由于使用了String键,混淆变得有问题.字符串是在编程之前生成的,但是在编程之后消耗,并且不与新重命名的类型对齐.

在我们禁用反射模块适配器之前Dagger 1.x(大约1.0.0)将起作用,因为纯反射导致类型的提供和注入被认为是"及时"(即在编程之后),因此混淆类型匹配.如果代码混淆的优先级高于性能,请考虑使用这个稍微旧的版本.

Dagger 2.x(正在进行中)没有字符串键,导致直接引用类,并且应该与Proguard很好地配合.请继续关注匕首列表和项目.我们预计2.x的早期版本会在发布后几周内下降.

此外,更具体地说,请确保您-keepattributes Signature.您看到的具体错误是因为JDK5 +泛型被proguard剥离.一个注射Lazy,一个注射,一个注射Lazy<Foo>.这将修复此特定错误,但您将遇到上述问题.