任务“:app:compileDebugJavaWithJavac”执行失败。在运行 React Native 项目时

IT技术 java android reactjs react-native
2021-05-15 00:01:38

我在我的真实 android 设备上运行我现有的 React Native 项目。然后以某种方式弹出这个错误。错误总是关于“找不到符号”。我有 JDK 和 SDK 并添加到我的系统变量中。但我仍然不知道为什么它会给我这样的错误。我记得,我只是降级了 react-native 的版本。

:app:compileDebugJavaWithJavac - 不是增量的(例如,输出已经改变,之前没有执行,等等)。D:\rnprojects\firstproject\android\app\src\main\java\com\emptyprojecttemplate\MainApplication.java:5: 错误:找不到符号 import com.facebook.react.ReactApplication; ^ 符号:类 ReactApplication 位置:包 com.facebook.react D:\rnprojects\firstproject\android\app\src\main\java\com\emptyprojecttemplate\MainApplication.java:6: 错误:找不到符号导入 com.facebook。 react.ReactNativeHost; ^ 符号:类 ReactNativeHost 位置:包 com.facebook.react D:\rnprojects\firstproject\android\app\src\main\java\com\emptyprojecttemplate\MainApplication.java:14: 错误:

FAILURE:构建失败,出现异常。

  • 出了什么问题:任务“:app:compileDebugJavaWithJavac”的执行失败。编译失败;有关详细信息,请参阅编译器错误输出。

构建.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/应用程序:

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.emptyprojecttemplate"
    minSdkVersion 16
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    ndk {
        abiFilters "armeabi-v7a", "x86"
    }
}
splits {
    abi {
        reset()
        enable enableSeparateBuildPerCPUArchitecture
        universalApk false  // If true, also generate a universal APK
        include "armeabi-v7a", "x86"
    }
}
buildTypes {
    release {
        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:
        // 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 fileTree(dir: "libs", include: ["*.jar"])
    compile "com.android.support:appcompat-v7:23.0.1"
    compile "com.facebook.react:react-native:+"  // From node_modules
}

 // 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'
 }
2个回答

您的堆栈跟踪以 :error: cannot find symbol import com.facebook.react.ReactApplication这似乎表明它找不到 React 库导入。

我将在 Github 上为您提供一个答案,请在此处阅读:

https://github.com/transistorsoft/react-native-background-geolocation/issues/294

(顺便说一句,这与您的 build.gradle 相关/your-project/android/build.gradle

如果其他人遇到同样的问题:确保正确添加新存储库。根据 Android 文档,每个 maven 存储库都应该在自己的 maven {} 块中。

这就是为什么

maven {
    // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
    url "$rootDir/../node_modules/react-native/android"
    url 'some new extra repo'
}

打破依赖关系。正确的版本是

maven {
    // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
    url "$rootDir/../node_modules/react-native/android"
}
maven {
    url 'some new extra repo'
}

更新

由于以上没有解决您的问题,并且您的 build.gradle(s) 看起来不错(对我来说)。我将包括一些其他解决方案:

按照此处的建议升级 RN 和 RN-cli:

无法解析符号 ReactApplication/ReactNativeHost

另一个在这里:

失败:构建失败,在 React-native Android 中出现异常

另一个在这里:

react-nativeandroid错误:找不到符号

最后一招

可以在短短创建一个新的测试项目(具有最新版本),值得喜欢react-native init anotherproject,看看是否能运行。

我最近遇到了同样的问题,所以我希望读者知道我做了什么让应用程序正常工作。最初该应用程序甚至无法启动,因此我使用“react-native init”命令使其工作。但是该应用程序仍然无法启动并且它给了我一个服务器错误,所以我在这个链接上找到了解决方案:https : //github.com/facebook/react-native/issues/21310并遵循了这个建议:

npm add @babel/runtime
npm install

我的应用程序开始像魅力一样工作!