如何处理 Android 中不同的 JAVA 版本?

How to handle different JAVA version in Android?

我正在测试 Android Studio 3.0 预览版和一些本机 java 8 功能(例如 lambda)

我有两个模块(app,mylibrary)

在我的应用 build.gradle 文件中,我包含了库模块。

implementation project(':mylibrary')

在 mylibrary build.gradle 文件中,我指定 java 版本以测试 lambda 表达式。

compileOptions {
    android.compileOptions.sourceCompatibility 1.8
    android.compileOptions.targetCompatibility 1.8
}

在我将 android 工作室从 Canary 3 升级到 Canary 5 之前,它对我来说工作正常,我的方法被打破了。

我收到以下动态错误消息

Information:Gradle tasks [:app:assembleDebug]
/Users/chchi/Documents/PayPal/Android/AS3.0Preview/app/src/main/java/com/example/chchi/myapplication/MainActivity.kt
Error:Error converting bytecode to dex:
Cause: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
Error:com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)
...
...
Error:  ... 5 more
Error:...at bytecode offset 00000016
Error:locals[0000]: Lcom/android/tools/fd/runtime/IncrementalChange;
Error:locals[0001]: <invalid>
Error:...while working on block 0016
Error:...while working on method testJava8feature:()V
Error:...while processing testJava8feature ()V
Error:...while processing com/example/mylibrary/LamdaTest.class
Error:Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Unable to convert input to dex archive.
Information:BUILD FAILED in 8s
Information:40 errors
Information:1 warning
Information:See complete output in console

在我按照说明将 minSdkVersion 撞到 26 之后。我收到有关转换为 dex 文件的以下错误。

Information:Gradle tasks [:app:assembleDebug]
Error:Error converting bytecode to dex:
Cause: Shouldn't happen
Error:com.android.dex.util.ExceptionWithContext: Shouldn't happen
Error:  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Error:  at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
Error:  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
Error:  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
...
Error:Caused by: com.android.dex.util.ExceptionWithContext: Shouldn't happen
Error:  at com.android.dex.util.ExceptionWithContext.withContext(ExceptionWithContext.java:45)
Error:  at com.android.dx.dex.file.MixedItemSection.placeItems(MixedItemSection.java:320)
Error:  at com.android.dx.dex.file.DexFile.toDex0(DexFile.java:611)
Error:  at com.android.dx.dex.file.DexFile.toDex(DexFile.java:242)
Error:  at com.android.builder.dexing.DxDexArchiveBuilder.lambda$processJobChunk[=15=](DxDexArchiveBuilder.java:118)
Error:  ... 5 more
Error:Caused by: java.lang.RuntimeException: Shouldn't happen
Error:  at com.android.dx.dex.file.ValueEncoder.constantToValueType(ValueEncoder.java:276)
Error:  at com.android.dx.dex.file.ValueEncoder.writeConstant(ValueEncoder.java:137)
Error:  at com.android.dx.dex.file.ValueEncoder.writeArray(ValueEncoder.java:310)
Error:  at com.android.dx.dex.file.CallSiteItem.place0(CallSiteItem.java:63)
Error:  at com.android.dx.dex.file.OffsettedItem.place(OffsettedItem.java:242)
Error:  at com.android.dx.dex.file.MixedItemSection.placeItems(MixedItemSection.java:311)
Error:  ... 8 more
Error:...while placing call site{method-handle{invoke-static,method{java.lang.invoke.LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;}}, string{"adder"}, proto{()Lcom/example/mylibrary/PayPalInterface;}, (II)V, method-handle{invoke-static,method{com.example.mylibrary.LamdaTest.lambda$testJava8feature[=15=]:(II)V}}, (II)V}
Error:...while writing section 12
Error:Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Unable to convert input to dex archive.
Information:BUILD FAILED in 1s
Information:38 errors
Information:0 warnings
Information:See complete output in console

最终,为了让编译器通过,我不得不将我的所有模块设置为 1.8(无需将 minSdkVersion 设置为 26)。有没有办法让我的库模块只使用 1.8 并且使用我的库的人仍然可以留在他们现在的位置 (1.7)?

如果我只使用具有 high backward compatibility 的某些功能,那么我为什么要担心谁使用我的库目标 1.8

这是新 Android Gradle 插件的错误 - 我的库项目面临同样的问题。似乎 desugar - 将 Java 8 字节码转换为遗留字节码 - 不适用于库项目。

我已将问题报告给 google。希望它修复。 https://issuetracker.google.com/issues/63513242