由于我是 react-native 的新手,所以如果步骤中有任何问题,请告诉我。
我已经按照文档使用命令构建了一个反应原生 android 应用程序
反应原生安卓
在设备上运行时,使用了以下命令
react-native run-android
这给了我 projectfolder/android/app/build/outputs/apk 中 2 个 apk 文件的输出
https://i.stack.imgur.com/B2r4Q.png
现在,当我在安装后安装此 apk 时,它会要求开发服务器连接以捆绑 JS。但我的要求是用户不必为开发服务器而苦苦挣扎,他只需要安装 apk 就可以了。
已经经历了一些 stackoverflow Questions 但对构建不需要开发服务器的未签名 apk 没有帮助。
你们能帮我找到如何在本机反应中构建和未签名的apk的方法吗?
您需要为调试构建手动创建捆绑包。
捆绑调试版本:
#React-Native 0.59
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/src/main/assets/index.android.bundle --assets-dest ./android/app/src/main/res
#React-Native 0.49.0+
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug
#React-Native 0-0.49.0
react-native bundle --dev false --platform android --entry-file index.android.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug
然后在捆绑后构建APK:
$ cd android
#Create debug build:
$ ./gradlew assembleDebug
#Create release build:
$ ./gradlew assembleRelease #Generated `apk` will be located at `android/app/build/outputs/apk`
PS 另一种方法可能是修改 gradle 脚本。
请按照这些步骤操作。
捆绑你的js:
如果您在项目根目录中有 index.android.js 然后运行
react-native bundle --dev false --platform android --entry-file index.android.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug
如果你在项目根目录中有 index.js 然后运行
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
创建调试 apk:
cd android/
./gradlew assembleDebug
然后你可以在这里找到你的 apk:
cd app/build/outputs/apk/
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug
反应版本 0.62.1
在您的项目根目录中
确保您已经有目录 android/app/src/main/assets/,如果没有创建目录,则创建新文件并另存为 index.android.bundle
并像这样将文件放入 android/app/src/main/assets/index.android.bundle
之后运行这个
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/
cd android && ./gradlew assembleDebug
然后您可以在 app/build/outputs/apk/debug/app-debug.apk 中获取 apk
index.android.bundle
的点对于不在主机中使用开发服务器非常重要。
和我一起,在项目目录中运行以下命令。
对于 react native 旧版本(您将在根目录中看到 index.android.js):
mkdir -p android/app/src/main/assets && rm -rf android/app/build && 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 && cd android && ./gradlew clean assembleRelease && cd ../
对于 react native 新版本(你只在 root 中看到 index.js):
mkdir -p android/app/src/main/assets && rm -rf android/app/build && 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 && cd android && ./gradlew clean assembleRelease && cd ../
apk 文件将在以下位置生成:
Gradle < 3.0:android/app/build/outputs/apk/
Gradle 3.0+:android/app/build/outputs/apk/release/
$ mkdir -p android/app/src/main/assets && rm -rf android/app/build && 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 && cd android && ./gradlew assembleDebug
index.js
而不是 index.android.js
的较新项目,需要将 --entry-file index.android.js
替换为 --entry-file index.js
。
遵循 first response 后,您可以使用
react-native run-android --variant=debug
您的应用程序无需打包程序即可运行
从 react-native 0.57 开始,之前提供的答案都不再有效,因为 gradle 期望在其中找到捆绑包和资产的目录已更改。
没有 react-native bundle 的简单方法
构建调试版本的最简单方法是完全不使用 react-native bundle
命令,而只需修改您的 app/build.gradle
文件。
在 app/build.gradle
文件的 project.ext.react
映射中,添加 bundleInDebug: true
条目。如果您希望它不是一个 --dev
构建(没有警告和缩小的捆绑包),那么您还应该将 devDisabledInDebug: true
条目添加到同一个地图。
使用 react-native 捆绑包
如果由于某种原因您需要或想要使用 react-native bundle
命令创建捆绑包,然后使用 ./gradlew assembleDebug
创建包含捆绑包和资产的 APK,您必须确保将捆绑包和资产放入正确的路径,gradle 可以在其中找到它们。
从 react-native 0.57 开始,这些路径是捆绑包的 android/app/build/generated/assets/react/debug/index.android.js
android/app/build/generated/res/react/debug
用于资产。因此,使用捆绑包和资产手动捆绑和构建 APK 的完整命令是:
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/generated/assets/react/debug/index.android.bundle --assets-dest ./android/app/build/res/react/debug
接着
./gradlew assembleDebug
捆绑和资产路径
请注意,gradle 查找捆绑包和资产的路径可能会发生变化。要找出这些路径的位置,请查看 node_modules/react-native
目录中的 react.gradle
文件。以 def jsBundleDir =
和 def resourcesDir =
开头的行分别指定 gradle 查找包和资产的目录。
react-native bundle
命令不匹配。并且没有一个变体最终以 bundle 成为 React Native 0.63 的 APK 的一部分。请参阅 bug reported to React Native CLI docs 中的说明。
bundleInDebug
的建议。这是正确的方法。当有 Gradle 脚本处理所有事情时,使用 react-native bundle
毫无意义。
在没有开发服务器的情况下生成调试 APK
如果您真的想生成一个调试 APK(用于测试目的),可以在没有开发服务器的情况下运行,恭喜!我来帮你。 :)
每个人都说我们需要运行两个命令 react-native bundle ...
然后 gradlew assembleDebug
但生成的 APK 在没有开发服务器的情况下仍然无法工作。经过多次研究&试试我发现我们需要更改 gradle 文件(如步骤 2 所示)。只需按照以下步骤操作:
在您的反应原生项目中,转到 /android/app/src/main 创建文件夹 assets 编辑 android > app > build.gradle
project.ext.react = [
...
bundleInDebug: true, // add this line
]
在你的 react native 项目的根目录下运行这个命令
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
现在,进入 android 文件夹: cd android 并运行以下命令: gradlew assembleDebug (如果构建失败,请尝试从 android studio 构建 assembleDebug)
这将在 android/app/build/outputs/apk/debug
目录中创建 app-debug.apk
文件,您可以安装 &在没有开发服务器的情况下运行。
您可以为此使用两个扩展。这被添加到 react-native 以设置这些:
disableDevInDebug:true:在调试 buildType 中禁用开发服务器 bundleInDebug:true:将 jsbundle 添加到调试 buildType。
因此,您在 android/app/build.gradle
中的最终 project.ext.react
应如下所示
project.ext.react = [
enableHermes: false, // clean and rebuild if changing
devDisabledInDev: true, // Disable dev server in dev release
bundleInDebug: true, // add bundle to dev apk
]
devDisabledInDev
和 bundleInDebug
都应设置为 true!
对于 Windows 用户,如果所有步骤都正确执行:https://facebook.github.io/react-native/docs/signed-apk-android.html
您只需运行: gradlew assembleRelease
你的文件将是:
应用程序-release.apk
应用发布-unaligned.apk
位置: E:\YourProjectName\android\app\build\outputs\apk
可以生成一个未签名的 apk 版本用于测试目的,以便您可以在手机上运行。
最初我得到了这里最提到的红屏错误。但我遵循了这里提到的相同方法,它对我有用。
在工作目录的控制台上,运行这四个命令
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug
cd android
gradlew assembleDebug
gradlew assembleRelease
然后APK文件将在:android\app\build\outputs\apk\debug\app-debug.apk
以防万一其他人最近遇到同样的问题,我使用的是 React Native 0.59.8(也用 RN 0.60 进行了测试),我可以确认其他一些答案,以下是步骤:
卸载设备上安装的应用程序的最新编译版本运行 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 run cd android/ && ./gradlew assembleDebug 在文件夹 android/app/build/outputs/apk/debug 中获取您的 app-debug.apk
祝你好运!
android/app/build/generated/assets/react/debug/index.android.js
中,资产位于 android/app/build/generated/res/react/debug
中。
聚会很晚,但没有一个解决方案 at least on page 1
对我不起作用。因此,我对 react 注册的成绩任务进行了更深入的研究;
只需添加 bundleInDebug : true
如@Er Suman G 的答案所示
根据您的环境在您的 android 文件夹中运行 ./gradlew assembleDebug
或 gradle.bat assembleDebug
。
这将在 android/app/build/outputs/apk/debug
中生成可调试的非服务器必需应用程序
我在 react native 0.55.4,基本上我必须手动捆绑:
react-native bundle --dev false --platform android --entry-file index.js --bundle-
output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-
dest ./android/app/build/intermediates/res/merged/debug
然后通过 USB 连接您的设备,启用 USB 调试。使用 adb devices
验证连接的设备。
最后运行 react-native run-android
,它将在您的手机上安装调试 apk,您可以使用开发服务器正常运行它
笔记:
从 0.49.0 开始,入口点是单个 index.js
gradlew assembleRelease 只生成无法安装的未签名的apks
我找到了一个像这样改变 buildTypes 的解决方案:
buildTypes {
release {
signingConfig signingConfigs.release
}
}
在 build.gradle 文件中将 devDisabledInDebug: true
添加到 project.ext.react
。
在没有开发服务器的情况下生成调试 APK
如果您想生成可以在没有开发服务器的情况下运行的调试 APK(用于测试目的)。
在您的反应原生项目中,转到 /android/app/src/main 创建文件夹 assets 编辑 android > app > build.gradle
universalApk true // If true, also generate a universal APK
在你的 react native 项目的根目录下运行这个命令
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
现在,进入 android 文件夹: cd android 并运行以下命令: gradlew assembleDebug (如果构建失败,请尝试从 android studio 构建 assembleDebug)
只做这件事。
如果您想在设备中安装应用程序,只需运行 cmd
adb install 'path/add-debug.apk'
2021年
按照 Aditya Singh 的回答,生成的(未签名的)apk 不会安装在我的手机上。我必须使用说明 here 生成签名的 apk。
以下对我有用:
$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
将 my-release-key.keystore 文件放在项目文件夹中的 android/app 目录下。然后编辑文件 ~/.gradle/gradle.properties 并添加以下内容(将 **** 替换为正确的密钥库密码、别名和密钥密码)
MYAPP_RELEASE_STORE_FILE=my-release-key.keystore
MYAPP_RELEASE_KEY_ALIAS=my-key-alias
MYAPP_RELEASE_STORE_PASSWORD=****
MYAPP_RELEASE_KEY_PASSWORD=****
然后编辑 app/build.gradle 并确保存在以下内容(可能需要添加带有 signingConfigs 的部分):
...
android {
...
defaultConfig { ... }
signingConfigs {
release {
if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
storeFile file(MYAPP_RELEASE_STORE_FILE)
storePassword MYAPP_RELEASE_STORE_PASSWORD
keyAlias MYAPP_RELEASE_KEY_ALIAS
keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}
}
...
然后运行命令 cd android && ./gradlew assembleRelease
,
对于 Windows 'cd android' 然后 run gradlew assembleRelease
command ,并在 android/app/build/outputs/apk/app-release.apk,
或 下找到您的 signed apk android/app/build/outputs/apk/release/app-release.apk
--assets-dest ./android/app/src/main/res/