0

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'
}
  • Could u edit ur post to include ur gradle/maven file? By default, java files are parsed from `src/main/java` and kotlin files from `src/main/kotlin`. In order to use a single sources directory for both files, u need to configure your IDE or build script to look for kotlin/java sources in the same directory. – Stan van der Bend Jan 12 '23 at 03:13
  • Does it really say`lass KotlinClass` or is that a typo in the question? – Tyler V Jan 12 '23 at 05:18
  • @StanvanderBend Thanks for your reply! I think i have made the adjustments to the question. – harry powers Jan 15 '23 at 23:15
  • @TylerV Thanks, it was a typo. I have update it. – harry powers Jan 15 '23 at 23:15

3 Answers3

0

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
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Hi, thank you so much for your response. I have tried the steps you have shown but for some reason, i get a new error in the logcat tab "Could not remove dir '/data/data/com.example.testingkotlinwithjava/code_cache/.ll/': No such file or directory" – harry powers Jan 15 '23 at 23:04
  • I'm not sure what is going wrong. I am deleting the kotlin main activity and then inserting a new java main activity. I think this might be where the issue is with this method. Is there a different way I should be doing it? – harry powers Jan 15 '23 at 23:33
  • @harrypowers Activities need to be defined in the manifest or not if they have been removed. – MikeT Jan 16 '23 at 05:10
0

Create Kotlin class like this and keep rest the same.

enter image description here

Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23
0

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
   }
Jayesh Dankhara
  • 525
  • 7
  • 8
  • Thanks for the respons! This did not work for me either i get the error "InvalidPluginRequestException: Plugin with id 'org.jetbrains.kotlin.android' was already requested" – harry powers Jan 15 '23 at 23:17