14

Is it even possible to use Kotlin to write a library?

The problem I see is that it will depend on specyfic versions of kotlin-stdlib and kotlin-runtime and force user of the library to use the same version.

I'm considering using jarjar to repackage those but then, how to include those in a fatjar-type distribution? Inside the unpacked aar I see /libs directory - is there a way to use that?

atok
  • 5,880
  • 3
  • 33
  • 62
  • Have a look [here](http://stackoverflow.com/questions/40859766/how-does-android-merge-the-custom-librarys-support-library-with-that-of-applica/40860398#40860398) to understand better how gradle builds the applications. The same depicts plugins on the classpath. I suggest not to build `fatJar` when distributing libraries, this will cause problems with versions. – R. Zagórski Dec 16 '16 at 12:05
  • There are Kotlin-based libraries in the Android Arsenal, so presumably it is possible. I haven't played much with Kotlin personally, and so I do not know how they do it. But unless `kotlin-stdlib` and `kotlin-runtime` are very small, your library may get rather large if you embed them. `libs/` in an AAR are for native libraries (`.so` files) IIRC. – CommonsWare Dec 16 '16 at 12:40
  • 1
    Thank you for your comments! Looks like the existing libraries just include the dependency: `compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"` – atok Dec 19 '16 at 08:25
  • @R.Zagórski: I understand your recomendation, that's why I'm looking into changing the package names. – atok Dec 19 '16 at 08:26
  • 1
    @atok : Why don't you write your comment as an answer. It works. It will be helpful – Ashwin Oct 17 '17 at 05:32

1 Answers1

6

As per the comments, there are several .aar libraries available that are written in Kotlin such as MapMe.

Indeed there is a curated list of such libraries in this GitHub repo

You can see by examining the build.gradle for the same that these do include a dependency on kotlin-stdlib.

This is not a lightweight dependency: the .jar size is approx 1MB. And if you make a blank project, compile the .apk, and upload it to a method count site it comes to 6086 methods.

However, if the consuming project already uses Kotlin they will already have bourne the burden of the extra classes. Since they will have explicitly included kotlin-stdlib as a dependency in their build.gradle it will probably force resolution of the dependency in your library to their version. This is unlikely to cause problems with stdlib.

Even if the consumer is not using Kotlin in their project, aggressive use of Proguard can eliminate unused methods. The official Kotlin website explains:

Footprint: Kotlin has a very compact runtime library, which can be further reduced through the use of ProGuard. In a real application, the Kotlin runtime adds only a few hundred methods and less than 100K to the size of the .apk file.

The source for the second statement is a blog post on Medium so your mileage may vary.

David Rawson
  • 20,912
  • 7
  • 88
  • 124