Jack and Android - Lambda runtime failure until cl

2019-03-15 00:59发布

问题:

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.

Example code:

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;

Example runtime exception:

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 build tools 2.2.2
  • Gradle 2.14.1
  • buildToolsVersion 25.0.0
  • sourceCompatibility/targetCompatibility JavaVersion.VERSION_1_8
  • compileSdkVersion/targetSdkVersion 24

回答1:

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.

UPDATE After 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.