【问题标题】:Getting Exception java.lang.NoClassDefFoundError: com.google.firebase.FirebaseOptions after updating to the new firebase更新到新的 Firebase 后出现异常 java.lang.NoClassDefFoundError: com.google.firebase.FirebaseOptions
【发布时间】:2016-09-18 12:17:30
【问题描述】:

我已经使用this 将我的应用程序更新到了新的firebase,现在当我编译我的项目时,我得到了以下异常。

这是我的 logcat:

11:57:54.533 27844-27844/com.example.dayshift_2.traveyy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dayshift_2.traveyy, PID: 27844
java.lang.NoClassDefFoundError: com.google.firebase.FirebaseOptions
at com.google.firebase.FirebaseApp.zzbu(Unknown Source)
at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1591)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1562)
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
at android.app.ActivityThread.installProvider(ActivityThread.java:4871)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4466)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4406)
at android.app.ActivityThread.access$1500(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1270)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)

我的Build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.example.dayshift_2.traveyy"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE-FIREBASE.txt'
    exclude 'META-INF/NOTICE'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.google.firebase:firebase-database:9.0.0'
compile 'com.google.firebase:firebase-auth:9.0.0'
compile 'com.android.support:support-v13:23.3.0'
compile 'com.roughike:bottom-bar:1.2.1'
}


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

不确定我从哪里得到这个异常。任何帮助将不胜感激。 谢谢

【问题讨论】:

  • 您在哪个版本的 android 上进行测试?我在 KitKat 上面临同样的问题,但我的应用程序在 Lollipop / Marshmallow 上运行良好。另外,请发布代码,否则很难为您提供帮助。
  • 您好,我已将 google play 服务更新为 30,将 google 存储库更新为 28。我已在自定义应用程序文件中初始化 Multidex.install(this)。我在 kitkat 上仍然遇到同样的错误,但在以后的版本中可以正常工作。
  • 您解决了 KitKat 设备的问题吗?
  • @guilherme.minglini 是的,我确实遵循了 BKC 的回答。我已经在带有 jellybean to marshmellow 的设备上对其进行了测试,并且工作正常。

标签: android exception firebase firebase-realtime-database


【解决方案1】:

在执行所有这些忙碌的解决方案之前,请尝试从 Android Studio Clean ProjectRebuild Project

java.lang. NoClassDefFoundError

当您在项目中更改任何 Class 名称时会引发异常,但它无法获取您在 Runtime 中所做的更改。

【讨论】:

    【解决方案2】:

    使用最新的 Google Play 服务但无法正常工作。对我来说,这个过程是有效的:

    1. 更新了我的应用程序类:

      public class App extends Application {
        @Override
        protected void attachBaseContext(Context base) {
          super.attachBaseContext(base);
          MultiDex.install(this);
       }
      }
      
    2. 更新build.gradle文件:

      defaultConfig {
          ...
          multiDexEnabled true
      }
      
    3. 包含的依赖项:

      dependencies {
        compile 'com.android.support:multidex:1.0.0'
      }
      

    【讨论】:

    • 是的,我只是忘记将属性 android:name="android.support.multidex.MultiDexApplication" 放在我的 Mianifest 中的 标记内
    • 不应该App extends Application 实际上是App extends MultiDexApplication
    【解决方案3】:

    为您的应用配置 multidex 以解决此问题。

    对于minSdkVersion21 或以上,在您的模块级 build.gradle 文件中将 multiDexEnabled 设置为 true,如下所示:

    android {
      defaultConfig {
        //...
        minSdkVersion 21 
        targetSdkVersion 26
        multiDexEnabled true
       }
      //...
     }
    

    对于minSdkVersion API 20 或更低

    1. 修改module-level build.gradle,添加multidex库为依赖,如下图:

      android { defaultConfig { ... minSdkVersion 15 targetSdkVersion 26 multiDexEnabled true } //... } dependencies { compile 'com.android.support:multidex:1.0.1' }

    2. 如果您不覆盖 Application 类,请编辑您的 manifest 文件以在应用程序标记中设置 android:name,如下所示

      <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp"> <application android:name="android.support.multidex.MultiDexApplication"> //... </application> </manifest>

      2.1.1。如果您确实覆盖了 Application 类扩展 MultiDexApplication

      public class MyApplication extends MultiDexApplication { //... }
      

      2.1.2。或者,如果您确实重写了 Application 类但无法更改基类,那么您可以重写 attachBaseContext() 方法并调用 MultiDex.install( this) 启用多重索引:

      public class MyApplication extends SomeOtherApplication { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } }

    了解更多mdex-gradle

    希望这将帮助其他面临同样问题的人。

    【讨论】:

      【解决方案4】:

      尝试解决...

      在 Gradle 文件中

      multiDexEnabled 真

      编译'com.android.support:multidex:1.0.1'

      清单文件

      应用标签添加这一行

      android:name="android.support.multidex.MultiDexApplication"

      【讨论】:

        【解决方案5】:

        build.gradle文件中降级你的依赖:

        来自:

        compile 'com.google.android.gms:play-services:9.4.0'
        

        到:

        compile 'com.google.android.gms:play-services:7.3.0'
        

        这对我有用,希望对你也一样。

        【讨论】:

          【解决方案6】:
          dependencies {
          compile 'com.android.support:support-v4:25.0.1'
          compile 'com.google.firebase:firebase-ads:9.0.2'
          compile files('libs/StartAppInApp-3.5.0.jar')
          compile 'com.android.support:multidex:1.0.1'    
          }  
          

          申请插件:'com.google.gms.google-services'.

          【讨论】:

            【解决方案7】:

            这对我有用:

            1. 如果您还没有,请从 Android SDK Manager > Extras 将您的“Google Play 服务”更新到第 30 版。

            1. 然后将compile 'com.android.support:multidex:1.0.1' 行添加到您的依赖项中(或者如果不需要,只需删除multiDexEnabled true

            2. 将此属性添加到清单中的应用程序标记:android:name="android.support.multidex.MultiDexApplication"
              如果您已经在 Android Manifest 中定义了自定义应用程序类,请将其从 MultiDexApplication 扩展而不是 Application

            希望对您有所帮助!

            【讨论】:

            • 我的项目中已经有一个自定义应用程序,它扩展了 ORM 应用程序类。应该如何管理 android:name="android.support.multidex.MultiDexApplication" 的需求?你能描述一下为什么会发生这个错误吗?
            • @EdgarDaSilvaFernandes “android.support.multidex.MultiDexApplication”所做的就是这一行。 "MultiDex.install(this);"
            • 您好,我已将 google play 服务更新为 30,将 google 存储库更新为 28。我已在自定义应用程序文件中初始化 Multidex.install(this)。我在 kitkat 上仍然遇到同样的错误,但在以后的版本中可以正常工作。
            • 除了按照上面的所有说明进行操作外,我还更新到了 SDK 的 24.0.0,不确定是否有所不同。但现在一切正常。
            • 我有 31 转。的 Play 服务,但不适用于 18 版之前的 API 设备
            【解决方案8】:
             GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestIdToken(getString(**R.string.default_web_client_id**))
                        .requestEmail()
                        .build();
            

            在我同样的情况下,我更改了 requestIdToken 并且它起作用了。

            【讨论】:

              【解决方案9】:

              降级我的 google play-service 依赖项来自

              compile 'com.google.android.gms:play-services:9.2.0' 
              

              compile 'com.google.android.gms:play-services:8.4.0' 
              

              它为我解决了这个问题。

              请参考the following Stack Overflow link for further description


              请注意:升级到最新的 google play-service
              compile 'com.google.android.gms:play-services:9.4.0' 也解决了这个问题。

              或者您可以选择性地导入您需要的某些服务,而不是导入所有 google play 服务。

              【讨论】:

              • Upgrading 从 8.4.0 到 9.2.0 为我修复了它(使用 com.google.android.gms:play-services-gcm,并且刚刚升级了播放服务到修订版 32,但该更改本身并没有解决它,所以我不知道它是否相关)
              【解决方案10】:

              第一步:修复多索引

              应用类:

              public class StudyNApplication extends Application {
              
                  @Override
                  public void onCreate() {
                      super.onCreate();
                      MultiDex.install(this);
                  }
               }
              

              成绩文件

              defaultConfig { 
              ...
              multiDexEnabled true
              }
              
              dependencies {
                compile 'com.android.support:multidex:1.0.0'
              }
              

              第二步:修复 java.lang.NoClassDefFoundError: com.google.firebase.FirebaseOptions

              删除

              compile 'com.google.android.gms:play-services:9.4.0'
              

              添加两个库:

              compile 'com.google.android.gms:play-services-maps:9.4.0'
              compile 'com.google.android.gms:play-services-location:9.4.0'
              

              效果很好。

              【讨论】:

              • 请将方法更新为 attachBaseContext - 它肯定有效。
              【解决方案11】:

              当我将 targetSDKVersion 从 22 增加到 23 时,我遇到了同样的错误,我至少花了一整天的时间来解决这个问题。现在我终于在你的 build.gradle 文件中找到了使用不同播放服务 api 的结果 资源: https://developers.google.com/android/guides/setup#add_google_play_services_to_your_project

              我只添加了相关的 API,我的应用运行良好。

              【讨论】:

                【解决方案12】:

                我通过添加这个并将播放服务依赖性降低到 8.4.0 来解决 编译 'com.google.android.gms:play-services:8.4.0'

                    @Override
                protected void attachBaseContext(Context newBase) {
                    super.attachBaseContext(newBase);
                    MultiDex.install(this);
                }
                

                并将以下内容添加到清单中

                android:name="android.support.multidex.MultiDexApplication"
                

                【讨论】:

                  【解决方案13】:

                  别忘了。

                  android:name="android.support.multidex.MultiDexApplication"
                  

                  【讨论】:

                    【解决方案14】:

                    我想我已经在answer 找到了解决这个问题的方法,请看一下并感谢 @Gabriele Mariotti,他写道:

                    在您的顶级 build.gradle 中,您必须添加:

                    buildscript {
                        // ...
                        dependencies {
                            // ...
                            classpath 'com.google.gms:google-services:3.0.0'
                        }
                    }
                    

                    然后在你的模块 build.gradle:

                    apply plugin: 'com.android.application'
                    
                    android {
                      // ...
                    }
                    
                    dependencies {
                      // ... 
                    
                    }
                    
                    // ADD THIS AT THE BOTTOM
                    apply plugin: 'com.google.gms.google-services'
                    

                    【讨论】:

                      【解决方案15】:

                      修复人员非常简单。如果您在项目中同时使用播放器服务和 firebase,请确保您没有在 google 中导入所有可用服务。只需导入您实际需要的 API。 例如:

                       compile 'com.google.firebase:firebase-messaging:9.2.0'
                       compile 'com.google.android.gms:play-services:9.2.0'
                      

                      这样做可以给你noclassdeffounderror。要解决这个问题

                      删除播放服务并同步您的项目。如果你有依赖,比如在你的应用中使用广告或地图,那么定义特定的 API。喜欢:

                      compile 'com.google.android.gms:play-services-maps:9.2.0'
                      

                      注意:Maximum 不要尝试使用multiDexEnabled true。这应该是您解决问题的最后一步。

                      希望这有帮助。

                      【讨论】:

                      • 这对我有用。我的应用在 Lollipop+ 上运行良好,但在 4.4+ 上崩溃。
                      • 几乎所有 cmets 和响应通常都建议启用多索引,在我的情况下指定我只使用地图是唯一需要的步骤,如本答案中所述。
                      • 完美运行,只需从 'com.google.android.gms:play-services:10.0.1' 更改为 'com.google.android.gms:play-services-ads:10.0.1 '
                      猜你喜欢
                      • 2016-12-29
                      • 2016-09-22
                      • 2017-02-04
                      • 1970-01-01
                      • 2015-03-09
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多