首页 > 解决方案 > 在 React 本机项目中链接 Google Place API 时构建失败

问题描述

当我尝试将 Google Places api 安装到我的 react native 项目中并构建项目时,它会引发以下错误:

* What went wrong:
Could not determine the dependencies of task ':react-native-google-places:compileDebugAidl'.
> Could not resolve all task dependencies for configuration ':react-native-google-places:debugCompileClasspath'.
   > Could not resolve com.android.support:appcompat-v7:28.0.0.
     Required by:
         project :react-native-google-places
      > Cannot find a version of 'androidx.appcompat:appcompat' that satisfies the version constraints:
           Dependency path 'MyTaxi:react-native-google-places:unspecified' --> 'com.android.support:appcompat-v7:28.0.0'
because of the following reason: ENABLE_JETIFIER is enabled
           Constraint path 'MyTaxi:react-native-google-places:unspecified' --> 'androidx.appcompat:appcompat:{strictly 1.0.0}' because of the following reason: debugRuntimeClasspath uses version 1.0.0
           Dependency path 'MyTaxi:react-native-google-places:unspecified' --> 'com.facebook.react:react-native:0.60.4' --> 'androidx.appcompat:appcompat:1.0.2'
           Dependency path 'MyTaxi:react-native-google-places:unspecified' --> 'com.google.android.libraries.places:places:1.1.0' --> 'androidx.appcompat:appcompat:1.0.0'

   > Could not resolve androidx.appcompat:appcompat:{strictly 1.0.0}.
     Required by:
         project :react-native-google-places
      > Cannot find a version of 'androidx.appcompat:appcompat' that satisfies the version constraints:
           Dependency path 'MyTaxi:react-native-google-places:unspecified' --> 'com.android.support:appcompat-v7:28.0.0'
because of the following reason: ENABLE_JETIFIER is enabled
           Constraint path 'MyTaxi:react-native-google-places:unspecified' --> 'androidx.appcompat:appcompat:{strictly 1.0.0}' because of the following reason: debugRuntimeClasspath uses version 1.0.0
           Dependency path 'MyTaxi:react-native-google-places:unspecified' --> 'com.facebook.react:react-native:0.60.4' --> 'androidx.appcompat:appcompat:1.0.2'
           Dependency path 'MyTaxi:react-native-google-places:unspecified' --> 'com.google.android.libraries.places:places:1.1.0' --> 'androidx.appcompat:appcompat:1.0.0'

我的 build.gradle 文件如下所示:

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

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
        supportLibVersion = "28.0.0"
    }
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }

    dependencies {
        classpath("com.android.tools.build:gradle:3.4.1")

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

allprojects {
    repositories {
        google()
        mavenLocal()
        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")
        }
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        jcenter()
    }
}

还有我的 .app\build.gradle:


apply plugin: "com.android.application"

import com.android.build.OutputFile


project.ext.react = [
    entryFile: "index.js",
    enableHermes: false,  // clean and rebuild if changing
]

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

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

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
    }

    defaultConfig {
        applicationId "com.mytaxi"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    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 {
            // Caution! In production, you need to generate your own keystore file.
            // see https://facebook.github.io/react-native/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            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:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            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) * 1048576 + 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-google-places')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-geolocation-service')
    implementation project(':react-native-maps')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.facebook.react:react-native:+"  // 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
    }
}

// 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 from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

我只给出了构建过程中显示的错误片段。执行了以下步骤:

npm i react-native-google-places --save react-native 链接 react-native-google-places

然后我将 APi 密钥添加到

我无法弄清楚为什么会发生此错误。有人可以帮忙吗。提前致谢!!!

标签: react-nativereact-native-androidgoogle-places

解决方案


运行以下命令以使构建与 android x 兼容:

npm install npx
npm install jetifier
npx jetify

推荐阅读