Search code examples
javaandroidkotlinaar

I'm not able to use top level functions from my custom Kotlin library


I wanted to create a kotlin library for android that contains a few top-level functions.

I've created an android library project.

I've added a few top-level functions and a few classes

// filename TestMethodFirst.kt
fun testMethodFirst() {}
// filename TestClass.kt
class TestClass() {}

I've built the library with the ./gradlew build

I've included this library to another project as an aar library

The classes from the library were resolved and imported without a problem. The top-level functions were unresolved so they couldn't be used. I've tried to add import manually but it wasn't working either. When I tried to use top-level functions from the java, as a static method execution, then it was working.

This doesn't work

// filename Test1.kt
fun test1() {
    testMethodFirst()
}

This works

// filename Test2.kt
fun test1() {
    TestClass()
}

This works

// filename Test3.java
class Test {
    void test() {
        TestMethodFirstKt.testMethodFirst();
    }
}

I've tried it with the AS 4.1 Canary 7 and with AS 3.6.2

Attachments contain the library project and the aar library

Project

Generated aar


Solution

  • For Android Studio 4.1 Canary, this is a known bug, until Canary 9, fixed in Canary 10. Based on this comment, try adding:

    android {
        packagingOptions {
            excludes -= "/META-INF/*.kotlin_module"
        }
    }
    

    That worked for me, and a more permanent fix is slated for Canary 10.