React Native 运行 Android 失败

4

我遵循了React Native Android指南页面上的所有指示,但是当我使用 react-cli 创建项目并运行 react-native run-android 时,我遇到了一些错误:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'test'.
> Could not resolve all dependencies for configuration ':classpath'.
   > Could not resolve org.apache.commons:commons-compress:1.8.1.
     Required by:
         :test:unspecified > com.android.tools.build:gradle:2.2.3 > com.android.tools.build:gradle-core:2.2.3 > com.android.tools.build:builder:2.2.3 >

com.android.tools:sdklib:25.2.3 :test:unspecified > com.android.tools.build:gradle:2.2.3 > com.android.tools.build:gradle-core:2.2.3 > com.android.tools.build:builder:2.2.3 > com.android.tools:sdklib:25.2.3 > com.android.tools:repository:25.2.3

Could not resolve org.apache.commons:commons-compress:1.8.1. Could not get resource 'https://jcenter.bintray.com/org/apache/commons/commons-compress/1.8.1/commons-compress-1.8.1.pom'. Could not HEAD 'https://jcenter.bintray.com/org/apache/commons/commons-compress/1.8.1/commons-compress-1.8.1.pom'. Connect to jcenter.bintray.com:443 [jcenter.bintray.com/75.126.118.188] failed: Connection timed out (Connection timed out)

* 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: 2 mins 16.65 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/android-setup.html

我在谷歌上搜索了整整一天,但没有找到有用的信息来解决这个问题。以下是一些相关信息:

$ java -version

openjdk version "1.8.0_131"   
OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2ubuntu1.16.04.3b11)
OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)

$ android list avd

Available Android Virtual Devices:
    Name: Pixel_API_25
  Device: pixel (Google)
    Path: /home/danial/.android/avd/Pixel_API_25.avd
  Target: Google APIs (Google Inc.)
          Based on: Android 7.1.1 (Nougat) Tag/ABI: google_apis/x86_64
    Skin: pixel
  Sdcard: 100M

$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/danial/Android/Sdk/tools:/home/danial/Android/Sdk/platform-tools:/home/danial/Android/Sdk/tools/bin

如果一切设置正确,我真的不知道问题在哪里。

**First Answer Error Again:**

[Fatal Error] apache-13.pom:1:44: Content is not allowed in prolog.
[Fatal Error] apache-13.pom:1:44: Content is not allowed in prolog.
[Fatal Error] apache-13.pom:1:44: Content is not allowed in prolog.
[Fatal Error] apache-13.pom:1:44: Content is not allowed in prolog.
[Fatal Error] apache-13.pom:1:44: Content is not allowed in prolog.
[Fatal Error] apache-13.pom:1:44: Content is not allowed in prolog.
[Fatal Error] apache-13.pom:1:44: Content is not allowed in prolog.
[Fatal Error] apache-13.pom:1:44: Content is not allowed in prolog.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'test'.

> Could not resolve all dependencies for configuration ':classpath'.
   > Could not resolve org.apache.commons:commons-compress:1.8.1.
     Required by:
         :test:unspecified > com.android.tools.build:gradle:2.2.3 > com.android.tools.build:gradle-core:2.2.3 > com.android.tools.build:builder:2.2.3 >

com.android.tools:sdklib:25.2.3 :test:unspecified > com.android.tools.build:gradle:2.2.3 > com.android.tools.build:gradle-core:2.2.3 > com.android.tools.build:builder:2.2.3 > com.android.tools:sdklib:25.2.3 > com.android.tools:repository:25.2.3

Could not resolve org.apache.commons:commons-compress:1.8.1. Could not parse POM https://jcenter.bintray.com/org/apache/commons/commons-compress/1.8.1/commons-compress-1.8.1.pom Could not resolve org.apache.commons:commons-parent:33. Could not resolve org.apache.commons:commons-parent:33. Could not parse POM https://jcenter.bintray.com/org/apache/commons/commons-parent/33/commons-parent-33.pom Could not resolve org.apache:apache:13. Could not resolve org.apache:apache:13. Could not parse POM https://jcenter.bintray.com/org/apache/apache/13/apache-13.pom Content is not allowed in prolog. Could not resolve org.apache:apache:13. Could not parse POM https://repo1.maven.org/maven2/org/apache/apache/13/apache-13.pom Content is not allowed in prolog. Could not resolve org.apache.commons:commons-parent:33. Could not parse POM https://repo1.maven.org/maven2/org/apache/commons/commons-parent/33/commons-parent-33.pom Could not resolve org.apache:apache:13. Could not resolve org.apache:apache:13. Could not parse POM https://jcenter.bintray.com/org/apache/apache/13/apache-13.pom Content is not allowed in prolog. Could not resolve org.apache:apache:13. Could not parse POM https://repo1.maven.org/maven2/org/apache/apache/13/apache-13.pom Content is not allowed in prolog. Could not resolve org.apache.commons:commons-compress:1.8.1. Could not parse POM https://repo1.maven.org/maven2/org/apache/commons/commons-compress/1.8.1/commons-compress-1.8.1.pom Could not resolve org.apache.commons:commons-parent:33. Could not resolve org.apache.commons:commons-parent:33. Could not parse POM https://jcenter.bintray.com/org/apache/commons/commons-parent/33/commons-parent-33.pom Could not resolve org.apache:apache:13. Could not resolve org.apache:apache:13. Could not parse POM https://jcenter.bintray.com/org/apache/apache/13/apache-13.pom Content is not allowed in prolog. Could not resolve org.apache:apache:13. Could not parse POM https://repo1.maven.org/maven2/org/apache/apache/13/apache-13.pom Content is not allowed in prolog. Could not resolve org.apache.commons:commons-parent:33. Could not parse POM https://repo1.maven.org/maven2/org/apache/commons/commons-parent/33/commons-parent-33.pom Could not resolve org.apache:apache:13. Could not resolve org.apache:apache:13. Could not parse POM https://jcenter.bintray.com/org/apache/apache/13/apache-13.pom Content is not allowed in prolog. Could not resolve org.apache:apache:13. Could not parse POM https://repo1.maven.org/maven2/org/apache/apache/13/apache-13.pom Content is not allowed in prolog.


你使用的操作系统是什么? - Wanda Ichsanul Isra
@wlisrausr Ubuntu 16.04 LTS - Daniel.V
你试过这个吗?https://dev59.com/BV8e5IYBdhLWcg3wLYHt - Wanda Ichsanul Isra
是的,但仍然出现错误,没有任何改变 :( - Daniel.V
我用Android Studio创建了一个纯Android项目并进行了测试。在运行该项目时,它会在模拟器上安装应用程序并完美地运行。然而,当我阅读https://developer.android.com/studio/build/building-cmdline.html#RunningOnEmulator有关从命令行运行Android项目的页面时,当我在命令行上运行gradlew installDebug时,出现了相同的错误。因此,真正的问题是,Android Studio如何构建并在模拟器上运行项目,而我无法通过命令行完成这一操作? - Daniel.V
你能否发布你的 build.gradle 和 gradle-wrapper.properties 文件?在 gradle.wrapper.properties 文件中,请检查你的 distributionUrl 是否正确。 - chetan
2个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
1
请尝试从根目录开始操作。 在其中创建一个 assets 文件夹。
 /android/app/src/main/

 cd android

 ./gradlew clean

 react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

 react-native run-android

1

看起来你的项目无法从jcenter获取依赖项。尝试在build.gradle文件中添加仓库。

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
    jcenter()
}
将上述代码放置在 buildscript {...} 的 body 中。

我更新了我的问题,你的解决方案也有一些错误。 - Daniel.V

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,