'Is it possible in one Java file to mix Java code and Kotlin code

Android Studio 3.4.2

in build.gradle:

buildscript {
    ext.kotlin_version = '1.3.41'
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

in app/build.gradle:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.29.0'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'

In my android project I have activity as java class - LoginActivity.java

import android.app.Activity; import android.os.Bundle;

public class LoginActivity extends Activity{

private void testJavaMethod() {
    // this ia a java code
    String testJava = "Hello Java world";
}

private void testKotlinMethod() {
    // this ia a Kotlin method
    val testKotlin = "Hello Kotlin world"
}

In method testJavaMethod() success compile. But testKotlinMethod() not compile.

Error in this line:

    val testKotlin = "Hello Kotlin world"

error message: Cannot resolve symbol 'val'

The question is:

Is it possible in Android project in JAVA file to has one method write on Java and another method write on Kotlin?



Solution 1:[1]

Is it possible in Android project in JAVA file to has one method write on Java and another method write on Kotlin?

No, sorry. A source file is either Java or Kotlin, not some mix of the two.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CommonsWare