Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 956
adek Avatar asked May 19 '17 11:05

adek


People also ask

Which Gradle version support Java 8?

Android Gradle plugin 3.0. 0 and later support all Java 7 language features and a subset of Java 8 language features that vary by platform version. When building your app using Android Gradle plugin 4.0. 0 and higher, you can use a number of Java 8 language APIs without requiring a minimum API level for your app.

Does Android support Java 8?

Java 8 language features are now supported by the Android build system in the javac/dx compilation path. Android Studio's Gradle plugin now desugars Java 8 class files to Java 7-compatible class files, so you can use lambdas, method references and other features of Java 8.

What is desugaring Android?

Through a process called API desugaring, the DEX compiler (D8) allows you to include more standard language APIs in apps that support older versions of Android.

Which version of Java is used in Android Studio?

A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.


1 Answers

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.

like image 130
Stefan Zobel Avatar answered Sep 19 '22 13:09

Stefan Zobel