react-native运行android vs./gradlew assembleDebug

时间:2018-10-20 00:14:28

标签: react-native react-native-android

我注意到在构建android构建时,react-native和gradle具有两个不同的输出。

  1. 本机构建似乎合并了react-native-config等插件的一些预构建工作
  2. 如果没有要安装的设备(本机为仿真器),react-native将在构建时失败
  3. 不同的BUILD SUCCESSFUL输出,特别是error type 3
  4. 安装-仅react-native

问题

除了安装部分(4)之外,这两个命令的构建部分为何不同?

样本输出

react-native run-android

$ react-native run-android
Scanning folders for symlinks in /Users/Jackson/Sites/fnmultiapptest/node_modules (10ms)
JS server already running.
Building and installing the app on the device (cd android && ./gradlew installDebug)...

> Configure project :app 
Reading env from: .env
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
Observed package id 'build-tools;20.0.0' in inconsistent location '/Users/Jackson/.android/build-tools/android-4.4W' (Expected '/Users/Jackson/.android/build-tools/20.0.0')

> Configure project :react-native-config 
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

> Task :react-native-config:installDebugAndroidTest 
11:40:14 V/ddms: execute: running am get-config
11:40:14 V/ddms: execute 'am get-config' on 'emulator-5554' : EOF hit. Read: -1
11:40:14 V/ddms: execute: returning
Installing APK 'react-native-config-debug-androidTest.apk' on 'Nexus_5X_API_26(AVD) - 8.0.0' for react-native-config:debugAndroidTest
11:40:14 D/react-native-config-debug-androidTest.apk: Uploading react-native-config-debug-androidTest.apk onto device 'emulator-5554'
11:40:14 D/Device: Uploading file onto device 'emulator-5554'
11:40:14 D/ddms: Reading file permision of /Users/Jackson/Sites/fnmultiapptest/node_modules/react-native-config/android/build/outputs/apk/androidTest/debug/react-native-config-debug-androidTest.apk as: rw-r--r--
11:40:14 V/ddms: execute: running pm install -r -t "/data/local/tmp/react-native-config-debug-androidTest.apk"
11:40:15 V/ddms: execute 'pm install -r -t "/data/local/tmp/react-native-config-debug-androidTest.apk"' on 'emulator-5554' : EOF hit. Read: -1
11:40:15 V/ddms: execute: returning
11:40:15 V/ddms: execute: running rm "/data/local/tmp/react-native-config-debug-androidTest.apk"
11:40:15 V/ddms: execute 'rm "/data/local/tmp/react-native-config-debug-androidTest.apk"' on 'emulator-5554' : EOF hit. Read: -1
11:40:15 V/ddms: execute: returning
Installed on 1 device.


BUILD SUCCESSFUL in 2s
38 actionable tasks: 1 executed, 37 up-to-date
Running /Users/Jackson/.android/platform-tools/adb -s emulator-5554 reverse tcp:8081 tcp:8081
Starting the app on emulator-5554 (/Users/Jackson/.android/platform-tools/adb -s emulator-5554 shell am start -n com.workingproject/com.workingproject.MainActivity)...
Starting: Intent { cmp=com.workingproject/.MainActivity }
Error type 3
Error: Activity class {com.workingproject/com.workingproject.MainActivity} does not exist.

./ gradlew assembleDebug

$ ./gradlew assembleDebug

> Configure project :app 
Reading env from: .env
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html

> Configure project :react-native-config 
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html


BUILD SUCCESSFUL in 0s
50 actionable tasks: 50 up-to-date

2 个答案:

答案 0 :(得分:2)

react-native run-android包含命令./gradlew installDebug

./gradlew installDebug用于需要将apk推送到设备时,而./gradlew assembleDebug仅用于构建apk。

如果必须要有一个观点,可以说./gradlew assembleDebug./gradlew installDebug执行的过程的子集

从本质上讲,如果我必须列出,react-native run-android执行的过程包括-

  1. 启动都会打包程序捆绑器,它将监听您所有的js更改
  2. 使用./gradlew installDebug
  3. 构建apk
  4. 检查可用设备,如果不是,则失败。
  5. 如果成功,它将在设备上安装apk,您可以从显示给我们的日志中读取该apk。
  6. 最后,它执行reverse tcp:8081 tcp:8081,它将您的Metro Bundle捆绑到应用仿真器/设备

然后./gradlew assembleDebug会构建apk。没有了

希望它会有所帮助:)

PS:如果仔细检查,react-native run-android的构建部分是成功的,因此可以看到BUILD SUCCESSFUL的输出,但是由于其他进程失败,因此它整体上会失败。

答案 1 :(得分:1)

根据我的说法,您正在比较两种不同的事物。

  1. react-native run-android

    它执行cd android && ./gradlew installDebug(在打印的日志中可以看到),它专门用于安装已经构建并签名的apk,或者构建apk并立即将其安装在正在运行的模拟器或连接的设备上。
    /> 由于安装不成功,它会引发错误。

  2. ./gradlew assembleDebug

    另一方面,./gradlew assembleDebug仅用于构建应用程序。(这意味着您可以使用此命令构建调试APK。)成功创建调试apk时,它会显示消息“ BUILD SUCCESSFUL”(构建成功)


REF: Build a debug APK