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'
}

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.
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:-