任务执行失败':app:compileDebugJavaWithJavac':

时间:2018-06-12 11:14:19

标签: android react-native react-native-android react-native-ios

当我在Android模拟器上尝试$ react-native run-android时,我收到此错误:

:app:compileDebugJavaWithJavac
/home/user/app/android/app/src/main/java/com/package/MainApplication.java:8: error: a type with the same simple name is already defined by the single-type-import of RNAWSCognitoPackage
import com.airlabsinc.RNAWSCognitoPackage;
^
1 error
Incremental compilation of 1 classes completed in 0.448 secs.
: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.

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

BUILD FAILED

Total time: 10.964 secs
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

然后我在/home/user/app/android/app/src/main/java/com/package/MainApplication.java中有2个同名导入 这可能导致这个问题吗? 这是RN的问题吗?

package com.package;

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.amazonaws.RNAWSCognitoPackage; // 1
import com.amazonaws.amplify.pushnotification.RNPushNotificationPackage;
import com.airlabsinc.RNAWSCognitoPackage; // 2
import com.horcrux.svg.SvgPackage;
import com.toast.ToastPackage;
import com.vdi.VDIPackage;
import com.BV.LinearGradient.LinearGradientPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import java.util.Arrays;
import java.util.List;

我的环境:

**Environment**:
  * OS: Linux 4.13
  * Node: 8.9.4
  * Yarn: Not Found
  * npm: 5.6.0
  * Watchman: Not Found
  * Xcode: N/A
  * Android Studio: Not Found

**Packages**: (wanted => installed)
  * react: 16.3.0-alpha.2 => 16.3.0-alpha.2
  * react-native: 0.54.2 => 0.54.2

4 个答案:

答案 0 :(得分:0)

您不能从JAVA中的不同包中导入相同的类,建议您删除第一个导入:

- import com.amazonaws.RNAWSCognitoPackage; // 1 (remove this line)

答案 1 :(得分:0)

  1. 删除android文件夹中的build文件
  2. 删除android>app中的构建文件
  3. 运行react-native run-android

答案 2 :(得分:0)

有两种可能,请尝试哪种更合适,

  1. 无论使用哪种方式,您都可能错过了RNAWSCognitoPackage软件包及其相关服务的某些初始设置。因此,请一次检查安装设置。

  2. 如果一切正常,那么另一种情况是,android存储缓存以加快构建速度。每次安装新软件包时,都应清除代码并再次运行。

    删除node_modules和Package-lock.json并运行以下命令

    cd android
    
    ./gradlew clean
    
    ./gradlew cleanBuildCache
    
    npm install

一些罕见的情况,您需要尝试一下

npx react-native-clean-project clean-project-auto

答案 3 :(得分:0)

不确定这是否是正确的解决方案。你能不能试一下!

  1. 在 Android Studio 中打开您的项目。转到“文件”菜单,然后单击“使缓存无效/重新启动”。
  2. 重建您的项目。
  3. 执行 npm run android 或 react-native run-android