Android N Java 8 features (Jack compiler) and Kotl

2019-01-30 02:27发布

Update 3. KOTLIN IS NOW OFFICIALLY SUPPORTED FOR ANDROID DEVELOPMENT. BY GOOGLE. YAAAAAAAAS!

Update 2: It looks like JetBrains is really committed to supporting Kotlin for Android in the long run. I'm a happy kotlin user :).

Update: Hadi Hariri, from JetBrains, mentioned they're going to release some info on this topic. I will update this post once they do.


=== DEPRECATED STUFF NEXT ===

Google just released a preview for the upcoming Android N with some interesting features, the most notable being partial Java 8 language support. This is possible due to the new Jack toolchain Google is working on.

The current toolchain using javac or kotlinc:
javac (.java --> .class) --> dx (.class --> .dex)
kotlinc (.kt --> .class) --> dx (.class --> .dex)

New Jack toolchain:
Jack (.java --> .jack --> .dex)

I am assuming Google will push forward towards making Jack the default toolchain for Android development. Update: Jack is now deprecated. Yas.

My question is how will this new toolchain affect me, in the future, as a kotlin user for Android development? Will I get "stuck in the past"?

8条回答
smile是对你的礼貌
2楼-- · 2019-01-30 03:04

UPDATE (03/16/2017)

Luckily, Jack is dead and so it won't affect Kotlin developers.


If Jack is the future then you will get stuck in the past with Kotlin. Currently Jack doesn't support plugins that can compile non-Java source into Dalvik bytecode. And even if it did JetBrains would need to add a new backend to the Kotlin compiler which is not a trivial task. So you will have to use Kotlin with Jill and it's going to be something very similar to the toolchain you use now.

As you can see in the image below even if it's impossible to explicitly turn off Jack you'll still be able to convert the project to a library project to use Jill. And the application project will just reference this library project.

Jack and Jill Application Build

The only way I see how Kotlin can work with Jack, which probably will not be implemented, is adding a Java backend to the Kotlin compiler, i.e. a backend that generates Java code like Xtend. In this case code generated by the Kotlin compiler can be processed by Jack as any other Java code.

But at the moment we don't know exactly what Jack will support when it's released. Maybe something will change dramatically and adding Kotlin support to Jack will become possible.

查看更多
forever°为你锁心
3楼-- · 2019-01-30 03:05

Google is not going to push Jack as the default tool, but Jack and Jill.
Compiling .class files to dex with Jill is here to stay. Otherwise, you can say goodbye to jar/aar libraries.

Whether Jack or Jill will be slower is still up for debate. The Android team hopes that jack will be faster than the current build process, but that's not the case right now

Furthermore, Jack and Dex are available in the open, nothing prevents the kotlin team from writing a tool emiting .jack or .dex files from kotlin sourcecode.

查看更多
劫难
4楼-- · 2019-01-30 03:13

As per latest google announcement -

We've decided to add support for Java 8 language features directly into the current javac and dx set of tools, and deprecate the Jack toolchain. With this new direction, existing tools and plugins dependent on the Java class file format should continue to work. Moving forward, Java 8 language features will be natively supported by the Android build system. We're aiming to launch this as part of Android Studio in the coming weeks, and we wanted to share this decision early with you.

We initially tested adding Java 8 support via the Jack toolchain. Over time, we realized the cost of switching to Jack was too high for our community when we considered the annotation processors, bytecode analyzers and rewriters impacted. Thank you for trying the Jack toolchain and giving us great feedback. You can continue using Jack to build your Java 8 code until we release the new support. Migrating from Jack should require little or no work.

So we need not worry about jack toolchain becoming default toolchain for android development. You can continue to use kotlin or use normal javac/dx set of tools.

Source : Future of Java 8 Language Feature Support on Android

查看更多
不美不萌又怎样
5楼-- · 2019-01-30 03:18

disclaimer: I work on Jack

This won't affect you. Kotlin's compiler produces Java 6 bytecode, which Jack/Jill can import just fine.

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-30 03:18

I've already found this blog post from the official Kotlin's blog: : Kotlin’s Android Roadmap

There you would find a part which tells that:

The next thing we plan to do to improve Android build performance is providing an integration with Android’s new Jack and Jill toolchain. Right now there are some issues that prevent Jack from handling Kotlin-generated bytecode correctly (196084 and 203531), but we plan to work together with the Google team to either resolve the issues or provide workarounds on our side. Once this is done, we’ll be able to translate only changed class files using Jill during incremental compilation, as opposed to translating all class files every time (which is the only possible behavior in the old Android tooling).

So as @LukasBergstrom said, there won't be any problem with "stucking in the past" ;-)

You can also check Reddit discussion linked with this topic: What is the status of Kotlin with Jack and Jill?

Happy coding.

查看更多
干净又极端
7楼-- · 2019-01-30 03:20

As said in the blog post (Kotlin's Android Roadmap) that appeared today:

Right now there are some issues that prevent Jack from handling Kotlin-generated bytecode correctly (196084 and 203531), but we plan to work together with the Google team to either resolve the issues or provide workarounds on our side. Once this is done, we’ll be able to translate only changed class files using Jill during incremental compilation, as opposed to translating all class files every time (which is the only possible behavior in the old Android tooling).

So Kotlin will eventually support Jack & Jill and get benefits from it.

查看更多
登录 后发表回答