5

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?

1
  • 2
    ... Java is Java, Kotlin is Kotlin. While it would be technically feasible to do such a thing, you'd need to have a mechanism for unified parsing and bytecode generation. Even if that was something anybody was interested in doing, it would be very confusing to mix two languages in a single file. It's not even clear to me why you'd ever want such a thing. Commented Jul 12, 2019 at 14:48

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.