20

来自 Android Studio 的屏幕截图

谷歌播放服务

这是 React Native 项目。在 Android Studio Emulator 中成功构建后出现错误:

java.lang.NoClassDefFoundError:解析失败:Lcom/google/firebase/FirebaseApp

我的文件:

包.json:

...
"react-native": "^0.55.3",
"react-native-camera": "1.1.2",
"react-native-check-box": "^2.1.0",
"react-native-extended-stylesheet": "^0.8.1",
"react-native-firebase": "^4.2.0",
"react-native-geocoder": "^0.5.0",
"react-native-git-upgrade": "^0.2.7",
"react-native-htmlview": "^0.12.1",
"react-native-image-picker": "^0.26.10",
"react-native-linear-gradient": "^2.4.0",
"react-native-local-storage": "^1.5.2",
"react-native-maps": "^0.21.0",
"react-native-modal": "^5.4.0",
"react-native-modal-dropdown": "^0.6.1",
"react-native-read-more-text": "^1.0.0",
"react-native-router-flux": "^4.0.0-beta.27",
"react-native-svg-image": "^2.0.1",
"react-native-text-input-mask": "^0.7.0",
...

安卓/应用程序/build.gradle:

...    
android {
    compileSdkVersion 27
    buildToolsVersion "27.0.1"

    defaultConfig {
        applicationId "com.something.anything"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        multiDexEnabled true
    }
    signingConfigs {
        release {
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            }
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    compile(project(':react-native-firebase')) {
        transitive = false
    }
    compile project(':react-native-geocoder')
    compile(project(':react-native-maps')) {
      exclude group: 'com.google.android.gms', module: 'play-services-base'
      exclude group: 'com.google.android.gms', module: 'play-services-maps'
      exclude group: 'com.google.android.gms', module: 'play-services-location'
    }

    compile 'com.google.android.gms:play-services-base:15.+'
    compile 'com.google.android.gms:play-services-maps:15.+'
    compile 'com.google.android.gms:play-services-location:15.+'
    compile (project(':react-native-camera')) {
    exclude group: "com.google.android.gms"
    compile 'com.android.support:exifinterface:25.+'
    compile ('com.google.android.gms:play-services-vision:12.0.1') {
        force = true
        }
    }
    compile project(':react-native-text-input-mask')
    compile project(':react-native-linear-gradient')
    compile project(':react-native-image-picker')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
}

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        maven { url "https://maven.google.com" }
    }
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

安卓/build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}

subprojects {
  project.configurations.all {
     resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'com.android.support'
              && !details.requested.name.contains('multidex')
        ) {
           details.useVersion "27.1.0"
        }
     }
  }
}

我尝试了很多解决方案,但没有任何帮助。

4

11 回答 11

14

面临同样的问题。然后我发现我错过了将 Firebase 与 Android 项目集成的一组重要说明:https ://firebase.google.com/docs/android/setup#add_firebase_to_your_app

以下是该文档中对我的案例有所帮助的步骤摘要:

  1. 生成google-services.json并将其放入您的android/app/目录。
  2. 添加classpath 'com.google.gms:google-services:4.1.0'buildscript -> dependencies您的根级build.gradle配置部分;添加google()allprogects -> repositories同一文件的部分。
  3. 添加implementation 'com.google.firebase:firebase-core:16.0.3'dependencies应用模块build.gradle文件的部分;apply plugin: 'com.google.gms.google-services'在同一个文件的最底部添加。
  4. 同步和重建。现在它应该可以工作了。
于 2018-09-04T19:08:36.900 回答
7

在您android/app/build.gradle添加一个依赖项的另一个实现:

...
dependencies {
    implementation "com.google.firebase:firebase-auth:17.0.0"
    ...
}
于 2019-09-15T15:29:25.623 回答
4

所涉及的依赖项可能存在一些问题。

就我而言,在应用程序级别进行以下更改非常有效build.gradle

dependencies{
     implementation 'com.google.firebase:firebase-auth:17.0.0'
     implementation 'com.firebaseui:firebase-ui-database:6.0.2'
     ...
}
于 2019-11-02T20:26:27.653 回答
3

更改了最新的 firebase-messaging google 依赖项,并在应用程序 gradle 文件中的同一版本中修改了其他依赖项。现在工作正常

依赖项版本详细信息,您可以在此处参考

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.google.firebase:firebase-messaging:18.0.0'
}
于 2019-06-14T09:05:30.297 回答
2

就我而言,我忘记在 MainApplication.java 中评论数据库包

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
            new AsyncStoragePackage(),
            new RNCameraPackage(),
            new VectorIconsPackage(),
            new RNGestureHandlerPackage(),
        new RNFirebasePackage(),
        // add/remove these packages as appropriate
        //new RNFirebaseAdMobPackage(),
        new RNFirebaseAnalyticsPackage(),
        //new RNFirebaseAuthPackage(),
        // new RNFirebaseRemoteConfigPackage(),
        new RNFirebaseCrashlyticsPackage(),
        // new RNFirebaseDatabasePackage(), <==== Comment this package
       // new RNFirebaseFirestorePackage(),
        // new RNFirebaseFunctionsPackage(),
        new RNFirebaseInstanceIdPackage(),
       // new RNFirebaseInvitesPackage(),
        new RNFirebaseLinksPackage(),
        new RNFirebaseMessagingPackage(),
        new RNFirebaseNotificationsPackage(),
        new RNFirebasePerformancePackage()
       // new RNFirebaseStoragePackage()
      );
    }
于 2019-07-13T10:15:25.873 回答
2

如果您在 Flutter 中遇到此问题,请在您的 android/app/build.gradle 中添加此依赖项。并运行你的 main.dart。无需在 ListView 中添加任何类型的控制器。如果您在该页面上使用列表视图。如果您使用的是 Firebase,请在依赖项中添加此内容。

implementation "com.google.firebase:firebase-auth:19.3.0"

于 2020-04-16T09:43:11.777 回答
1

我也遇到过同样的问题。您必须更改 pakage.json 文件中的 react-native-firebase 版本。我从这里找到了解决方案

这里

于 2019-08-21T12:54:42.640 回答
0
implementation("com.google.firebase:firebase-iid")
于 2021-11-23T06:06:02.040 回答
0

解决方案1:

android/app/build.gradle在您的文件中添加一个对依赖项的实现。

dependencies {
    implementation "com.google.firebase:firebase-auth:17.0.0"
    ...
}

解决方案2:

  • 生成google-services.json并将其放入您的android/app/目录。

  • 添加classpath 'com.google.gms:google-services:4.1.0'到你的根级 build.gradle 配置的 buildscript -> dependencies 部分,并google()作为第一个条目添加到同一文件的 allprogects -> repositories 部分。

      buildscript {
          repositories {
              google()
              jcenter()
          }
          dependencies {
              ...
              classpath 'com.google.gms:google-services:4.1.0'
          }
      }
      allprojects {
          repositories {
              google()
              ...
          }
      }
    
  • 添加implementation 'com.google.firebase:firebase-core:16.0.3'到应用程序模块build.gradle文件的依赖项部分并添加apply plugin: 'com.google.gms.google-services'到同一文件的最底部。

    dependencies {
        ...
        implementation 'com.google.firebase:firebase-core:16.0.3'
    } 
    apply plugin: 'com.google.gms.google-services'
    
  • 同步和重建。

于 2021-11-23T16:59:34.500 回答
-2

更新您的 Firebase 库!!!!

例如。如果您的应用程序使用 Firebase 身份验证,Google 为其提供最新版本的文档,请将其粘贴到您的 build.gradle

您的 Firebase 助手在 Android Studio 中添加的库通常较旧。

于 2020-06-01T18:52:35.820 回答
-3

在 gradle.properties 文件中,尝试添加::

android.enableJetifier = true
android.useAndroidX = true
于 2019-12-05T18:56:17.843 回答