首页 > 解决方案 > React-native:任务':app:compileDebugJavawithJavac'的执行失败

问题描述

我对 react-native 和 gradle (一般编码)相对较新。我正在尝试在手机上运行我的代码,但我不断收到以下错误:Execution failed for task ':app:compileDebugJavawithJavac'.

我曾尝试寻找解决方案,但都没有奏效。我努力了:

  1. 重新安装环境变量 JAVA_HOME

  2. 运行 gradlew clean,并且

  3. npm也安装在我的 android 文件夹中,但没有任何效果。

这是我得到的回复:

C:\Users\Saint\OneDrive\Desktop\Important Stuff\Programming stuff\react\Calendar\android\app\src\main\java\com\Calendar\MainApplication.java:5: error: cannot find symbol
import com.facebook.react.ReactApplication;
                         ^

  symbol:   class ReactApplication 
  location: package com.facebook.react 
C:\Users\Saint\OneDrive\Desktop\Important Stuff\Programming stuff\react\Calendar\android\app\src\main\java\com\Calendar\MainApplication.java:6: error: cannot find symbol
import com.facebook.react.ReactNativeHost;
                         ^

  symbol:   class ReactNativeHost
  location: package com.facebook.react
C:\Users\Saint\OneDrive\Desktop\Important Stuff\Programming stuff\react\Calendar\android\app\src\main\java\com\Calendar\MainApplication.java:14: error: cannot find symbol
public class MainApplication extends Application implements ReactApplication {
                                                            ^

  symbol: class ReactApplication
C:\Users\Saint\OneDrive\Desktop\Important Stuff\Programming stuff\react\Calendar\android\app\src\main\java\com\Calendar\MainApplication.java:16: error: cannot find symbol
  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
                ^

  symbol:   class ReactNativeHost
  location: class MainApplication
C:\Users\Saint\OneDrive\Desktop\Important Stuff\Programming stuff\react\Calendar\android\app\src\main\java\com\Calendar\MainApplication.java:36: error: cannot find symbol
  public ReactNativeHost getReactNativeHost() 
         ^

  symbol:   class ReactNativeHost
  location: class MainApplication
C:\Users\Saint\OneDrive\Desktop\Important Stuff\Programming stuff\react\Calendar\android\app\src\main\java\com\Calendar\MainActivity.java:5: error: MainActivity is not abstract and does not override abstract method getPackages() in ReactActivity
public class MainActivity extends ReactActivity {
       ^

C:\Users\Saint\OneDrive\Desktop\Important Stuff\Programming stuff\react\Calendar\android\app\src\main\java\com\Calendar\MainApplication.java:16: error: cannot find symbol
  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
                                                       ^

  symbol:   class ReactNativeHost
  location: class MainApplication
C:\Users\Saint\OneDrive\Desktop\Important Stuff\Programming stuff\react\Calendar\android\app\src\main\java\com\Calendar\MainApplication.java:35: error: method does not override or implement a method from a supertype
  @Override
  ^

8 errors

FAILURE: Build failed with an exception.

 What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

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

* Get more help at https://help.gradle.org

BUILD FAILED in 3s
13 actionable tasks: 1 executed, 12 up-to-date
error 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
error Command failed: gradlew.bat app:installDebug. Run CLI with --verbose flag for more details.

谢谢您的帮助!

编辑:这是我的 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()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'

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

标签: androidreact-nativegradle

解决方案


推荐阅读