React-Native-Admob:找不到 com.google.android.gms:play-services-ads-lite:12.0.1

React-Native-Admob: Could not find com.google.android.gms:play-services-ads-lite:12.0.1

使用命令 react-native run-android 构建我的 Android 应用程序时遇到问题。收到此错误:

Nicholass-MacBook-Pro:MonsterHunterWorld nicholaslie$ react-native run-android
Scanning folders for symlinks in /Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/node_modules (16ms)
JS server already running.
Building and installing the app on the device (cd android && ./gradlew installDebug)...
Incremental java compilation is an incubating feature.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugApk'.
   > A problem occurred configuring project ':react-native-admob'.
      > Could not resolve all dependencies for configuration ':react-native-admob:_debugPublishCopy'.
         > Could not find com.google.android.gms:play-services-ads-lite:12.0.1.
           Searched in the following locations:
               file:/Users/nicholaslie/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services-ads-lite/12.0.1/play-services-ads-lite-12.0.1.pom
               file:/Users/nicholaslie/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services-ads-lite/12.0.1/play-services-ads-lite-12.0.1.aar
               file:/Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/android/sdk-manager/com/google/android/gms/play-services-ads-lite/12.0.1/play-services-ads-lite-12.0.1.aar
               file:/Users/nicholaslie/Library/Android/sdk/extras/android/m2repository/com/google/android/gms/play-services-ads-lite/12.0.1/play-services-ads-lite-12.0.1.pom
               file:/Users/nicholaslie/Library/Android/sdk/extras/android/m2repository/com/google/android/gms/play-services-ads-lite/12.0.1/play-services-ads-lite-12.0.1.aar
               file:/Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/android/sdk-manager/com/google/android/gms/play-services-ads-lite/12.0.1/play-services-ads-lite-12.0.1.aar
           Required by:
               MonsterHunterWorld:react-native-admob:unspecified > com.google.android.gms:play-services-ads:12.0.1
         > Could not find com.google.android.gms:play-services-basement:12.0.1.
           Searched in the following locations:
               file:/Users/nicholaslie/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services-basement/12.0.1/play-services-basement-12.0.1.pom
               file:/Users/nicholaslie/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services-basement/12.0.1/play-services-basement-12.0.1.aar
               file:/Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/android/sdk-manager/com/google/android/gms/play-services-basement/12.0.1/play-services-basement-12.0.1.aar
               file:/Users/nicholaslie/Library/Android/sdk/extras/android/m2repository/com/google/android/gms/play-services-basement/12.0.1/play-services-basement-12.0.1.pom
               file:/Users/nicholaslie/Library/Android/sdk/extras/android/m2repository/com/google/android/gms/play-services-basement/12.0.1/play-services-basement-12.0.1.aar
               file:/Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/android/sdk-manager/com/google/android/gms/play-services-basement/12.0.1/play-services-basement-12.0.1.aar
           Required by:
               MonsterHunterWorld:react-native-admob:unspecified > com.google.android.gms:play-services-ads:12.0.1
         > Could not find com.google.android.gms:play-services-gass:12.0.1.
           Searched in the following locations:
               file:/Users/nicholaslie/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services-gass/12.0.1/play-services-gass-12.0.1.pom
               file:/Users/nicholaslie/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services-gass/12.0.1/play-services-gass-12.0.1.aar
               file:/Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/android/sdk-manager/com/google/android/gms/play-services-gass/12.0.1/play-services-gass-12.0.1.aar
               file:/Users/nicholaslie/Library/Android/sdk/extras/android/m2repository/com/google/android/gms/play-services-gass/12.0.1/play-services-gass-12.0.1.pom
               file:/Users/nicholaslie/Library/Android/sdk/extras/android/m2repository/com/google/android/gms/play-services-gass/12.0.1/play-services-gass-12.0.1.aar
               file:/Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/android/sdk-manager/com/google/android/gms/play-services-gass/12.0.1/play-services-gass-12.0.1.aar
           Required by:
               MonsterHunterWorld:react-native-admob:unspecified > com.google.android.gms:play-services-ads:12.0.1
         > Could not find com.google.android.gms:play-services-ads-license:12.0.1.
           Searched in the following locations:
               file:/Users/nicholaslie/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services-ads-license/12.0.1/play-services-ads-license-12.0.1.pom
               file:/Users/nicholaslie/Library/Android/sdk/extras/google/m2repository/com/google/android/gms/play-services-ads-license/12.0.1/play-services-ads-license-12.0.1.aar
               file:/Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/android/sdk-manager/com/google/android/gms/play-services-ads-license/12.0.1/play-services-ads-license-12.0.1.aar
               file:/Users/nicholaslie/Library/Android/sdk/extras/android/m2repository/com/google/android/gms/play-services-ads-license/12.0.1/play-services-ads-license-12.0.1.pom
               file:/Users/nicholaslie/Library/Android/sdk/extras/android/m2repository/com/google/android/gms/play-services-ads-license/12.0.1/play-services-ads-license-12.0.1.aar
               file:/Users/nicholaslie/Documents/Personal/React Native/MonsterHunterWorld/android/sdk-manager/com/google/android/gms/play-services-ads-license/12.0.1/play-services-ads-license-12.0.1.aar
           Required by:
               MonsterHunterWorld:react-native-admob:unspecified > com.google.android.gms:play-services-ads:12.0.1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 20.356 secs
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html

我目前使用的是 react-native-admob 版本 2.0.0-beta.5

"react-native-admob": "^2.0.0-beta.5",

我的顶级build.gradle:

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

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

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

我的应用级别build.gradle:

apply plugin: "com.android.application"

import com.android.build.OutputFile

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation
 *   entryFile: "index.android.js",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
 *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

project.ext.react = [
    entryFile: "index.js"
]

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

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = false

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "nicholaslie.monsterhunterworld"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 95
        versionName "1.0.36"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    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 {
        debug {
                manifestPlaceholders = [excludeSystemAlertWindowPermission: "false"]
        }
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            manifestPlaceholders = [excludeSystemAlertWindowPermission: "true"]
            signingConfig signingConfigs.release
        }
    }
    // 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-device-info')
    compile project(':react-native-i18n')
    compile project(':react-native-billing')
    compile project(':react-native-admob')
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
    compile project(':react-native-photo-view')
}

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

尝试降级到以前的版本,取消链接并重新链接库,删除 node_modules 文件夹并执行新的 npm install 也不起作用。有什么想法吗?

花了一整天终于让这个工作起来了。我认为这是由我过时的 Gradle 工具造成的。您需要做的是在 Android Studio (project_root/android) 中打开您的 android 文件夹,直到您在项目(左侧窗格)中得到与此类似的内容。

Android Studio 将扫描项目的 Gradle 定义并查看所有项目中是否存在任何问题。我所做的只是使用 Android Studio 升级 Gradle 定义和工具。

完成此操作后,尝试重新运行 react-native run-android,它应该会再次起作用。希望这对您有所帮助!

我通过修改 android/build.gradle

内所有项目的存储库解决了这个问题
allprojects {
  repositories {
    mavenLocal()
    maven {
        url 'https://maven.google.com'
    }
    jcenter()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
  }
}