Dagger cannot create object graph although it can produce dot file

前端 未结 4 2158
盖世英雄少女心
盖世英雄少女心 2021-02-06 12:50

I\'m struggling with the setup of Dagger (1.0.1), in a existing application. It was configured to use ProGuard but I disabled it for this test with -dontobfuscate.<

相关标签:
4条回答
  • 2021-02-06 13:05

    Dagger relies a lot on reflection and class names that are hard-coded and manipulated as strings. This makes the code difficult to shrink/optimize/obfuscate.

    The following configuration works for the sample dagger/examples/simple in Dagger 1.1.0:

    -keepattributes *Annotation*
    
    -keepclassmembers,allowobfuscation class * {
        @javax.inject.* *;
        @dagger.* *;
        <init>();
    }
    
    -keep class **$$ModuleAdapter
    -keep class **$$InjectAdapter
    -keep class **$$StaticInjection
    
    -keepnames !abstract class coffee.*
    
    -keepnames class dagger.Lazy
    

    The configuration keeps all fields and methods with javax.inject or dagger annotations, and all parameterless constructors. ProGuard might otherwise remove them if they appear unused, but Dagger is actually injecting/accessing them through reflection. This is similar to RoboGuice.

    It also has to keep all adapter classes generated by Dagger.

    It also has to keep all class names related to these adapter classes, so the names still match. In this sample, those are almost all classes in the package coffee, so the easiest way is to use a wild-card. This line will be different for other applications.

    Finally, it also has to keep the name of the class dagger.Lazy, since its name is hard-coded as a string in the generated code.

    0 讨论(0)
  • 2021-02-06 13:07

    I got the app to start after adding -dontshrink to the ProGuard config file. Having -dontobfuscate at the beginning was not enough.

    In fact, if I remove -dontobfuscate it also works.

    I definitely need finer control for this but it's a starting point. My current ProGuard setup for Dagger is:

    #############
    #   Dagger  #
    #############
    
    -keep class dagger.** { *; }
    -dontwarn dagger.internal.codegen.**
    
    0 讨论(0)
  • 2021-02-06 13:13

    I ended up burning a week+ trying to get this to work. In the end I failed and decided to give DexGuard a shot. It worked beautifully right out of the box. Yes its a commercial product but DexGuard has great support and because of such we were able to finally ship. Id definitely recommend DexGuard if you absolutely need to solve this issue.

    0 讨论(0)
  • 2021-02-06 13:21

    Dagger doesn't require @Inject to be on a class to be passed into graph.inject(myActivity) because some activities may not have any injections to make. However, these seem like upstream dependencies, which means that they need to be provided to ComponentInfo, and therefore need to be provisioned by Dagger. It cannot do this if it cannot create these classes, and it can't do so if these are not annotated, unless it provides them via a @Provides method.

    So, you either need to create an @Module-annotated class which returns these types from @Provides-annotated methods, or you need to add @Inject to their constructor.

    -keep class * extends dagger.internal.Binding
    

    That said, in this case, are you using proguard in "release" mode? And not proguarding in debug mode? If so, I suspect Proguard to be stripping away annotations. You'll need to do some variant of:

    -keep class javax.inject.** { *; }
    

    ... to ensure that Proguard doesn't remove the annotations.

    0 讨论(0)
提交回复
热议问题