Yvgen
Yvgen

Reputation: 2212

Start kotlin activity *.kt from java activity *.java?

Invalidate Caches/ restart... helps me!

My code in java class is:

Intent intent = new Intent(view.getActivity(), AddPaymentActivity.class);
view.getActivity().startActivity(intent);

AddPaymentActivity has kotlin extention .kt
Got error java.lang.NoClassDefFoundError

Upvotes: 14

Views: 12787

Answers (6)

saurav singh
saurav singh

Reputation: 51

Add Kotlin in your java project

  1. This should be your build.gradle(Project level) enter image description here

Add this kolin plugin in build.gradle module level

Add this in your depedency

Hope it work for u ,worked for me

Upvotes: 0

KKM
KKM

Reputation: 754

To anyone struggling inspite of all these answers, in my case, I forgot to add apply plugin: 'kotlin-android' inside the app level's build.gradle

Upvotes: 0

Dominik
Dominik

Reputation: 1693

You just do it like "normally" in Java:

Intent intent = new Intent(getActivity(), KotlinActivity.class);
startActivity(intent);

Just don't forget to add Kotlin to your project fist (in the gradle files). Otherwise it won't work.

Upvotes: 3

Linh
Linh

Reputation: 61019

In my case, I forgot to add

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

and

apply plugin: 'kotlin-android'
...
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

to build.gradle

Example

project build.gradle

...
buildscript {
  ext.kotlin_version = '1.1.51'
  dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    ...
  }
}

allprojects {
  repositories {
    jcenter()
    google()
  }
}

app build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
...

dependencies {
  ...
  compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

Upvotes: 9

Yvgen
Yvgen

Reputation: 2212

File -> Invalidate Caches/ restart...

Upvotes: 7

voddan
voddan

Reputation: 33839

There is no real difference between classes produced by Java and Kotlin. If your code is statically resolved by Android Studio (as it should), then it must run unless Gradle is misconfigured.

Upvotes: 4

Related Questions