Android njava 8特性(Jack 编译器)和 Kotlin 互操作系统

更新3。 KOTLIN 是 现在正式支持 ANDROID 开发。由谷歌。 yaaaaas!

更新2 : 它看起来像 从长远来看,jetBrains 确实致力于为 Kotlin 的安卓系统提供支持。我是一个快乐的 kotlin 用户:)。

更新 : 来自 JetBrains,提到他们将要发布一些关于这个话题的信息的 Hadi Hariri,我会在他们更新后更新这篇文章。


= = = 绝望的东西下一集 = =

Google 刚刚发布了 Android N 的预览版,其中包含了一些有趣的功能,其中最引人注目的是部分 Java8语言支持。这是可能的,因为新的 杰克工具链谷歌正在工作。

目前使用 JavacKotlinc的工具链:
Javac (.java—— > .class)—— > Dx(.class—— > .dex)
Kotlinc (.kt-> .class)-> Dx(.class-> .dex)

新杰克工具链:
杰克 (.java—— > .jack—— > .dex)

我认为 Google 会推动 杰克成为 Android 开发的默认工具链。 更新: 杰克现在是 不赞成

我的问题是,这个新的工具链将如何影响我,在未来,作为一个 Kotlin的 Android 开发用户?我会被“困在过去”吗?

8929 次浏览

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.

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.

disclaimer: I work on Jack

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

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.

@Pavel Dudka

Jack - is a compiler. Similar to javac, but it does a slightly different thing:

enter image description here

As you can see, Jack compiles Java source code straight into Dex file! We don't have intermediate *.class files anymore, so dx tool is not needed!

But wait! What if I include a third-party library in my project (which comes as a collection of .class files)?

And that's when Jill comes into play:

enter image description here

Jill can process class files and transform them into special Jayce format which can be used as an input for Jack compiler.

So now let's step aside for a second and think... What is going to happen to all those cool plugins we got so addicted to? They all need .class files and Jack compiler doesn't have those anymore...

Luckily, Jack provides some of those important for us features out of the box:

  • Retrolambda - will not be needed. Jack can handle lambdas properly
  • Proguard - it is baked into Jack now, so you can still use obfuscation and minimization

Advantages:

Jack supports Java programming language 1.7 and integrates additional features described below.

  • Predexing

    When generating a JACK library file, the .dex of the library is generated and stored inside the .jack library file as a pre-dex. When compiling, JACK reuses the pre-dex from each library. All libraries are pre-dexed.

  • Incremental compilation

    Incremental compilation means that only components that were touched since the last compilation, and their dependencies, are recompiled. Incremental compilation can be significantly faster than a full compilation when changes are limited to only a limited set of components.

  • Repackaging

    JACK uses jarjar configuration files to do the repackaging.

  • Multidex support

    Since dex files are limited to 65K methods, apps with over 65K methods must be split into multiple dex files. (See ‘Building Apps with Over 65K Methods’ for more information about multidex.)

Disadvantages:

  • Transform API is not supported by Jack - there is no intermediate Java bytecode you can modify, so some plugins I didn't mention here will stop working
  • Annotation processing is not currently supported by Jack, so if you heavily depend on libraries like Dagger, AutoValue, etc., you should think twice before switching to Jack. EDIT: As pointed out by Jake Wharton, Jack in N Preview has annotation processing support, but it is not exposed yet through Gradle.
  • Lint detectors which operate on a Java bytecode level are not supported.
  • Jacoco is not supported - well, I personally find Jacoco questionable (it doesnt really show what you want to see), so can totally live without it
  • Dexguard - enterprise version of Proguard is not currently supported

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.

According to the Kotlin blog, release 1.1-beta2 New Features section:

Support for building Android projects when the Jack toolchain is enabled (jackOptions { true });

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