Referencing Java classes from Kotlin in Android Studio


#1

Hello,

I have an Android project written in Java. While trying to migrate some bits to Kotlin, I am facing a dependency issue. Specifically, I want to use some classes, which are in Java.
The Kotlin compiler complains “Unresolved reference: MyActivity”

here is my gradle config

buildscript {   ext.kotlin_version = '0.6.1910'   repositories {   mavenCentral()   }   dependencies {   classpath 'com.android.tools.build:gradle:0.7.+'   classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$ext.kotlin_version"   } }

apply plugin: 'android’
apply plugin: ‘kotlin-android’

repositories {
  mavenCentral()

  maven {
  url ‘http://oss.sonatype.org/content/repositories/snapshots
  }
}

android {
  compileSdkVersion 18
  buildToolsVersion “19.0.1”

  defaultConfig {
  minSdkVersion 15
  targetSdkVersion 18
  }

  sourceSets {
  main.java.srcDirs += ‘src/main/kotlin’
  }
}

dependencies {
  compile ‘com.android.support:appcompat-v7:19.0.0’
  compile ‘com.android.support:support-v4:19.0.0’
  compile fileTree(dir: ‘libs’, include: ‘*.jar’)

  compile “org.jetbrains.kotlin:kotlin-stdlib:$ext.kotlin_version”
}


Is that possible to java classes from Kotlin?
Everything else compile fine, but as soon as I reference a Java class from Kotlin – the is a compilation error.


#2

Yes, you should be able to call Java classes from Kotlin. Could you share an example that doesn't work for you. At least the Kotlin code?


#3

I have the following structure

main

  • java
      – com.app.activities
  • kotlin
      – com.app.activities

And I can’t reference the Java classes from Kotlin. Neither I can do another way around.

Here is how I decrate an activity in Kotlin:

public open class SignUp() : Fragment() {


Basically, there should be some kind of support for incremental compilation, because classes are in the same package.
Or I am doing something wrong?


#4

Hi Andrei, I've tried to reproduce your problem but on my local machine all works fine. Could you please attach a project where this problem appears? If not, could you check that you'll have the same error compiling this project with gradle from command line? Thank you!


#5

@Natalia.Ukhorskaya

I’m having the exact same issue, on my local machine and CI machine (Travis CI).
I cannot reference any Java resource from Kotlin.

my project build.gradle:

buildscript {
ext.kotlin_version = '1.2.10'
repositories {
    jcenter()
    google()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
    classpath "io.realm:realm-gradle-plugin:3.3.2"
    classpath 'com.google.gms:google-services:3.0.0'
    classpath 'com.dicedmelon.gradle:jacoco-android:0.1.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}

ext {

build_tools_version = "26.0.3"

// Google libraries
support_version = '25.3.1'
google_service_version = '10.0.1'
}

allprojects {
repositories {
    jcenter()
}

app.gradle:

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

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}

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


repositories {
    maven { url 'https://maven.fabric.io/public' }
    mavenCentral()
}


kapt {
    arguments {
        arg('eventBusIndex', 'com.streetvoice.streetvoice.MyEventBusIndex')
    }
}

dependencies {
    implementation project(':in-app-apk-updater')
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
   ...
    implementation 'org.greenrobot:eventbus:3.1.1'
    kapt 'org.greenrobot:eventbus-annotation-processor:3.1.1'
   ...
    kapt 'com.google.dagger:dagger-compiler:2.14.1'
    kaptTest 'com.google.dagger:dagger-compiler:2.14.1'
    implementation 'com.google.dagger:dagger:2.14.1'
}


apply plugin: 'com.google.gms.google-services'

I did follow kotlin-examples: android-mixed-java-kotlin-project, but with no luck.

I’d happy to attach my project in private If you like to check it.