117

I have an issue with my app that when I log in, the app crashes and I get the error:

java.lang.NoSuchMethodError: No static method metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; in class Ljava/lang/invoke/LambdaMetafactory; or its super classes (declaration of 'java.lang.invoke.LambdaMetafactory' appears in /apex/com.android.runtime/javalib/core-oj.jar)
  at okhttp3.internal.Util.<clinit>(Util.java:87)
  at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Util.java:321)
  at okhttp3.HttpUrl$Builder.parse(HttpUrl.java:1313)
  at okhttp3.HttpUrl.get(HttpUrl.java:917)
  at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:492)
  at com.example.usub.Remote.RetrofitClient.getClient(RetrofitClient.java:14)
  at com.example.usub.Common.Common.getGoogleAPI(Common.java:10)
  at com.example.usub.Welcome.onCreate(Welcome.java:208)
  at android.app.Activity.performCreate(Activity.java:7825)
  at android.app.Activity.performCreate(Activity.java:7814)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
  at android.os.Handler.dispatchMessage(Handler.java:107)
  at android.os.Looper.loop(Looper.java:214)
  at android.app.ActivityThread.main(ActivityThread.java:7356)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I have tried to mess with the gradle and change variables. The issue seems to stem from this package:

import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RetrofitClient {
    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseURL)
    {
        if(retrofit == null)
        {
            retrofit = new Retrofit.Builder()
                         .baseUrl(baseURL)
                         .addConverterFactory(ScalarsConverterFactory.create())
                         .build();
        }
        return retrofit;
    }
}

It then gives to another reference to:

import com.example.usub.Remote.IGoogleAPI;
import com.example.usub.Remote.RetrofitClient;

public class Common {
    public static  final String baseURL = "https://maps.googleapis.com";

    public static IGoogleAPI getGoogleAPI()
    {
        return RetrofitClient.getClient(baseURL).create(IGoogleAPI.class);
    }
}

Here is my gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.example.usub"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0-alpha01'
    implementation 'com.firebase:geofire-android:2.1.2'
    implementation 'com.github.kmenager:material-animated-switch:1.2.2'
    implementation 'com.squareup.retrofit2:retrofit:2.7.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.firebase:firebase-database:19.2.0'
    implementation 'com.google.firebase:firebase-auth:19.2.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.github.d-max:spots-dialog:0.7@aar'
    implementation "androidx.cardview:cardview:1.0.0"
    implementation 'uk.co.chrisjenx:calligraphy:2.3.0'
    implementation 'com.rengwuxian.materialedittext:library:2.1.4'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    //implementation 'com.google.android.gms:play-services:17.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.firebase:firebase-analytics:17.2.1'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
}

apply plugin: 'com.google.gms.google-services'

How can I find the method to fix the error?

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Stradtdog
  • 1,490
  • 2
  • 7
  • 13
  • I had already added the Java 8 options in build.gradle and still had the issue. What worked for me is to move .addConverterFactory( ) before calling baseUri() – png Mar 09 '21 at 17:49

5 Answers5

354

Try to switch to Java 8 compatibility mode, for proper de-sugaring of some library:

android {

    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Besides, Google JSON API might rather need the GsonConverterFactory.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • 11
    According to documentation https://square.github.io/okhttp/upgrading_to_okhttp_4/ it's also recommended to add android { kotlinOptions { jvmTarget = "1.8" } } – Mikhail Sharin Mar 20 '20 at 07:55
  • 6
    @MikhailSharin This only applies when trans-piling from Kotlin to Java byte-code; so that's only required for Kotlin projects (or mixed projects), but it's not required for pure Java projects. Thanks for noticing, because I'm sure this may eventually help someone. – Martin Zeitler Apr 02 '20 at 17:11
  • 1
    Man, I know that it's unprofessional but I Love You! I've been struggling with it for few hours now, and at first I didn't saw the compileOption in my gradle so I added it, and it works Perfect, Thank you! – Lidor Eliyahu Shelef Dec 13 '20 at 16:56
  • In my case, I was importing a new android library, my android library was compatible with java 8 but my main project wasn't. My solution was to make both compatible with java 8. – Luis Muñoz Aug 13 '21 at 21:42
  • I had to do a clean build after implementing these changes to get it working. – oziomajnr Sep 23 '21 at 17:08
22

You can resolve this error by setting compileOptions in build.gradle(app) file.

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
6

In my case, it was not for retrofit. I was using a local library module (named Android XML to PDF generator) that was using Java version 1.8 (for using Rx Java), but my app level Java version was less than 1.8.

So I simply added in the following lines in my app level build.gradle -

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

Then it worked for me!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
3

Try to refactor the Retrofit dependencies.

Write this:

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
xKobalt
  • 1,498
  • 2
  • 13
  • 19
0

I just changed my dependencies to below and cleaned project. I was using latest version of retrofit library before but failed with same error. It worked for me.

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'