React Native 编译 Android APK

更新日期: 2024-12-31 阅读次数: 105 字数: 890 分类: Android

React Native 编译 Android APK 真是费劲,周末折腾了半天。浪费生命。有空试试 Google NPX 能否云编译。 国内的网络环境,搭建环境能要人命。

新建项目

npx create-expo-app@latest

网页浏览器调试

> npm run web

Android 真机调试

PS D:\android_sdk\platform-tools> .\adb.exe devices
List of devices attached
1eb7903e        device

最好是把 adb.exe 的目录,放到系统环境变量中。要不每次都要到 Android platform tools 中执行。

确认手机开启了开发者模式之后,并且 USB 连接成功,就可以真机调试了。

npm run android

修改会及时更新到手机上,倒是非常方便。只不过会安装一个 Expo Go 的 App,并不是纯正的 Android APK 应用。

但是要编译成 Android APK,经历了种种曲折。

> npx react-native build-android

⚠️ react-native depends on @react-native-community/cli for cli commands. To fix update your package.json to include:

增加 package.json 配置

  "devDependencies": {
    "@react-native-community/cli": "latest",
  }

执行

npm install

error Android project not found

> npx react-native build-android
(node:16640) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
error Android project not found. Are you sure this is a React Native project? If your Android files are located in a non-standard location (e.g. not inside 'android' folder), consider setting `project.android.sourceDir` option to point to a new location.

解决方法:

> npx expo prebuild
› Android package name: com.anonymous.myapp
√ Created native directory
√ Updated package.json
√ Finished prebuild

然后就能看到 Android 相关目录了,跟真实 android 原生项目类似:

> ls .\android\


    目录: D:\work\test\rn\my-app\android


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2024-12-29     17:16                app
d-----        2024-12-29     17:16                gradle
-a----        1985-10-26     16:15            129 .gitignore
-a----        2024-12-29     17:16           1594 build.gradle
-a----        2024-12-29     17:16           2290 gradle.properties
-a----        1985-10-26     16:15           8762 gradlew
-a----        1985-10-26     16:15           2966 gradlew.bat
-a----        2024-12-29     17:16           1470 settings.gradle

如果不想生成 ios 目录,只生成 android 目录,还可以直接运行:

> npx expo run:android

› Android package name: com.anonymous.xxx
√ Created native directory
√ Updated package.json
√ Finished prebuild

无法下载 gradle

> npx react-native build-android
(node:7372) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
info Building the app...
Downloading https://services.gradle.org/distributions/gradle-8.10.2-all.zip

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake

替换 android\gradle\wrapper\gradle-wrapper.properties 中的 gradle 下载链接为国内腾讯云的链接:

#distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-all.zip
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.11-all.zip

注意,不要使用 8.10 版本(有bug,见文末),需要改成 8.11 版本。

Dependency requires at least JVM runtime version 11

Dependency requires at least JVM runtime version 11. This build uses a Java 8 JVM.

参考:

https://github.com/facebook/react-native/issues/33731

用 Android Studio 打开 react native 项目内的 android 目录。查看 jdk 目录,然后设置:

gradle.properties

org.gradle.java.home=D\:\\android_studio_2022\\jbr

或者去

https://mirrors.huaweicloud.com/openjdk/17/

下载个 jdk 也行。

NDK not configured

NDK not configured. Download it with SDK manager. Preferred NDK version is '26.1.10909125'.

打开 Android Studio, build.gradle sync 会自动提示下载的。

Failed to find Build Tools revision 35.0.0

Android Studio - System Settings - Android SDK 安装 Android API 35

如果需要安装 35, 需要点击右下角的 Show Package Details 才能看到 35. 由于设置了 34 也需要安装上。

诊断工具

npx react-native doctor

这个可以方便的查找问题。

Failed to resolve the Android SDK path

Failed to resolve the Android SDK path. Default install location not found: C:\Users\zhong\AppData\Local\Android\Sdk. Use ANDROID_HOME to set the Android SDK location.

java.io.UncheckedIOException: Could not move temporary workspace

java.io.UncheckedIOException: Could not move temporary workspace (D:\work\rasa_mobile\android.gradle\8.10.2\dependencies-accessors\569c8b261a8a714d7731d5f568e0e5c05babae10-2a37d69b-af29-4152-89dd-915156af273f) to immutable location (D:\work\rasa_mobile\android.gradle\8.10.2\dependencies-accessors\569c8b261a8a714d7731d5f568e0e5c05babae10)

https://github.com/gradle/gradle/issues/28475

regrading this issue. in react native 76 version . please use the gradlew 8.11 we resolve your issue. thanks

distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-8.11-all.zip

编译成 APK

  • https://reactnative.dev/docs/signed-apk-android
  • https://medium.com/@tywosemail/building-an-apk-file-for-a-react-native-android-project-involves-several-steps-e97d1294aafc

参考这两个文档,默认安装到手机上的 APK 只是个壳子,需要开发机上的 server 才能加载运行。

只有配置了证书之后,才能生成真正的 APK。

微信关注我哦 👍

大象工具微信公众号

我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式