我正在使用一些原生的Java 8功能(例如lambda)
测试Android Studio 3.0预览版我有两个模块(app,mylibrary)
在我的应用build.gradle
文件中,我包含了库模块。
implementation project(':mylibrary')
在mylibrary build.gradle
文件中,我指定了java版本以测试lambda表达式。
compileOptions {
android.compileOptions.sourceCompatibility 1.8
android.compileOptions.targetCompatibility 1.8
}
在我将安卓工作室从金丝雀3升级到金丝雀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$0(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$0:(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
?
答案 0 :(得分:1)
这是新的Android Gradle插件的错误 - 我的库项目遇到了同样的问题。似乎desugar
- 将Java 8字节码转换为传统字节码 - 不适用于库项目。
我向谷歌报告了问题。希望它能解决。 https://issuetracker.google.com/issues/63513242