调试和发布 apk 在 React Native 0.63.3 中不起作用

Posted

技术标签:

【中文标题】调试和发布 apk 在 React Native 0.63.3 中不起作用【英文标题】:Debug and Release apk not working in React Native 0.63.3 【发布时间】:2021-05-27 17:01:39 【问题描述】:

我正在使用以下步骤来构建调试和发布 apk

    cd android && ./gradlew clean && cd .. cd android && ./gradlew cleanBuildCache && cd .. npx react-native bundle --platform android --dev true --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android /app/src/main/res --verbose cd android && ./gradlew assembleDebug && cd .. 或 cd android && ./gradlew assembleRelease && cd ..

apk 生成成功。但是当我运行它们时,我总是得到这个。

但是,如果我启动 Metro-server,应用程序可以正常工作,但 debug-apk 不能。

任何帮助将不胜感激。

安卓配置:

 buildToolsVersion = "29.0.2"
 minSdkVersion = 23
 compileSdkVersion = 29
 targetSdkVersion = 29

 classpath 'com.google.gms:google-services:4.1.0'

清单

<application
    android:name=".MainApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true"
    >

应用的 build.gradle

project.ext.react = [
    entryFile: "index.js",
    bundleAssetName: "index.android.bundle",
    enableHermes: false,  // clean and rebuild if changing
    devDisabledInDev: true, // Disable dev server in dev release
    bundleInDev: true, // add
    bundleInDebug: true
]

谢谢

【问题讨论】:

在主终端的项目目录中运行 npx react-native start 或在运行 npx react-native start 后在 VS 中运行 npm react-native run-android 我也试过了,但是当我生成构建并安装该 apk 文件时,我得到了同样的错误。 如果您在模拟器上运行应用程序,请尝试react-native run-android --variant=debug 查看dev.to/nitish173/… 我能够成功生成构建,问题是当我从存储安装它时,它给了我这个错误。 【参考方案1】:

在项目目录的终端中:-

cd android 然后./gradlew clean 然后cd ..

然后 //创建包npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

然后 //创建调试版本npx react-native run-android variant=debug

和 //创建发布版本npx react-native run-android variant=release

注意:- 确保你已经在项目根目录中运行了npm start

【讨论】:

感谢 +1 的回答,我试过了。

以上是关于调试和发布 apk 在 React Native 0.63.3 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在 React Native 中为 iOS 生成调试 IPA

React Native:调试和发布版本的工作方式不同

React-Native build release apk 执行 aapt 失败

任务:react-native-screens:androidJavadoc FAILED

react-native 打包Apk实践版

React Native v0.59.2 android签名apk卡在启动画面