Annotation processor which can generates modules, integrated with Hilt, that can be reload.
Find a file
dependabot[bot] d96830cae8
Bump kotlin_version from 1.5.21 to 1.6.0
Bumps `kotlin_version` from 1.5.21 to 1.6.0.

Updates `kotlin-gradle-plugin` from 1.5.21 to 1.6.0
- [Release notes](https://github.com/JetBrains/kotlin/releases)
- [Changelog](https://github.com/JetBrains/kotlin/blob/master/ChangeLog.md)
- [Commits](https://github.com/JetBrains/kotlin/commits)

Updates `kotlin-stdlib` from 1.5.21 to 1.6.0
- [Release notes](https://github.com/JetBrains/kotlin/releases)
- [Changelog](https://github.com/JetBrains/kotlin/blob/master/ChangeLog.md)
- [Commits](https://github.com/JetBrains/kotlin/commits)

Updates `kotlin-compiler-embeddable` from 1.5.21 to 1.6.0
- [Release notes](https://github.com/JetBrains/kotlin/releases)
- [Changelog](https://github.com/JetBrains/kotlin/blob/master/ChangeLog.md)
- [Commits](https://github.com/JetBrains/kotlin/commits)

Updates `kotlin-annotation-processing-embeddable` from 1.5.21 to 1.6.0
- [Release notes](https://github.com/JetBrains/kotlin/releases)
- [Changelog](https://github.com/JetBrains/kotlin/blob/master/ChangeLog.md)
- [Commits](https://github.com/JetBrains/kotlin/commits)

---
updated-dependencies:
- dependency-name: org.jetbrains.kotlin:kotlin-gradle-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
- dependency-name: org.jetbrains.kotlin:kotlin-stdlib
  dependency-type: direct:production
  update-type: version-update:semver-minor
- dependency-name: org.jetbrains.kotlin:kotlin-compiler-embeddable
  dependency-type: direct:production
  update-type: version-update:semver-minor
- dependency-name: org.jetbrains.kotlin:kotlin-annotation-processing-embeddable
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2021-11-15 12:13:43 +00:00
.github ci setup 2021-07-31 00:12:59 +03:00
.idea Initial project setup 2021-07-31 00:12:12 +03:00
annotation deployment setup 2021-07-31 00:25:50 +03:00
app Create example application 2021-07-31 00:12:55 +03:00
gradle/wrapper Initial project setup 2021-07-31 00:12:12 +03:00
processor deployment setup 2021-07-31 00:25:50 +03:00
.gitignore Initial commit 2021-07-30 21:17:36 +03:00
build.gradle Bump kotlin_version from 1.5.21 to 1.6.0 2021-11-15 12:13:43 +00:00
deploy.gradle deployment setup 2021-07-31 00:25:50 +03:00
gradle.properties Initial project setup 2021-07-31 00:12:12 +03:00
gradlew Initial project setup 2021-07-31 00:12:12 +03:00
gradlew.bat Initial project setup 2021-07-31 00:12:12 +03:00
LICENSE Initial commit 2021-07-30 21:17:36 +03:00
README.md Update README.md 2021-07-31 00:31:21 +03:00
settings.gradle Annotation processor Implementation 2021-07-31 00:12:46 +03:00

ReloadableHiltModule

Annotation processor which can generates modules integrated with Hilt that can be reload. Reload means all the objects provided by the Module will be kept until it's reloaded. At that point the provided instances are recreated.

Why

Hilt is opinionated and clear on it's rules. However with dagger I was able to have Components scoped for a Logged In User, or for a specific flow.

Today the equivalent can be done as detailed here: https://medium.com/androiddevelopers/hilt-adding-components-to-the-hierarchy-96f207d6d92d

However this requires to inject a UserManager, and inject your dependencies from EntryPoints. Which doesn't seem to worth it for me.

Of course if you have Single Activity for your logged in state, Hilt works just fine.

My case

In my case however, I still have multiple activities on some projects, so It would be great to easily clean caches when my user logs out.

I had the idea to borrow the concept loadKoinModules with override which can be used just to do that. So in this annotation processor I generate a module definition which works similarly.

Setup

Latest version: Latest release

// top level build.gradle //..

subprojects {
    repositories {
        // ...
        maven {
            url "https://maven.pkg.github.com/fknives/ReloadableHiltModule"
            credentials {
                username = project.findProperty("GITHUB_USERNAME") ?: System.getenv("GITHUB_USERNAME")
                password = project.findProperty("GITHUB_TOKEN") ?: System.getenv("GITHUB_TOKEN")
            }
            // how to get token
            // https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token
        }
    }
}

// local build.gradle

kapt {
    correctErrorTypes = true
}

dependencies {
    implementation "org.fnives.library.reloadable.module:annotation:$latest_version"
    kapt "org.fnives.library.reloadable.module:annotation-processor:$latest_version"
}

Usage

Create your ModuleAnnotation, example:

import org.fnives.library.reloadable.module.annotation.ReloadableModule

@ReloadableModule
@Target(AnnotationTarget.CONSTRUCTOR)
@Retention(AnnotationRetention.SOURCE)
annotation class LoggedInModuleInject

Apply to your classes such as:

class ContentRepository @LoggedInModuleInject constructor(private val contentGenerator: ContentGenerator) {

Where you want to reload the module, inject the generated Helper class:

import javax.inject.Inject

class LogoutUseCase @Inject constructor(
    private val reloadLoggedInModuleInjectModule: ReloadLoggedInModuleInjectModule
) {

    fun invoke() {
        reloadLoggedInModuleInjectModule.reload()
    }
}