15

I'm trying to replace Dagger 2 to Koin in my current project and I don't want to rewrite some classes in Kotlin to use it.

Is possible to inject with Koin in java classes?

In Kotlin is just

// Inject MyPresenter
val presenter : MyPresenter by inject()

Thanks

Rafa Araujo
  • 314
  • 1
  • 2
  • 9

6 Answers6

23

Yes it is Possible. Just sync project with this gradle file

implementation "org.koin:koin-java:$koin_version"

In your java class replace

// Inject MyPresenter
private val presenter : MyPresenter by inject()

with

private Lazy<MyPresenter> presenter = inject(MyPresenter.class);

and get presenter method inside Java class like

presenter.getValue().sayHello() 
Pawan Soni
  • 860
  • 8
  • 19
  • Hey @pawan soni, could you clarify which imports you are using for inject and Lazy? is it `import static org.koin.core.KoinComponentKt.inject;` and `import kotlin.Lazy;` ? – Blaze Gawlik Sep 30 '19 at 20:17
  • 1
    kotlin.Lazy and org.koin.java.KoinJavaComponent.inject – Pawan Soni Oct 01 '19 at 17:21
  • 2
    Per Koin's GitHub page: `// Koin for Java developers is now part of core` so no need to add any extra implementations in the grade file. – ekreloff Sep 24 '20 at 16:20
11

You no longer need the additional koin-java dependency as this is now a part of koin-android & koin-core.

// Imports
import static org.koin.java.KoinJavaComponent.get;
import static org.koin.java.KoinJavaComponent.inject;
import static org.koin.core.qualifier.QualifierKt.named;
import static org.koin.core.parameter.DefinitionParametersKt.parametersOf;

// Lazy injection
Lazy<MyDependency> dependency = inject(MyDependency.class);

// Eager injection
MyDependency dependency = get(MyDependency.class);

// Named injection
get(MyDependency.class, named("MyNamedDependency"));

// Parameter injection
get(MyDependency.class, null, () -> parametersOf(this));

Koin Android Java docs

2

A class or file can either have .kt extension means Kotlin, or have .java extension means Java. You can't write a file or class in both the languages simultaneously.

But your Java and Kotlin files can talk with each other, i.e you can a Java class with a variable and access that variable in your Kotlin file or vice-versa.

So you can inject a Kotlin class reference and use that in your Java class and Vice-versa.

This might help: https://kotlinlang.org/docs/tutorials/mixing-java-kotlin-intellij.html

I hope that clears the doubt.

Keshav Aggarwal
  • 754
  • 6
  • 14
1

Use

// Java Compatibility
implementation "io.insert-koin:koin-android-compat:$koin_version"

Instead

implementation "org.koin:koin-java:$koin_version"

Don't forget

// Add Maven Central to your repositories if needed
repositories {
    mavenCentral()
}

Importante point

Due to Jcenter shutdown, the koin project's maven group id was previously org.koin and is now io.insert-koin. Please check your configuration with modules below.

See official documentation here

Have a good job!

Abner Escócio
  • 2,697
  • 2
  • 17
  • 36
0

It is, make sure you import in gradle the koin for java library and use above answers.

Gradle:

implementation "org.koin:koin-java:2.0.1"
peresisUser
  • 1,680
  • 18
  • 23
0

following code worked for me without org.koin:koin-java:$koin_version dependency:

private MyPresenter presenter = org.koin.java.KoinJavaComponentKoinJavaComponent.inject(MyPresenter.class).getValue();
ultra.deep
  • 1,699
  • 1
  • 19
  • 23