Replace Retrolambda with Android Studio 3.0 Java 8 built-in features

3.8k views Asked by At

In my project I'm using popular library retrolambda. I've just downloaded new Android Studio 3.0 Canary 1.

I've updated my project to use new version of Gradle etc. And everything is OK.

What's new in Android Studio 3 is built in support for some Java8 features. New AS3 is suggesting to remove retrolambda and use these features.

Retrolambda warning

I have removed retrolambda, Gradle build was successful but app is crashing with this error (in a place where there is lambda)

E/UncaughtException: java.lang.NoSuchMethodError: No static method lambda$replace$2

I'm using RxJava2 in my project. I'm not sure this is related with it, but it looks like in my case built-in features for Java8 are not working. Maybe I need to set something "somewhere"?

My project settingsenter image description here

My Gradle files

Root project

   dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
        classpath 'com.google.gms:google-services:3.0.0'
        //classpath 'me.tatarka:gradle-retrolambda:3.6.1'
    }

App module

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
    }
}

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }
}


apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.hugo'

...



    compile 'com.android.support:multidex:1.0.1'
    compile 'com.google.firebase:firebase-analytics:9.8.+'
    compile 'com.google.firebase:firebase-crash:9.8.+'
    compile 'com.google.android.gms:play-services-maps:9.8.+'
    compile 'com.google.android.gms:play-services-analytics:9.8.+'
    compile 'com.google.android.gms:play-services-auth:9.8.+'
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
    //Support Library

(...)

    compile 'com.squareup.retrofit2:retrofit:2.2.0'
    compile 'com.squareup.retrofit2:converter-gson:2.2.0'
    compile 'com.google.maps.android:android-maps-utils:0.4'

    /* RXJAVA2 */
    compile 'io.reactivex.rxjava2:rxjava:2.0.6'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
    compile 'com.github.VictorAlbertos:ReactiveCache:1.1.0-2.x'
    compile 'com.github.VictorAlbertos.Jolyglot:gson:0.0.3'


android {


    compileSdkVersion 25
    buildToolsVersion '25.0.2'
    defaultConfig {
        applicationId "my_app_id"
        minSdkVersion 15
        targetSdkVersion 25
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

(...)

    dexOptions {
        javaMaxHeapSize "4g"
    }
    lintOptions {
        abortOnError false
    }

    }

    buildTypes {
        debug {
            minifyEnabled false
            shrinkResources false
        }

        debugpro {
            minifyEnabled true
            shrinkResources false
            proguardFile file('proguard-project.txt')
            proguardFile file('proguard-google-api-client.txt')
            //noinspection GroovyAssignabilityCheck
            signingConfig signingConfigs.debug
        }

        release {
            minifyEnabled true
            shrinkResources false
            proguardFile file('proguard-project.txt')
            proguardFile file('proguard-google-api-client.txt')



        }
        releaseci {
            minifyEnabled true
            shrinkResources false
            proguardFile file('proguard-project.txt')
            proguardFile file('proguard-google-api-client.txt')
            //noinspection GroovyAssignabilityCheck
            signingConfig signingConfigs.releaseci
        }


(...)

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }




}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.jakewharton.butterknife'
1

There are 1 answers

7
Stefan Zobel On

This is probably caused by a bug in the Gradle Java 8 language feature desugaring that is tracked in https://issuetracker.google.com/issues/62456849

desugar seems to blindly rename the synthetic methods in a class file if their name starts with lambda$ (appending the owner class name) regardless of whether a reference to that method already exists in the bytecode (and that reference doesn't get renamed too).

When the code path hits such a reference at runtime the obvious result is a NoSuchMethodError because a method with that name doesn't exist anymore.