Skip to content

ansman/auto-dagger

Auto Dagger Build Gradle Maven Central

Auto Dagger allows you to automate some Dagger setup using Hilt.

For example you can use the @AutoInitialize annotation to automatically initialize objects during app launch and you can use the @AutoBind annotation to automatically bind objects.

Automatic initialization is done using AndroidX Startup.

To read more please refer to the documentation.

For the changelog see the releases page.

Basic usage

interface Repository

@AutoInitialize // Automatically create the repo during startup
@AutoBind // Automatically bind RealRepository as Repository
@Singleton
class RealRepository @Inject constructor() : Repository {
    init {
        // This will be executed at application startup, even if nobody injects it.
    }
}

@AutoInitialize
@Singleton
class InitializableRepository @Inject constructor() : Initializable {
    override fun initialize() {
        // This will be executed at application startup, even if nobody injects it.
    }
}

For the full documentation see https://auto-dagger.ansman.se/

Setup

For detailed instructions see the getting-started page.

dependencies {
    // Set up your dagger dependencies and compiler

    // Include this in kotlin or android modules
    implementation("se.ansman.dagger.auto:core:1.2.1")
    kapt("se.ansman.dagger.auto:compiler:1.2.1")
    // If you're using KSP
    ksp("se.ansman.dagger.auto:compiler:1.2.1")


    // Include this in your app module
    implementation("se.ansman.dagger.auto:android:1.2.1")
    // Or this in your library module
    implementation("se.ansman.dagger.auto:android-api:1.2.1")

    // Add these if you want to replace objects during tests
    testImplementation("se.ansman.dagger.auto:android-testing:1.2.1")
    kaptTest("se.ansman.dagger.auto:compiler:1.2.1")
    // If you're using KSP
    kspTest("se.ansman.dagger.auto:compiler:1.2.1")

    // If you want to provide Retrofit services add the Retrofit dependency
    implementation("se.ansman.dagger.auto:retrofit:1.2.1")

    // If you want to provide Ktorfit services add the Retrofit dependency
    implementation("se.ansman.dagger.auto:ktorfit:1.2.1")

    // If you want to inject a CoroutineScope into ViewModels add the ViewModel dependency
    implementation("se.ansman.dagger.auto:androidx-viewmodel:1.2.1")

    // If you want to automatically provide your Room DAOs add the Room dependency
    implementation("se.ansman.dagger.auto:androidx-room:1.2.1")
}

License

This project is licensed under the Apache-2.0 license. See LICENSE.txt for the full license.


Copyright 2022-2023 Nicklas Ansman Giertz

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.