任务':app:compileDebugJavaWithJavac'的执行失败。在运行React Native Project时

时间:2018-09-24 04:18:35

标签: java android reactjs react-native

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

  

:app:compileDebugJavaWithJavac-不是增量的(例如,输出已更改,之前没有执行等)。   D:\ rnprojects \ firstproject \ android \ app \ src \ main \ java \ com \ emptyprojecttemplate \ MainApplication.java:5:错误:找不到符号   导入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:错误:找不到符号   公共类MainApplication扩展应用程序实现ReactApplication {                                                               ^     符号:类ReactApplication   D:\ rnprojects \ firstproject \ android \ app \ src \ main \ java \ com \ emptyprojecttemplate \ MainApplication.java:16:错误:找不到符号     私有最终ReactNativeHost mReactNativeHost =新的ReactNativeHost(this){                   ^     符号:类ReactNativeHost     位置:类MainApplication   D:\ rnprojects \ firstproject \ android \ app \ src \ main \ java \ com \ emptyprojecttemplate \ MainApplication.java:36:错误:找不到符号     公共ReactNativeHost getReactNativeHost(){            ^     符号:类ReactNativeHost     位置:类MainApplication   D:\ rnprojects \ firstproject \ android \ app \ src \ main \ java \ com \ emptyprojecttemplate \ MainActivity.java:5:错误:MainActivity不是抽象的,并且不会覆盖ReactActivity中的抽象方法getPackages()   公共类MainActivity扩展了ReactActivity {          ^   D:\ rnprojects \ firstproject \ android \ app \ src \ main \ java \ com \ emptyprojecttemplate \ MainApplication.java:16:错误:找不到符号     私有最终ReactNativeHost mReactNativeHost =新的ReactNativeHost(this){                                                          ^     符号:类ReactNativeHost     位置:类MainApplication   D:\ rnprojects \ firstproject \ android \ app \ src \ main \ java \ com \ emptyprojecttemplate \ MainApplication.java:35:错误:方法未覆盖或从超类型实现方法     @Override     ^   8个错误   :app:compileDebugJavaWithJavac FAILED

     

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

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

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 / app:

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 个答案:

答案 0 :(得分:2)

您的stacktrace以:error: cannot find symbol import com.facebook.react.ReactApplication开头,这似乎表明它找不到React库导入。

我将在Github上为您提供一个答案,在这里阅读:

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

(顺便说一句,这与/your-project/android/build.gradle中的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看起来不错(对我而言)。我将提供其他解决方案:

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

Cannot resolve symbol ReactApplication/ReactNativeHost

另一个在这里:

FAILURE: Build failed with an exception in react-native Android

另一个在这里:

React native android error: cannot find symbol

不得已

只需创建一个新的测试项目(具有最新版本)(例如react-native init anotherproject),看看它是否可以运行,这是值得的。

答案 1 :(得分:0)

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

npm add @babel/runtime
npm install

我的应用开始像超级魅力!