androiddagger-hiltandroid-r8

Android AGP 8.4 and hilt injecting into a service


After upgrading to Android Studio Jellyfish, and updating the Android Gradle Plugin 8.4.0, on a multi-module project I can't seem to inject into my Service any more when minify in enabled.

After the upgrades, I spent quite a bit of time adding new rules to the non-app modules (they are aar modules). I now have the app running, and most of the features working again. But I can't get the service working. It has quite a few things injected into it. Since it worked with the previous version of AGP, and it works with a debug build, I assume it's due some missing rule that's needed to due the new version of AGP. It really acts it doesn't see the annotations. I have the same rules in this module for keeping the annotations as I do in other modules, and they are getting their injections.

I've reduced the service to:

@AndroidEntryPoint
open class MyService : Service() {
    @Inject
    lateinit var test: DTest

    override fun onCreate() {
        super.onCreate()

        Timber.d("onCreate, ${test.test}")
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }
}

Where DTest is in the same module and is just:

@InstallIn(SingletonComponent::class)
@Module
object TestModule {
    @Provides
    @Singleton
    fun providesDTestModule(): DTest {
        return DTest("TestModule")
    }
}

data class DTest(val test: String)

When the service is started, this is the result:

E  java.lang.RuntimeException: Unable to create service my.package.MyService: kotlin.UninitializedPropertyAccessException: lateinit property test has not been initialized
E      at android.app.ActivityThread.handleCreateService(ActivityThread.java:4664)
E      at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
E      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2264)
E      at android.os.Handler.dispatchMessage(Handler.java:106)
E      at android.os.Looper.loopOnce(Looper.java:205)
E      at android.os.Looper.loop(Looper.java:294)
E      at android.app.ActivityThread.main(ActivityThread.java:8177)
E      at java.lang.reflect.Method.invoke(Native Method)
E      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
E      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
E  Caused by: kotlin.UninitializedPropertyAccessException: lateinit property test has not been initialized
E      at my.package.MyService.onCreate(SourceFile:8)
E      at android.app.ActivityThread.handleCreateService(ActivityThread.java:4651)
E      ... 9 more

Solution

  • These rules got me past the problem:

    -keep class my.pkg.name.di.** { *;}
    -keepclassmembers my.pkg.name.di.** { *; }
    -keep class my.pkg.name.MyService* { *; }
    

    where my.pkg.name.di is package where I keep my dagger modules

    Note that the keep on the service alone didn't work, I had to add an asterisk at the end to make sure some of the hilt generated class were kept. In fact, that was the key to getting this working.

    I might be wrong, but my sense is dagger needs to update their rules for the new version of the AGP.