问题描述
看似随机,Android Studio 安装的 apk 已损坏.它通常以 lambda 为中心(但每次都是不同的).我正在使用 Jack 编译和最新版本的所有内容(在最后列出).问题一直存在,直到我进行了干净的构建(然后一切正常).间歇性故障是一种新现象.使用 Jack 和 Java 8 特性一段时间以来,情况稳定.不幸的是,我无法确定在不稳定的故障开始之前发生了什么变化.
Seemingly at random, the apk that Android Studio installs is corrupted. It usually centers around lambdas (but it's a different one every time). I'm using Jack compilation and the latest version of everything (listed at the end). The problem persists until I do a clean build (and then everything's fine). The intermittent failures are a new phenomena. Things were stable with Jack and Java 8 features for awhile. Unfortunately, I can't pinpoint what changed before the flaky failures began.
示例代码:
private static final Ordering<Foo> ORDER_BY_FOO =
Ordering.natural().onResultOf(Foo::getId);
实际问题分配(在番石榴代码中):
Actual problem assignment (in Guava code):
ByFunctionOrdering(Function<F, ? extends T> function, Ordering<T> ordering) {
this.function = checkNotNull(function); // <-- Fails
[...]
class ByFunctionOrdering<F, T> extends Ordering<F> {
final Function<F, ? extends T> function;
运行时异常示例:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mypackage, PID: 32222
java.lang.ExceptionInInitializerError
at com.mypackage.MyFragment.myOtherMethod(MyFragment.java)
at com.mypackage.MainActivity.myMethod(MainActivity.java:144)
at com.mypackage.MainActivity.onActivityResult(MainActivity.java:107)
at android.app.Activity.dispatchActivityResult(Activity.java:6932)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: com.mypackage.-$Lambda$0 cannot be cast to com.google.common.base.Function
at com.google.common.collect.ByFunctionOrdering.<init>(ByFunctionOrdering.java:39)
at com.google.common.collect.Ordering.onResultOf(Ordering.java:416)
at com.mypackage.MyFragment.<clinit>(MyFragment.java:110)
at com.mypackage.MyFragment.myOtherMethod(MyFragment.java)
at com.mypackage.MainActivity.myMethod(MainActivity.java:144)
at com.mypackage.GamesApiManager.onActivityResult(GamesApiManager.java:205)
at com.mypackage.MainActivity.onActivityResult(MainActivity.java:107)
at android.app.Activity.dispatchActivityResult(Activity.java:6932)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
- Android Studio/gradle 构建工具 2.2.2
- Gradle 2.14.1
- 构建工具版本 25.0.0
- sourceCompatibility/targetCompatibility JavaVersion.VERSION_1_8
- compileSdkVersion/targetSdkVersion 24
推荐答案
我认为这是构建工具 25.0.0 和 25.0.1 中的错误.恢复到 24.0.3 为我解决了这个问题.
I assume this is a bug in Build Tools 25.0.0 and 25.0.1. Reverting back to 24.0.3 fixed this issue for me.
更新更新到 com.android.tools.build:gradle:2.2.3
后,我度过了一个完整的工作日,使用 Build Tools v25.0.1 没有构建错误.
UPDATEAfter updating to com.android.tools.build:gradle:2.2.3
, I had a full work-day without build errors with Build Tools v25.0.1.
这篇关于Jack 和 Android - Lambda 运行时失败,直到干净构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!