koin-java

Koin features for Java developers

Below is a short description of koin-java features. This project is a small subset of static utils to help java developers.

Setup

// Add Jcenter to your repositories if needed
repositories {
    jcenter()    
}
dependencies {
    
    implementation 'org.koin:koin-java:1.0.0'
}

Declare a Java friendly module

You need to tag your module variable with @JvmField to make it readable from Java world:

@JvmField
val koinModule = module {
    single { ComponentA() }
    single { ComponentB(get()) }
    single { ComponentC(get(), get()) }

    module("anotherModule") {
        single { ComponentD(get()) }
    }
}

Start Koin

Just use the startKoin() static function (with the static import to reduce the syntax):

import static org.koin.java.standalone.KoinJavaStarter.startKoin;

//startKoin(/* list of module */);
startKoin(singletonList(koinModule));

You have access to the same options than the Kotlin startKoin().

Inject with static helpers

The KoinJavaComponent class is a static helper that brings Koin powers to Java:

  • inject() - lazy inject instance
  • get() - retrieve instance
  • getKoin() - get Koin context
  • getProperty() - get property
import static org.koin.java.standalone.KoinJavaComponent.*;

ComponentA a = get(ComponentA.class);
Lazy<ComponentA> lazy_a = inject(ComponentA.class);

More about Java features