React Native FAILURE:构建因异常而失败。无法解析':classpath'。找不到com.android.tools.build:gradle:3.0.1

时间:2017-11-23 12:17:28

标签: android react-native classpath gradle-3.0

当我发出命令“react-native run-android”时,它就发生了:

失败:构建因异常而失败。

截图:

enter image description here

7 个答案:

答案 0 :(得分:29)

我遇到了同样的问题,我尝试了Manoj Prabhakar's solution,但我修复了将google()存储库添加到项目级build.gradle

中的buildscript块的问题
buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

答案 1 :(得分:8)

我已将google()mavenlocal()添加到buildscript

buildscript {
    repositories {
        google()
        mavenLocal()
        jcenter()
    }
    ....
}

答案 2 :(得分:6)

Jcenter没有Gradle 3.0.1。

Gradle 3.0.1

它可以在Google的maven存储库中找到。 here

您应该将google()添加到项目级build.gradle

中的所有项目

执行此操作:

在您的反应原生项目中,导航到 - > android - >的build.gradle。

将google()添加到allproject存储库,如下所示:

enter image description here

这可以解决您的问题。

编辑1: 用这个替换google()

maven {
            url "https://maven.google.com/"
}  

答案 3 :(得分:1)

    // 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.0.1'

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

答案 4 :(得分:1)

buildscript {
    repositories {
        google() // I had to put this before jcenter(). If I put jcenter() first it fails when running react-native run-android
        jcenter()
    }
}

要明确的是,如果我将jcenter()放在buildscript中,我仍然可以在Android Studio中成功构建。然而,运行react-native run-android失败,直到我将google()放在jcenter()之前。在那之后,一切都变得光滑如黄油。

答案 5 :(得分:0)

所以,对我来说这个问题已解决,请将google()添加到存储库

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

然后从终端导航到您的项目并运行

 cd android && gradlew clean

然后在项目中运行

 react-native run-android 

答案 6 :(得分:0)

尝试从

更改build.gradle文件中的类路径
@Override
public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel chan = new NotificationChannel("test", "your_channel_name", NotificationManager.IMPORTANCE_NONE);
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (manager != null) {
            manager.createNotificationChannel(chan);
        }

        Notification.Builder notificationBuilder = new Notification.Builder(context, "test");
        notificationBuilder.setOngoing(true);
        notificationBuilder.setContentTitle("Updatig")
                .setContentText("Wait for finish updating")
        startForeground(NOTIFICATION_ID, notificationBuilder.build());
    }
}

    classpath 'com.android.tools.build:gradle:3.1.4'