1

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

  • 1
    you don't have to add any library into another library, this is non-sense. libraries are meant to be modular. – Martin Zeitler Sep 26 '19 at 10:25
  • yes but how can i add the dependency ? if my library depend on kotlin-runtime library the user need only to include my library and somehow the inner depence will be charged automatically and not manually – Anonyme Player Sep 26 '19 at 11:19
  • @MartinZeitler, that's not true. Libraries may have dependencies And they mostly have. Try run `./gradlew :app:dependencies` to see both, project and internal library's dependency list. You may add them to project as a single dependency because gradle is resolving them. – Cililing Sep 26 '19 at 13:59
  • @Cililing doesn't this imply not baking them into the package? You probably should first read, understand and then comment, because I've never claimed, that a library cannot have dependencies. – Martin Zeitler Sep 26 '19 at 14:27

2 Answers2

1

Call the below call for smile.aar file in build.gradle file.

implementation project(':smile)

Assuming that smile is the .aar file name.

If you want to run Kotlin you must include following in project build.gradle

buildscript {
    ext.kotlin_version = '1.3.31'
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

and also include these in app level build.gradle

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


//in dependencies
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  • exactly, what i am trying to do, is including the dependencies and plugins somehow in my .aar file in the end these are just files and dependencies, can't i include them in my aar file ? – Anonyme Player Sep 26 '19 at 11:17
  • Adding library dependencies to parent is not a good solution. Gradle is able to resolve them automatically if the `.pom` is provided. – Cililing Sep 26 '19 at 12:02
1

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.

Cililing
  • 4,303
  • 1
  • 17
  • 35
  • i don't undrestand, can you please provide some details, if i use api 'kotlin-runtime' in my library and then include my library using api keyword again in any project why won't gradle fetch the kotlin-runtime library ? – Anonyme Player Sep 26 '19 at 13:38
  • Gradle is smart enough to fetch only dependencies that are not used in project. So, if you declare a library's dependency in app it should work - but it's not the right way, as the library have a possibility to declare its own dependencies. That's the reason why you should use Maven. The trick maven does is providing dependency list with the library (in `.pom` file I mentioned before). Library's user may be aware of library's dependecies (and actually he should be) - it's up to gradle to get library dependencies with the library. – Cililing Sep 26 '19 at 13:51
  • THANK You so much man, i get the idea and it's way more clear now, i still can't run my app but i think it's a whole another problem, i now did published my library into a local maven but still not able to run my app – Anonyme Player Sep 27 '19 at 07:35
  • Unable to resolve dependency for ':app@debug/compileClasspath': – Anonyme Player Sep 27 '19 at 07:35
  • Helped me a lot also, thank you. I was stuck on 1 and 2 options, but now it works. It should be explained a bit more. To use that publishMavenLocal.gradle from 1 it needs to add mavenLocal() to repositories in your main(project) build.gradle. And then add publishMavenLocal.gradle to library project and put this line at the bottom of library build.gradle: apply from: 'publishMavenLocal.gradle' After that it will work as expected. – Evgen Oct 04 '19 at 15:57
  • And then just use implementation 'com.mycompany:library:0.0.1' in dependencies section of app-module build.gradle – Evgen Oct 04 '19 at 15:59