Monocito
Monocito

Reputation: 107

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

Upvotes: 1

Views: 4420

Answers (1)

Yash Joshi
Yash Joshi

Reputation: 627

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.

Upvotes: 3

Related Questions