Android Studio 3.6.1 与 Gradle 插件版本 3.6.1 和 Gradle 版本 5.6.4 不工作

Android Studio 3.6.1 with Gradle Plugin Version 3.6.1 and Gradle Version 5.6.4 not working

我的应用程序运行良好。我刚刚更新了 Android Studio 3.6.1,

更新 Android Studio 后,我收到此对话框,我将更新 Gradle 插件的版本,如下所示。

更新后,我的应用程序崩溃并出现以下错误。

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/R$string;
        at com.google.android.gms.common.internal.StringResourceValueReader.<init>(Unknown Source:4)
        at com.google.firebase.FirebaseOptions.fromResource(com.google.firebase:firebase-common@@19.3.0:156)
        at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@19.3.0:244)
        at com.google.firebase.provider.FirebaseInitProvider.onCreate(com.google.firebase:firebase-common@@19.3.0:51)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1940)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1915)
        at com.google.firebase.provider.FirebaseInitProvider.attachInfo(com.google.firebase:firebase-common@@19.3.0:45)
        at android.app.ActivityThread.installProvider(ActivityThread.java:6770)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6317)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6232)
        at android.app.ActivityThread.access00(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7078)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)

我的项目等级build.gradle是:

buildscript {
    ext.objectboxVersion = '2.5.0'
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.fabric.io/public'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
        classpath 'com.google.gms:google-services:4.3.3'

        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.2.1"
        classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

        // Add dependency
        classpath 'io.fabric.tools:gradle:1.31.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的应用级别 build.gradle 是:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: "androidx.navigation.safeargs"

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.sohamerp.marsremedies"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }
    dataBinding {
        enabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    //implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.navigation:navigation-fragment:2.2.1'
    implementation 'androidx.navigation:navigation-ui:2.2.1'

    implementation 'androidx.multidex:multidex:2.0.1'

    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.2.0-alpha05'

    implementation 'com.github.mukeshsolanki:android-otpview-pinview:2.1.0'

    implementation 'com.squareup.retrofit2:retrofit:2.7.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'

    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'com.google.firebase:firebase-core:17.2.2'
    implementation 'com.google.firebase:firebase-auth:19.2.0'
    implementation 'com.google.firebase:firebase-messaging:20.1.0'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'

    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    annotationProcessor 'androidx.annotation:annotation:1.1.0'

    implementation "com.github.firdausmaulan:GlideSlider:1.5.1"

    implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'

    implementation 'org.greenrobot:eventbus:3.1.1'

    debugImplementation "io.objectbox:objectbox-android-objectbrowser:$objectboxVersion"
    releaseImplementation "io.objectbox:objectbox-android:$objectboxVersion"

    implementation 'com.intuit.sdp:sdp-android:1.0.6'
}
// apply the plugin after the dependencies block

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

There is only one solution which I have tried is reverting back the version of Gradle which I have updated but what if I want to use the latest versions.

更新:

One of my projects is working with some version of Android Studio and Gradle. Here is Firebase SDK is different in both project so I am thinking that Firebase is Project.

有什么帮助吗?我该怎么办?

buildToolsVersion "29.0.2"

build.gradle

中删除这一行

我尝试了很多方法来解决这个问题,如下所示:

第一个解决方案: 我已经尝试了 Mayur Sojitra 的解决方案并且有效了一天。 (第二天我又遇到了同样的问题。)

第二个解决方案: 我尝试使用较低版本的 Gradle 恢复,即 3.5.0,它可以工作,但我想使用最新版本只要。 (找到新解决方案的方法)

第三个解决方案: 我正在使用 firebase 依赖项,我认为错误是因为 Firebase 版本与 Gradle 冲突,所以我更新了所有 firebase 存储库。 (希望这对你也有用)

第 4 种解决方案: 我使用了 ObjectBox 库,因此将该版本更新到最新的 2.5.1。

第 5 种解决方案: 最后我发现了我的互联网连接受限(根据公司规则)和我的 Gradle 和其他依赖项没有成功下载的问题我认为更新所有依赖项并成功下载它将是这个奇怪问题的解决方案。

如果您有任何疑问或仍然遇到问题,请告诉我。

谢谢。

如果你还在看这个,我刚刚更新到 3.6.2,它在重启后立即做了一些与 firebase 相关的更新(虽然我的代码不使用 firebase..)。

我运行进入同样的错误。但是,我按照下面的方式搭建成功了

- classpath 'com.google.firebase:firebase-plugins:1.1.1'
+ classpath 'com.google.firebase:firebase-plugins:1.2.0'

可能的解决方案:

升级顶层插件版本build.gradle

详情:

我把Gradle版本从5.4.1升级到5.6.4,插件版本从3.5.3升级到3.6.2,然后遇到了问题class 找不到。

我在项目中使用过Realm,顶层build.gradle是:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    /.../
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'
        classpath "io.realm:realm-gradle-plugin:5.8.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

这个问题好像有些library/plugin不处理R classes in R.jar而是在R.java之后Gradle插件版本大于 3.6.0。有关详细信息,请参阅 here

解决方案是将 Realm 升级到较新的版本,对我来说是 6.0.2。详情请参阅 here

    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.2'
        classpath "io.realm:realm-gradle-plugin:6.0.2" // <- modify
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }

gradle 5.6.4 有一些同步问题。转到 gradle-wrapper.properties 并更改 url 对此

distributionUrl=https://services.gradle.org/distributions/gradle-6.0-all.zip

我尝试了来自 Whosebug(此处)、github 论坛等的许多建议。 检查 gradle 和 gradle 插件版本是否存在任何兼容性问题。 只工作: 我清理 .idea 和 .gradle 文件,退出 AndroidS,然后在 Android studio 上打开我的 react native proj,我确实从 file 打开并选择 android 文件夹,然后等待 android studio 完成索引(这次真的等了)并按 hammer 图标进行构建并再次耐心等待并在模拟器上成功构建-运行。

之前没等到,估计没想到Android studio index-运行-build

这么费时间

我正在分享,如果有人仍然卡住,钥匙在等我,祝你好运

您似乎将旧的 fabric&crashlytics 与新的 firebase&crashlytics 混合在一起。尝试:

项目级别build.gradle:

buildscript {
    ext.objectboxVersion = '2.5.0'
    repositories {
        google()
        jcenter()
//        maven {
//            url 'https://maven.fabric.io/public'
//        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
        classpath 'com.google.gms:google-services:4.3.3'

        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.2.1"
        classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

        // Add dependency
//        classpath 'io.fabric.tools:gradle:1.31.0'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.1.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用级别build.gradle:

apply plugin: 'com.android.application'
apply plugin: "androidx.navigation.safeargs"
//apply plugin: 'io.fabric'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 29
//    buildToolsVersion "29.0.2"    // buildtools is now automatically determined, based on compileSDKVersion
    defaultConfig {
        applicationId "com.sohamerp.marsremedies"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }
    dataBinding {
        enabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

dependencies {
    //implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.navigation:navigation-fragment:2.2.1'
    implementation 'androidx.navigation:navigation-ui:2.2.1'

    implementation 'androidx.multidex:multidex:2.0.1'

    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.2.0-alpha05'

    implementation 'com.github.mukeshsolanki:android-otpview-pinview:2.1.0'

    implementation 'com.squareup.retrofit2:retrofit:2.7.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.7.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'

    implementation 'com.google.android.gms:play-services-location:17.0.0'
    implementation 'com.google.firebase:firebase-core:17.2.2'
    implementation 'com.google.firebase:firebase-auth:19.2.0'
    implementation 'com.google.firebase:firebase-messaging:20.1.0'
//    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
    implementation 'com.google.firebase:firebase-crashlytics:17.0.0'

    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    annotationProcessor 'androidx.annotation:annotation:1.1.0'

    implementation "com.github.firdausmaulan:GlideSlider:1.5.1"

    implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'

    implementation 'org.greenrobot:eventbus:3.1.1'

    debugImplementation "io.objectbox:objectbox-android-objectbrowser:$objectboxVersion"
    releaseImplementation "io.objectbox:objectbox-android:$objectboxVersion"

    implementation 'com.intuit.sdp:sdp-android:1.0.6'
}
// apply the plugin after the dependencies block

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