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?

StealthRT
  • 10,108
  • 40
  • 183
  • 342
Alex
  • 1,857
  • 4
  • 17
  • 34
  • 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. – Dave Newton Jul 12 '19 at 14:48

1 Answers1

11

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491