Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 566 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inject Koin in Java classes

#1
I'm trying to replace Dagger 2 to [Koin][1] 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


[1]:

[To see links please register here]

Reply

#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:

[To see links please register here]


I hope that clears the doubt.
Reply

#3
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"
Reply

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

private MyPresenter presenter = org.koin.java.KoinJavaComponentKoinJavaComponent.inject(MyPresenter.class).getValue();
Reply

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

```java
// 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][1]


[1]:

[To see links please register here]

Reply

#6
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()

Reply

#7
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][1]


[1]:

[To see links please register here]



Have a good job!
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through