Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't call kotlin module from java module

Tags:

android

kotlin

I want to have an android app in java, but one library module in kotlin. However when I try to run the app in my phone, there's an error saying that it can't find my Kotlin class. This is my kotlin class:

    package com.example.mylibrary

    import android.util.Log

    class A {
        fun helloWorld(){
            Log.d("Kotlin", "Hello World!")
        }
    }

and gradle file for my kotlin module:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
buildscript {
    ext.kotlin_version = '0.6.+'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
repositories {
    mavenCentral()
}

Here's my main activity:

package com.example.uisample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.mylibrary.A;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        A obj = new A();
        obj.helloWorld();

    }

}

Notice how android studio imports com.example.mylibrary.A instead of com.example.mylibrar.Akt as the reference says. Android studio reports no errors before compiling.

Gradle file for app:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.uisample"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile project(':mylibrary')
}

After running the project, gradle throws this error: "cannot find symbol class A". What am I doing wrong?

like image 368
gesuwall Avatar asked Apr 08 '16 17:04

gesuwall


1 Answers

A few things:

  1. Apply the kotlin-android plugin to your library module build.gradle (see the docs):

    apply plugin: 'kotlin-android'
    

    As it stands now, you're not even telling gradle to compile Kotlin files.

  2. Your buildscript block should probably be at the top-level build.gradle rather than the module build.gradle.

  3. Consider updating the fully released version of Kotlin 1.0.1-2

like image 62
Doug Stevenson Avatar answered Nov 14 '22 02:11

Doug Stevenson