0

I'm trying to configure Dagger2 to be able to inject Appliaction instance, but while build I'm getting weird error:

DaggerAppComponent.java:3: error: package android.app does not exist
import android.app.Application;
                  ^

Here's my code:

Gradle Dagger2 dependencies

dependencies {
    ...
    implementation 'com.google.dagger:dagger:2.27'
    kapt 'com.google.dagger:dagger-compiler:2.27'
    ...
}

AppComponent

@Singleton
@Component(modules = [HttpModule::class, AuthModule::class])
interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(app: Application): Builder

        fun build(): AppComponent
    }

    fun inject(activity: BootstrapActivity)

    fun inject(activity: LoginActivity)
}

MyApplication

class MyApplication : Application() {
    private lateinit var appComponent: AppComponent

    override fun onCreate() {
        super.onCreate()

        appComponent = DaggerAppComponent.builder()
            .application(this)
            .build()
    }

    fun getAppComponent() = appComponent
}

BootstrapActivity (NoDisplay)

class BootstrapActivity : AppCompatActivity() {
    @Inject
    lateinit var auth: Auth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        (applicationContext as MyApplication).getAppComponent()
            .inject(this)

        //auth.fetchCurrentUser()

        val intent: Intent = if (auth.isLoggedIn()) {
            Intent(this, HomeActivity::class.java)
        } else {
            Intent(this, LoginActivity::class.java)
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

        startActivity(intent)

        finish()
    }
}

How it's possible that android.app package does not exists? Please help me, I'm fighting 2 days with it :(

rgwsk65
  • 199
  • 2
  • 16

2 Answers2

1

You should use your own application class when you're using a custom app class instead of the default android one. So in order to do that change your app component builder to:

@Component.Builder
interface Builder {
    @BindsInstance
    fun application(app: MyApplication): Builder

    fun build(): AppComponent
}

Also don't forget to include the necessary dependencies in your build.gradle:

api 'com.google.dagger:dagger-android:2.28'
api 'com.google.dagger:dagger-android-support:2.28' // you're using the support libraries

And the annotation processor if you wish to use the android-specific annotations (which I recommend you to do):

kapt 'com.google.dagger:dagger-android-processor:2.28'
Sdghasemi
  • 5,370
  • 1
  • 34
  • 42
  • I tried this before. I added again these dependencies and changed type to `MyApplication` in component Builder, but this gives me new error during `Component.application(this)`: `DaggerAppComponent.java:85: error: cannot access Application this.application = Preconditions.checkNotNull(app); ^ class file for android.app.Application not found` – rgwsk65 Jun 20 '20 at 07:56
  • Have you added `apply plugin: "kotlin-kapt"` to your **build.gradle** to use the kapt plugin? – Sdghasemi Jun 20 '20 at 08:29
  • Yes, it's present – rgwsk65 Jun 20 '20 at 08:32
  • Then according to [this](https://stackoverflow.com/a/58926463/4399414) answer try `annotationProcessor` instead of `kapt` and check if it solves the problem. – Sdghasemi Jun 20 '20 at 08:36
  • I posted an own answer. I had JVM version set to 10, downgrading to JVM 8 fixed this issue. – rgwsk65 Jun 20 '20 at 08:36
  • 1
    Glad to hear it! – Sdghasemi Jun 20 '20 at 08:37
0

Probably I finally solved this issue. I had JVM version 10 in Gradle, changing it to 1.8 solved my issue

rgwsk65
  • 199
  • 2
  • 16