简体   繁体   中英

Import Kotlin class in Java Code - Android studio

I am working with an Android App written on Java and I need an external java file that imports some Kotlin Classes:

import kotlin.Pair;
import kotlin.Unit;
import kotlin.collections.CollectionsKt;
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;

I just cannot find any information on how to edit my Gradle scripts that the compiler is able to resolve those classes. When trying to run I only get the error org.gradle.api.GradleException: Please initialize at least one Kotlin target in 'app (:app)'

An instance of this class is created in my MainActivity.

My build.gradle for the Module contains the plugin id 'org.jetbrains.kotlin.multiplatform' version '1.5.10' and the dependencies

implementation 'androidx.core:core-ktx:1.5.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.10"

I am quite new to Gradle build scripts and I have no idea what I am doing wrong.

EDIT: I am using Gradle 7.0.1

Add this to your project-level build.gradle

    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

And to the top of your app-level build.gradle file:

    apply plugin: 'kotlin-android'

You may refer to this in case you still face any issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM