Search code examples
androidgradlekotlinandroid-libraryaar

Adding Kotlin runtime library to aar in android


WHAT I WANT :smile: :

building kotlin library, deliver it as an .aar file Java project that uses my .aar don’t need configure anything, just include my .aar and start playing. first Q : IT THAT EVEN Possible ? cause i’m loosing hope :smile:

if the project that uses my library doesn’t have Kotlin configured, then it says ClassNotFoundException.

-WHY IS THAT ?

if kotlin have the same byte code as Java byte code, (and 100% compatible),

then why i need to have kotlin when using .aar writen in kotlin in a JAVA Project ?

After some reaserch, i discovered that i must include kotlin runtime library in my project but i don’t know how, i’ve allready tried basically all the solution overs the net ,

i think fat aar isn’t supported on Android,

Thank You All for your attention.

Update in my aar project i have a module with the following build.gradle

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android' 

/////
.
.

dependencies {
////
.
.

    api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

}

in my application that uses the .aar i have the following in project build.gradle

 dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
    }


in the build.gralde module

    implementation(name: 'my-aar-library', ext: 'aar')

and when i run the app, it crash and here is the stack :


09-25 15:14:22.814 3239-3239/com.example.mymodule E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mymodule, PID: 3239
    java.lang.NoClassDefFoundError: kotlin.jvm.internal.Intrinsics
        at com.com.example.mymodule.views.MyCustomView.<init>(PCMajorHeadereView.kt)
        at .
    .
    .
    .
    .
    .

UPDATE 2 : PS :

clearly i must add the kotlin runtime-library to my .aar i tried all over the net, it doesn’t work :'(

Final Update : solution found thanks to cilling, note that you must include the runtime-library into the local maven repo, it can't access online content

Thnx for all


Solution

  • The problem is that your aar doesn't include dependency tree (.pom file), so Gradle is not able to download them during the sync.

    So, what's the proper solution? You should use repository manager, like Maven. You can see @Robyer post how to include all dependencies and publish it on MavenLocal: https://stackoverflow.com/a/42160584/7508302 That post is about providing source code for library, but there is also ready to use gradle publish script.

    Then, in your 'local maven' a library will be published. And then, you can add to your gradle file (in project you want to use that library): repositories { mavenLocal() } and then add dependecy like this:

    implementation ('com.example.android:library:0.0.1-SNAPSHOT@aar') {
        transitive = true
    }
    

    Full instruction:

    1) In your library add a gradle file responsible for publishing on mavenLocal(). See https://stackoverflow.com/a/42160584/7508302 for details and ready to use script.

    2) Push the library to mavenLocal. It's a local maven repository. You don't need to install anything, as the maven repository just has to have proper dir structure.

    3) Check mavenLocal dir. There should be a dir tree with your domain name, for example: com -> mycompany -> library -> 0.0.1 and in that folder you should find .pom file. Open it, to see dependencies of your library.

    4) Add mavenLocal() to your repository section in project level gradle file. Note, that mavenLocal just points to some place in your files.

    5) Add library dependency using just qualified name (for example: com.mycompany:library:0.0.1@aar. Add parameter transitive if you want to fetch transitive dependencies (that transitive parameter means that that library may depend on other modules).

    That way gradle should fetch declared dependencies and include them to project.