Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Kotlin in Android library distributed as AAR

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?

like image 400
atok Avatar asked Dec 16 '16 11:12

atok


1 Answers

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.

like image 136
David Rawson Avatar answered Nov 08 '22 06:11

David Rawson