简体   繁体   中英

Execution failed for task ':app:compileDebugJavaWithJavac'. while running React Native Project

I was running my existing React Native Project in my real android device. Then somehow this error pop up. The error are always about "cannot find symbol". I have JDK and SDK and add into my system variables. But I still do not know why it gives me error like this. As I remember, I just downgrade the version of react-native.

:app:compileDebugJavaWithJavac - is not incremental (eg outputs have changed, no previous execution, etc.). D:\\rnprojects\\firstproject\\android\\app\\src\\main\\java\\com\\emptyprojecttemplate\\MainApplication.java:5: error: cannot find symbol import com.facebook.react.ReactApplication; ^ symbol: class ReactApplication location: package com.facebook.react D:\\rnprojects\\firstproject\\android\\app\\src\\main\\java\\com\\emptyprojecttemplate\\MainApplication.java:6: error: cannot find symbol import com.facebook.react.ReactNativeHost; ^ symbol: class ReactNativeHost location: package com.facebook.react D:\\rnprojects\\firstproject\\android\\app\\src\\main\\java\\com\\emptyprojecttemplate\\MainApplication.java:14: error: cannot find symbol public class MainApplication extends Application implements ReactApplication { ^ symbol: class ReactApplication D:\\rnprojects\\firstproject\\android\\app\\src\\main\\java\\com\\emptyprojecttemplate\\MainApplication.java:16: error: cannot find symbol private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { ^ symbol: class ReactNativeHost location: class MainApplication D:\\rnprojects\\firstproject\\android\\app\\src\\main\\java\\com\\emptyprojecttemplate\\MainApplication.java:36: error: cannot find symbol public ReactNativeHost getReactNativeHost() { ^ symbol: class ReactNativeHost location: class MainApplication D:\\rnprojects\\firstproject\\android\\app\\src\\main\\java\\com\\emptyprojecttemplate\\MainActivity.java:5: error: MainActivity is not abstract and does not override abstract method getPacka ges() in ReactActivity public class MainActivity extends ReactActivity { ^ D:\\rnprojects\\firstproject\\android\\app\\src\\main\\java\\com\\emptyprojecttemplate\\MainApplication.java:16: error: cannot find symbol private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { ^ symbol: class ReactNativeHost location: class MainApplication D:\\rnprojects\\firstproject\\android\\app\\src\\main\\java\\com\\emptyprojecttemplate\\MainApplication.java:35: error: method does not override or implement a method from a supertype @Override ^ 8 errors :app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

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

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

Your stacktrace starts with : error: cannot find symbol import com.facebook.react.ReactApplication this seems to suggest it cannot find the React Library import.

I'm going to include a answer on Github for you, read here:

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

(Btw, this relates to your build.gradle in /your-project/android/build.gradle )

In case anyone else is experiencing the same issue: make sure you correctly add new repositories. According to Android docs every maven repo should be in its own maven {} block.

That's why

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

breaks dependencies. The correct version is

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

UPDATE

As the above isn't solving your issue and your build.gradle(s) look good (to me). I'll include some other solutions :

Upgrade RN and RN-cli as advised here:

Cannot resolve symbol ReactApplication/ReactNativeHost

Another here:

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

Another here:

React native android error: cannot find symbol

Last resort

It may be worth in just creating a new test project (with the latest versions) like react-native init anotherproject to see if that runs.

I ran into the same issue recently, so I would like the readers to know what I did to get the app working. Initially the app wouldn't even launch so I used 'react-native init' command to get it to work. But the app still wouldn't launch and it gave me a server error, so I found the solution on this link for it : https://github.com/facebook/react-native/issues/21310 and followed this suggestion:

npm add @babel/runtime
npm install

My app started working like a charm !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM