android - React native Android 应用程序无法构建并显示以下错误

标签 android firebase gradle react-native

真实设备截图

  • 一加6
  • 氧气操作系统

我的应用程序/build.gradle

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
    entryFile: "index.js",
    bundleInRelease: true,
    enableHermes: true,
]

apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = true

def enableProguardInReleaseBuilds = true

def jscFlavor = 'org.webkit:android-jsc:+'

def enableHermes = project.ext.react.get("enableHermes", false);

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    lintOptions { 
        checkReleaseBuilds false
    }

    defaultConfig {
        applicationId "com.****.**"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 206
        versionName "2.1"
        multiDexEnabled true
        missingDimensionStrategy 'react-native-camera', 'general'

    }

    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {            
            // minifyEnabled true
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
            // shrinkResources true
            zipAlignEnabled true
            useProguard enableProguardInReleaseBuilds
        }

        // 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, "arm64-v8a": 3, "x86_64": 4]
                def abi = output.getFilter(OutputFile.ABI)
                if (abi != null) {  // null for the universal-debug, universal-release variants
                    output.versionCodeOverride =
                            versionCodes.get(abi) * 1048600 + defaultConfig.versionCode
                }
            }
        }
    }

    packagingOptions {
        pickFirst '**/armeabi-v7a/libc++_shared.so'
        pickFirst '**/x86/libc++_shared.so'
        pickFirst '**/arm64-v8a/libc++_shared.so'
        pickFirst '**/x86_64/libc++_shared.so'
        pickFirst '**/x86/libjsc.so'
        pickFirst '**/armeabi-v7a/libjsc.so'        
    }

}

dependencies {
    implementation project(':react-native-splash-screen')
    implementation project(':@react-native-community_async-storage')
    implementation project(':react-native-gesture-handler')
    implementation project(':@react-native-community_netinfo')
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.annotation:annotation:1.0.2'
    implementation fileTree(include: ['*.jar'], dir: 'libs')   
    implementation 'com.facebook.react:react-native:+'
    implementation 'com.android.support:multidex:1.0.1'

    // JSC from node_modules
   if (enableHermes) {
      def hermesPath = "../../node_modules/hermesvm/android/";
      debugImplementation files(hermesPath + "hermes-debug.aar")
      releaseImplementation files(hermesPath + "hermes-release.aar")    
    } else {
        implementation jscFlavor
    }

    // From node_modules
    implementation 'com.google.android.gms:play-services-auth-api-phone:16.0.0'
    implementation 'com.google.android.gms:play-services-identity:16.0.0'
    implementation 'com.google.android.gms:play-services-auth:16.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.android.volley:volley:1.1.1'
    implementation 'com.google.code.gson:gson:2.8.5'
    // Firebase dependencies
    implementation project(':react-native-firebase')
    implementation "com.google.android.gms:play-services-base:17.0.0"

    implementation "com.google.firebase:firebase-core:17.0.0"
    implementation "com.google.firebase:firebase-messaging:19.0.0"

    implementation project(':@mauron85_react-native-background-geolocation')
    implementation(project(':react-native-maps')){
       exclude group: 'com.google.android.gms', module: 'play-services-base'
       exclude group: 'com.google.android.gms', module: 'play-services-maps'
   }

}

// 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'
}

apply plugin: 'com.google.gms.google-services'
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

我的顶层 build.gradle 文件


buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
        googlePlayServicesVersion = "11+"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.4.1")
        classpath 'com.google.gms:google-services:4.2.0'

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

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

    }
}

/*
To resolve the gradle dependency issue
*/
subprojects {
    afterEvaluate {
        project ->
            if (project.hasProperty("android")) {
            android {
            compileSdkVersion = 28
            buildToolsVersion = "28.0.3"
            }
        }
    }
}
subprojects {
  project.configurations.all {
     resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'com.android.support'
              && !details.requested.name.contains('multidex') ) {
           details.useVersion "28.0.3"
        }
     }
  }
}

我的 package.json 依赖

"dependencies": {
    "@mauron85/react-native-background-geolocation": "^0.6.3",
    "@react-native-community/async-storage": "^1.5.1",
    "@react-native-community/geolocation": "^2.0.2",
    "@react-native-community/netinfo": "^3.2.1",
    "@types/react-native-vector-icons": "^6.4.2",
    "@typescript-eslint/eslint-plugin": "^2.2.0",
    "@typescript-eslint/parser": "^2.2.0",
    "buffer": "^5.2.1",
    "d3-shape": "^1.3.5",
    "deepmerge": "^3.2.0",
    "lottie-react-native": "^3.1.0",
    "moment": "^2.24.0",
    "object-path": "^0.11.4",
    "prop-types": "^15.7.2",
    "query-string": "^6.5.0",
    "react": "16.8.6",
    "react-native": "0.60.5",
    "react-native-android-location-services-dialog-box": "^2.8.2",
    "react-native-camera": "^3.0.0",
    "react-native-cli": "^2.0.1",
    "react-native-copilot": "^2.4.5",
    "react-native-datepicker": "^1.7.2",
    "react-native-device-info": "1.6.1",
    "react-native-document-picker": "^2.2.0",
    "react-native-elements": "^1.1.0",
    "react-native-exit-app": "^1.0.0",
    "react-native-fetch-blob": "^0.10.8",
    "react-native-firebase": "~5.5.6",
    "react-native-fs": "^2.13.3",
    "react-native-gesture-handler": "^1.3.0",
    "react-native-google-signin": "^2.0.0",
    "react-native-image-picker": "^0.28.0",
    "react-native-image-resizer": "^1.0.1",
    "react-native-image-zoom-viewer": "^2.2.25",
    "react-native-linear-gradient": "^2.5.4",
    "react-native-loader": "^1.2.1",
    "react-native-loading-spinner-overlay": "^1.0.1",
    "react-native-maps": "^0.26.1",
    "react-native-material-dropdown": "^0.11.1",
    "react-native-permissions": "^1.1.1",
    "react-native-picker-select": "^6.1.0",
    "react-native-privacy-snapshot": "^1.0.0",
    "react-native-qrcode-scanner": "^1.2.1",
    "react-native-shared-preferences": "^1.0.0",
    "react-native-simple-toast": "0.0.8",
    "react-native-snackbar": "^2.0.2",
    "react-native-splash-screen": "^3.2.0",
    "react-native-svg": "^9.4.0",
    "react-native-user-defaults": "^0.1.5",
    "react-native-vector-icons": "^6.6.0",
    "react-native-webview": "^5.12.1",
    "react-redux": "^5.0.6",
    "react-roka-components": "^1.0.1",
    "redux": "^3.0.0",
    "redux-devtools-extension": "^2.13.8",
    "redux-form": "^7.4.2",
    "url": "^0.11.0",
    "url-parse": "^1.4.7",
    "uuid": "^3.3.2",
    "whatwg-fetch": "^2.0.4"
  },

解决步骤:

尝试对“com.google.firebase:firebase-core”使用相同版本的依赖项 (17.0.0) 以及以下链接中建议的“com.google.firebase:firebase-messaging”。

无类定义错误:java.lang.NoClassDefFoundError: Failed resolution Failed resolution of: Lcom/google/android/gms/common/internal/zzab;

但是gradle构建失败并出现以下错误

CONFIGURE FAILED in 1m 0s In project 'app' a resolved Google Play services library dependency depends on another at an exact version (e.g. "[16.0.0]", but isn't being resolved to that version. Behavior exhibited by the library will be unknown.

Dependency failing: com.google.firebase:firebase-messaging:17.0.0 -> com.google.firebase:firebase-iid@[16.0.0], but firebase-iid version was 19.0.0.

The following dependencies are project dependencies that are direct or have transitive dependencies that lead to the artifact with the issue.

-- Project 'app' depends onto com.google.firebase:firebase-messaging@{strictly 17.0.0}

-- Project 'app' depends onto com.google.firebase:firebase-iid@{strictly 19.0.0}

最佳答案

我的 app/build.gradle 中缺少以下依赖项 实现('com.google.android.gms:play-services-location:17.0.0')。 添加它并重建项目成功了。

关于android - React native Android 应用程序无法构建并显示以下错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58607836/

相关文章:

java - Gradle 测试执行器因非零退出值 1 而失败(IntelliJ Idea 默认 Gradle[7.4.1]/JavaFX[17.0.1] 版本)

java - 如果使用 Cucumber JVM 和 Allure,build.gradle 中的标准设置应该如何?

android - 从 viewModelScope 中的 Flow 收集数据是否会阻止 Android Studio 中的 UI?

firebase - 安全规则,允许更新特定字段

maven - 使用 “.modules”文件下载Gradle依赖项

android - 如何通过许可或其他方式保护FCM服务

android - 可以在 firebase 中包含搜索查询吗?

java - 黑色 Activity 给出错误android studio

android - 强制动态生成的复选框只允许选择一个

android - ViewGroupOverlay 不显示 View