Android studio - kotlin class not found when implementing in java file - error "cannot find symbol class"

2.1k views Asked by At

I am trying to use a kotlin class in my java class in android studio. But I get the following error twice...

"cannot find symbol class KotlinClass"

KotlinClass is the file name of the kotlin class I am trying to implement.

Both the java and kotlin file are in the same package.

I have tried to put them in different packages but it caused me more problems and so i kept them in the same package but i might have done this so if this is a potential solution, i can try again but i would need more detailed steps if possible.

This error occurs in the build tab.

I have looked into this quite a bit but nothing seems to work.

This is my java class

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        KotlinClass x = new KotlinClass();
        x.kotlinMethod();
    }
}

This is my kotlin class

class KotlinClass {
    fun kotlinMethod() {
        println("You did it again!!!")
    }
}

Im very new to android studio, any help will be appreciated

Here is my project level gradle file

    plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
}

Here is my module level gradle file

    plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.testingjava'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.testingjava"
        minSdk 29
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
4

There are 4 answers

3
MikeT On

I believe that your issue is that you have created the project as a Java project and a Java project doesn't support Kotlin directly.

If you create the project as a Kotlin Project e.g.

enter image description here

Then Kotlin supports Java from the onset. The only issue is that the Activity will then be in Kotlin.

To circumvent this, as was done below, you could add another Activity set it to be Java, and then use this to replace the Kotlin activity (not sure why other than to do it though).

The following demonstrates:-

enter image description here

  • i.e. the project comppiled and ran on the emulator.
  • as can be seen MainActivity (with some manipulation) is Java
  • KotlinClass is Kotlin
1
Sohaib Ahmed On

Create Kotlin class like this and keep rest the same.

enter image description here

1
Jayesh Dankhara On

For this issue, I have a simple solution that works for me.

add below id in build.gradle app level gradle

plugins {
      id 'org.jetbrains.kotlin.android'
}

plugins {
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
   }

0
Gonzalo Ledezma Torres On

This maybe will be a late answer. I had this issue yesterday and I solved this way:

1.- Add kotlin plugin to your Project build.gradle. I'm using kotlin version 1.7.20

buildscript {
  ext.kotlin_version = "1.7.20"
  repositories {
    google()
    mavenCentral()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:8.0.2'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"

  }
}

2.- Add kotlin plugins at the top of your App build.gradle like this:

plugins {
  id 'com.android.application'
  id 'kotlin-android'
  id 'kotlin-kapt'
  id 'kotlinx-serialization'
  id 'kotlin-parcelize'
}

3.- Clean, Rebuild, Compile. If any other error jumps, try to Clear Cache/Restart.

Ps: Maybe you won't need all the plugins, but I posted all, just in case.