是否有方法使用Java 8特性与Android库项目?

时间:2023-01-18 23:39:53

I followed the Android Java 8 Features manual. It works well for Android application project. But when I try to use it with Android library project I get

我遵循了Android Java 8的特性手册。它适用于Android应用项目。但当我尝试使用它时,我得到了Android程序库项目。

Error:Library projects cannot enable Jack. Jack is enabled in default config.

Partial solution: I enabled lamdba expression with Gradle Retrolambda Plugin.

部分解决方案:我使用Gradle Retrolambda插件支持lamdba的表达式。

2 个解决方案

#1


17  

I had the same issue and tried different approaches. It now works for me without using retrolambda (which produced some weird error during runtime). Also Jack is not active for the same reason you already mentioned. There is an interesting bug post at google.com about this topic: https://code.google.com/p/android/issues/detail?id=211386

我有同样的问题,尝试了不同的方法。它现在对我有效,而不使用retrolambda(在运行时产生了一些奇怪的错误)。杰克也不像你刚才提到的那样活跃。在google。com上有一个关于这个主题的有趣的bug帖子:https://code.google.com/p/android/addrees/detail? id=211386。

Here is my build.gradle script, I used the workaround from the bug post to fix the "MethodType not found" exception during compilation.

这是我的建立。gradle脚本,我使用了来自bug post的变通方法来修复编译期间的“MethodType not found”异常。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}
apply plugin: 'com.android.library'

repositories {
    mavenCentral()
}

// Java8 not fully supported in library projects yet, https://code.google.com/p/android/issues/detail?id=211386
// this is a temporary workaround to get at least lambdas compiling
gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xbootclasspath/a:" + System.properties.get("java.home") + "/lib/rt.jar"
    }
}

android {
    compileSdkVersion 24
    buildToolsVersion "24"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 24
    }

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

#2


0  

This sure has been a long ride. I tried all possible combinations of the gradle plugin, the experimental gradle plugin + retrolambda + Jack etc, but with no luck. Until now. From Android Studio 3.0 Preview 1 or later (and, consequently, the Android Gradle plugin 3.0.0-alpha1 or later), the Jack toolchain is deprecated and replaced by some new bytecode transformation - sugaring, used in conjunction with the standard javac compiler.

这确实是一次长途旅行。我尝试了所有可能组合的gradle插件,实验性的gradle插件+ retrolambda + Jack等等,但是没有运气。直到现在。从Android Studio 3.0预览1或以后(因此,Android Gradle插件3.0.0-alpha1或更高版本),Jack工具链被弃用,取而代之的是一些新的字节码转换——加糖化,与标准javac编译器一起使用。

With this setup I've personally (finally!!!) been successful in using Java 8 features such as lambdas in a library project.

通过这个设置,我个人(终于!!!)成功地使用了Java 8特性,例如在一个库项目中使用lambdas。

This page has all the techy info as well as migration help etc: https://developer.android.com/studio/write/java8-support.html

这个页面包含了所有的技术信息以及迁移帮助等:https://developer.android.com/studio/write/java8-support.html。

#1


17  

I had the same issue and tried different approaches. It now works for me without using retrolambda (which produced some weird error during runtime). Also Jack is not active for the same reason you already mentioned. There is an interesting bug post at google.com about this topic: https://code.google.com/p/android/issues/detail?id=211386

我有同样的问题,尝试了不同的方法。它现在对我有效,而不使用retrolambda(在运行时产生了一些奇怪的错误)。杰克也不像你刚才提到的那样活跃。在google。com上有一个关于这个主题的有趣的bug帖子:https://code.google.com/p/android/addrees/detail? id=211386。

Here is my build.gradle script, I used the workaround from the bug post to fix the "MethodType not found" exception during compilation.

这是我的建立。gradle脚本,我使用了来自bug post的变通方法来修复编译期间的“MethodType not found”异常。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}
apply plugin: 'com.android.library'

repositories {
    mavenCentral()
}

// Java8 not fully supported in library projects yet, https://code.google.com/p/android/issues/detail?id=211386
// this is a temporary workaround to get at least lambdas compiling
gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xbootclasspath/a:" + System.properties.get("java.home") + "/lib/rt.jar"
    }
}

android {
    compileSdkVersion 24
    buildToolsVersion "24"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 24
    }

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

#2


0  

This sure has been a long ride. I tried all possible combinations of the gradle plugin, the experimental gradle plugin + retrolambda + Jack etc, but with no luck. Until now. From Android Studio 3.0 Preview 1 or later (and, consequently, the Android Gradle plugin 3.0.0-alpha1 or later), the Jack toolchain is deprecated and replaced by some new bytecode transformation - sugaring, used in conjunction with the standard javac compiler.

这确实是一次长途旅行。我尝试了所有可能组合的gradle插件,实验性的gradle插件+ retrolambda + Jack等等,但是没有运气。直到现在。从Android Studio 3.0预览1或以后(因此,Android Gradle插件3.0.0-alpha1或更高版本),Jack工具链被弃用,取而代之的是一些新的字节码转换——加糖化,与标准javac编译器一起使用。

With this setup I've personally (finally!!!) been successful in using Java 8 features such as lambdas in a library project.

通过这个设置,我个人(终于!!!)成功地使用了Java 8特性,例如在一个库项目中使用lambdas。

This page has all the techy info as well as migration help etc: https://developer.android.com/studio/write/java8-support.html

这个页面包含了所有的技术信息以及迁移帮助等:https://developer.android.com/studio/write/java8-support.html。