0
votes

I would like to use Koin library from Java. According to this tutorial, Koin modules can be written in Kotlin even using Java in the rest of a project.

https://insert-koin.io/docs/quickstart/android-java/

I use also @JvmField annotation for the module field because in the source code of this tutorial it is used

https://github.com/InsertKoinIO/koin/blob/master/quickstart/getting-started-koin-android/app/src/main/kotlin/org/koin/sample/AppModule.kt

In my project I use koinInjector which is a list of modules

import org.koin.android.java.KoinAndroidApplication;
import org.koin.core.KoinApplication;

import static org.koin.core.context.DefaultContextExtKt.startKoin;
import static com.x.y.KoinInjectorKt.koinInjector;

public class App extends Singleton {
    @Override
    public void onCreate() {
        super.onCreate();
        KoinApplication koin = KoinAndroidApplication
                .create(this)
                .modules(koinInjector);
        startKoin(koin);
    }
}

KoinInjector.kt:

@JvmField
val koinInjector: List<Module> = listOf(
        localDbModule,
)

But during build I get the error:

error: cannot find symbol
import static com.x.y.KoinInjectorKt.koinInjector;
                               ^
  symbol:   class KoinInjectorKt
  location: package com.x.y
3
What is the file name of kotlin file where koinInjector is declared? - Jenea Vranceanu
KoinInjector . Whereas the name KoinInjectorKt in import was generated automatically - jdev
Perhaps a (subtle) difference in build scripts? Could you include your build.gradle file? - Jeroen Steenbeeke

3 Answers

0
votes

I had to add

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"

to build.gradle and

apply plugin: 'kotlin-android' to build.gradle (app)`

Now it works.

Thanks @Jeroen Steenbeeke for your comment

0
votes

Try to replace the code with -

import org.koin.android.java.KoinAndroidApplication; 
import org.koin.core.KoinApplication;

import static org.koin.core.context.DefaultContextExtKt.startKoin;
import static com.x.y.KoinInjectorKt.koinInjector;

public class App extends Singleton {
 @Override
 public void onCreate() {
    super.onCreate();
    KoinApplication koin = KoinAndroidApplication
            .create(this)
            .modules(listOf(localDbModule));
    startKoin(koin);
   }
} 
-1
votes

remove this import

'import static com.x.y.KoinInjectorKt.koinInjector;'

and write your code like this

public class App extends Singleton {
@Override
public void onCreate() {
    super.onCreate();
    List<Module> list = new ArrayList<Module>();
    list.add(localDbModule);
    KoinApplication koin = KoinAndroidApplication
            .create(this)
            .modules(list);
    startKoin(koin);
 }
}