React Native项目gradle手动编译

最近在折腾,远程开发React Native 项目,我想实现在ssh命令行中。在服务器上自动编译RN 项目(android),这样就可以使用高速的服务器来编译项目。

正解

cd android到工程目录后,执行以下命令即可完成debug 的apk生成。

gradlew assembleDebug

以下要探索过程,仅供参考。

查任务列表

cd /project/android执行以下命令,获取可执行的任务列表

gradlew tasks

查询结果如下(部分):

  android ./gradlew tasks
Starting a Gradle Daemon, 2 incompatible and 1 stopped Daemons could not be reused, use --status for details

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project 'ywh'
------------------------------------------------------------

Android tasks
-------------
androidDependencies - Displays the Android dependencies of the project.
signingReport - Displays the signing info for the base and test modules
sourceSets - Prints out all the source sets defined in this project.

Build tasks
-----------
assemble - Assemble main outputs for all the variants.
assembleAndroidTest - Assembles all the Test applications.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildKotlinToolingMetadata - Build metadata json file containing information about the used Kotlin tooling
buildNeeded - Assembles and tests this project and all projects it depends on.
bundle - Assemble bundles for all the variants.
clean - Deletes the build directory.
cleanBuildCache - Deletes the build cache directory.
compileDebugAndroidTestSources
compileDebugSources
compileDebugUnitTestSources
compileReleaseSources
compileReleaseUnitTestSources
extractDebugAnnotations - Extracts Android annotations for the debug variant into the archive file
extractReleaseAnnotations - Extracts Android annotations for the release variant into the archive file

编译生成安装包

方法一

gradlew installDebug

这样就会在以下目录中生成apk安装包:

/android/app/build/outputs/apk/debug

虽然可以正常生成debug的安装包,但是有个小问题。会在日志中显示报错信息,主要是因为服务器上并没有连接手机,所以无法正常安装。虽然影响不大,但是看着不舒服。

 android ./gradlew installDebug
> Task :app:installDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:installDebug'.
> com.android.builder.testing.api.DeviceException: No connected devices!

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

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.9/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 16s
225 actionable tasks: 3 executed, 222 up-to-date

方法二

这个方法也可以生成 debug的apk包,但是会同时生成release包的apk包,导致整个过程要稍微慢一些。

 ./gradlew assemble

比如在AS中只点Build大概18S 就可以生成 debug 的apk,而这个命令要21~49S 左右(时间通常皆比较长)。我分析出现是因为同时输出 了release包导致。

方法三

我比较了一下,这个命令应该就是AS中的Build中相同的功能。18S 左右就可以了。

gradlew assembleDebug

你可能感兴趣的:(React,Native,react,native)