13

I'm considering porting a Java (Android) library to Kotlin. I really like Kotlin and the benefits over Java should reduce the number of bugs in the library.

Since the library is targeting a resource constrained environment I'm worried that by porting the library to Kotlin there will be extra overhead.

Does a Kotlin library (distributed as .class files) introduce any runtime or extra overhead versus a Java library?

Will the resulting Android app be any larger or slower because of Kotlin?

Marc O'Morain
  • 3,699
  • 5
  • 28
  • 34
  • 1
    You are right there is a trade off, kotlin with stdlib could be an overkill for small libraries, but for big ones could make sence, since kotlin stdlib has many usefull api's. You could write your library in java and provide handy Kotlin api's/extensions via separate lib like RxJava and RxKotlin for example. – Stepango Sep 22 '16 at 09:25
  • Thanks for the suggestion, but the point is that I want to write the library in Kotlin - I want immutable data classes and null-safety. – Marc O'Morain Sep 22 '16 at 09:27
  • @MarcO'Morain java supports immutable classes too, you could provide null-safety in java via Nullable/NonNull annotations. If you want to use kotlin why are you worried about stdlib size? – Stepango Sep 22 '16 at 09:32
  • 1
    > If you want to use kotlin why are you worried about stdlib size? @Stepango I don't see the dichotomy here. I want to use Kotlin for the improved syntax and compile-time safety. It's an improvement over Java in every way. I don't want to increase the APK size of apps that use my library any more than necessary. I don't want to add any transitive dependencies to client apps. – Marc O'Morain Sep 22 '16 at 09:42

2 Answers2

8

Kotlin has many optimizations specifically that help Android. If you read through blog posts you can see how it has constantly reduced stdlib size, and amount of classes at every release.

Will the resulting Android app be any larger or slower because of Kotlin?

No

Does a Kotlin library (distributed as .class files) introduce any runtime or extra overhead versus a Java library?

Standard library is very small and many of its functions are inline-only which mean they don't exist past compilation and just become inline code. Proguard can take care of a lot as well.

Since the library is targeting a resource constrained environment I'm worried that by porting the library to Kotlin there will be extra overhead.

You didn't define which resources are constrained. Kotlin isn't going to use more memory, not going to use more disk, and stdlib is tiny.

I really like Kotlin and the benefits over Java should reduce the number of bugs in the library.

Kotlin is popular on Android for a reason, and you should take that as a sign that there are many more benefits than negatives. Really you can see this for yourself by having a small example in Java and small in Kotlin and compare the difference. Proguard both, have the same type of build pipeline.

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
  • Great answer. Thanks. When I say resource constrained I mean an Android device in general - the the smaller APK size the better (faster download, less storage space, smaller memory footprint). I want to reduce memory usage of my library as much as possible to be a good citizen. – Marc O'Morain Sep 22 '16 at 09:58
  • 1
    The only hit you take is total class/method from stdlib, and Kotlin continues to work on ways to reduce them. But you make up for those points by having less of your own code, using less other libraries, and with some of them being inline they may not even count. So the NET change will depend on your useage. There are a lot of published notes about Kotlin and Android, and the impact, and slack channel on Kotlin slack has an #android channel where people can answer subjective things about it. – Jayson Minard Sep 22 '16 at 10:05
  • 4
    i wouldn't say that standard library is very small. Kotlin 1.3 stdlib adds 0.7Mb and it can be a problem for some apps – 4ntoine Jan 16 '19 at 16:29
  • 1
    @4ntoine 1.30 Mb to be precise, for JVM. For example without it a JAR of mine is only 20 Kb. Huge difference. – LppEdd Nov 22 '19 at 08:58
  • 1
    @LppEdd `1.3.0` adds 1.1Mb to be even more precise: https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib/1.3.0 – 4ntoine Nov 23 '19 at 18:43
  • @4ntoine yeah I was referring to 1.3.6. – LppEdd Nov 23 '19 at 18:56
0

The link at 5747 above goes to a porn site and should be removed.

"There is the Kotlin standard library, which has a method count of about 5747, though that may not matter if the client is using the stdlib anyway. – nhaarman Sep 22, 2016 at 8:56"

mpatch54
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31710592) – aemaem May 12 '22 at 08:10