Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject Koin in Java classes

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

like image 301
Rafa Araujo Avatar asked Jun 03 '18 16:06

Rafa Araujo


People also ask

Can we use KOIN with Java?

Yeah, they work.

Is KOIN better than dagger?

As I mentioned before Dagger/Hilt has a significant impact on build time due to code generation. On the other hand, Koin also affects time, but not build, but runtime. Koin has slightly worse runtime performance, because it resolves dependencies at runtime.

Is KOIN multiplatform?

Koin is a popular and declarative library for DI, and it supports Kotlin Multiplatform. You declare the dependencies as modules, and Koin resolves them at runtime when needed.


1 Answers

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() 
like image 187
Pawan Soni Avatar answered Oct 16 '22 17:10

Pawan Soni