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

1

15

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'
Picker answered 19/5, 2017 at 11:1 Comment(9)
Show gradle files pleaseDovelike
@TimCastelijns updatedPicker
can you show the code that crashesDovelike
Make sure you remove all the old code from the annotation preprocessor. Theoretically a "clean" will do this, but I like to also do a: rm -rf build app/build to completely clean things out.Symphonist
@JimAndreas I did clean, rebuild many times. Didn't work. I've got some additional logs that this could be related with ReactiveCache lib. I'm not sure this is it, I posted bug report on github.Picker
@Picker this rather sounds like a botch where the synthetic method for "replace" did not get created - either at compile time or maybe runtime. Hard to say without seeing the code that crashes.Symphonist
Same error with RxLocation lib! Have you able to solve this ?Glabrate
@MohamedALOUANE unfortunatelly no.Picker
@Mohamed ALOUANE See my answer and my comment on github.com/patloew/RxLocation/issues/37Leffen
L
4

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.

Leffen answered 6/7, 2017 at 14:10 Comment(7)
Has this been solved coz. I am also facing this issue ? What is the solution to this desugaring problem ? @stefanzobelCleft
@Akshay Mahajan Yes, this was fixed in Beta 1.Leffen
You mean to say classpath 'com.android.tools.build:gradle:3.0.0-beta1' @stefanzobel ?Cleft
@Akshay Mahajan Why use such an old version? The final release of AS 3.0 has been available for some time now. I use com.android.tools.build:gradle:3.0.0Leffen
I am using same com.android.tools.build:gradle:3.0.0 and AS 3.0, but no help. Am i missing something here ? @stefanzobelCleft
@Akshay Mahajan Ask a new question (maybe linking to this one) and describe your problem in detail. It's impossible to judge from afar what your problem is.Leffen
Let us continue this discussion in chat.Cleft

© 2022 - 2024 — McMap. All rights reserved.