如何在 android studio 中启用 Jack(Java Android 编译器工具包)

How to enable Jack (Java Android Compiler Kit) in android studio

我更新了我的 androidstudio 2.1 stable.As per Android Studio 2.1 supports Android N Developer Preview Android studio 2.1 支持 Jack (Java Android Compiler Kit) 编译器。

如何在android工作室添加或使用Jack?

注意:

The Jack toolchain is deprecated, as per Java 8 Language Feature Support on Android. However, you may continue to use it to enable Java 8 language features until the replacement is available.

As of this March 14, 2017 announcement, the Jack toolchain is deprecated. Jack was the default Android build toolchain for Android 6.0–8.1.

https://source.android.com/source/jack

有关使用 Jack 需要什么以及如何使用的详细信息,请参阅 documentation

这是 build.gradle 中有关如何使用 jackOptions 并为 java 1.8 设置 compileOptions 的文档的相关部分。

android {
    ...
    defaultConfig {
        ...
        jackOptions {
            enabled true
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

更新

根据 this post,Jack 工具链现在被认为已弃用,并且正在开展工作以在未来几周内原生支持 Java 8 个功能作为 Android 构建系统的一部分到 post.

post 还提到,如果您仍想尝试使用 Jack 启用 Java 8 个功能,那么从 Jack 迁移到新方法应该几乎不需要任何工作。

更新 2 预览内置支持

您现在可以使用最新的 Android Studio preview 2.4 preview 6 试用对 Java 8 的新内置支持。

有关如何启用它或从 Jack 或 Retrolambda 迁移的更多信息,请参阅 documentation

您可以通过在 build.gradle 文件中添加以下行来启用 jack 编译器。

android{

compileSdkVersion 23

buildToolsVersion "24rc2"

defaultConfig {
    ...
    jackOptions {
        enabled true
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}}

Jack/Jill近期将被废弃,见Googlepost。 https://android-developers.googleblog.com/2017/03/future-of-java-8-language-feature.html

插入块 android{}

jackOptions {
            enabled true
        }

它的解决方案

Java Android Studio 3 版本支持 8,所有这些 jackOptions 不再需要。

Jack is no longer supported, and you should first disable Jack to use the improved Java 8 support built into the default toolchain.

有关详细信息,请阅读此 link:

https://developer.android.com/studio/write/java8-support.html

1- build.gradle(模块)

defaultConfig {
        //remove jackOptions 
        jackOptions {
            enabled true
        }
    }

2- 如果您使用使用 Java 8 的第三方 build.gradle(项目)

buildscript {
    dependencies {
        //remove this line of your third-party dependency
        classpath 'PATH<VERSION>'
    }
}

3-删除 retrolamda,应用插件 '....labmda' remove these lines from your module gradle

4- 添加到 build.gradle(模块)

android {

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}