因为我是反应原生的新手,所以如果步骤中有任何错误,请告诉我。
我根据文档
使用命令构建了一个react native android应用程序react-native android
在设备上运行时,使用了以下命令
react-native run-android
在我的 projectfolder / android / app / build / outputs / apk中提供了2个apk文件的输出
现在当我在安装后使用安装这个apk时,它要求开发服务器连接到捆绑JS。但我的要求是,用户不需要为开发服务器而烦恼,只需要安装apk即可完成所有操作。
已经完成了一些stackoverflow Questions但没有帮助构建不需要开发服务器的unsigned apk。
你能帮助我找到如何在反应原生中构建和签名的apk吗?
答案 0 :(得分:187)
您需要手动为调试版本创建捆绑包。
捆绑调试版本:
#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`
P.S。另一种方法可能是修改gradle脚本。
答案 1 :(得分:61)
请按照这些步骤进行操作。
捆绑你的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/
答案 2 :(得分:23)
对我来说,在项目目录中运行以下命令。
对于本机旧版本(您将在root中看到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 ../
对于本机新版本(您只在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文件将在以下位置生成:
答案 3 :(得分:4)
答案 4 :(得分:3)
对于Windows用户,如果所有步骤都正确遵循:https://facebook.github.io/react-native/docs/signed-apk-android.html
您只需要运行: gradlew assembleRelease
您的文件将是:
位置: E:\YourProjectName\android\app\build\outputs\apk
答案 5 :(得分:3)
在您的根项目目录中
请确保您已经拥有目录 android / app / src / main / assets / (如果没有),请在创建新文件后创建目录并另存为 index.android.bundle ,然后将文件放入 android / app / src / main / assets / index.android.bundle
在cmd中运行
react-native软件包--platform android --dev false --entry-file index.js --bundle-output android / app / src / main / assets / index.android.bundle --assets-目标android / app / src / main / res /
cd android && ./gradlew assembleDebug
然后您可以获取apk app / build / outputs / apk / debug / app-debug.apk
答案 6 :(得分:3)
如果您真的想生成一个可以无需开发服务器即可运行的调试 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 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-debug.apk
目录中创建 android/app/build/outputs/apk/debug
文件,您可以在没有 dev-server 的情况下安装和运行该文件。
答案 7 :(得分:2)
可以生成未签名的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
答案 8 :(得分:2)
从反应本机0.57开始,以前提供的答案将不再起作用,因为gradle希望在其中找到捆绑包的目录和资产已更改。
构建调试版本的最简单方法是完全不使用react-native bundle
命令,而只需修改app/build.gradle
文件即可。
在project.ext.react
文件的app/build.gradle
映射内,添加bundleInDebug: true
条目。
如果您希望它不是--dev
的版本(没有警告和缩小的捆绑包),则还应该将devDisabledInDebug: true
条目添加到同一地图。
如果出于某些原因您需要或想要使用react-native bundle
命令创建捆绑包,然后使用./gradlew assembleDebug
创建包含捆绑包和资产的APK,则必须确保放置捆绑软件和资产位于正确的路径中,gradle可以在其中找到它们。
从本机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查找包和资产的路径可能会更改。要找出这些路径在哪里,请查看react.gradle
目录中的node_modules/react-native
文件。以def jsBundleDir =
和def resourcesDir =
开头的行分别指定gradle在其中查找包和资产的目录。
答案 9 :(得分:2)
您可以使用两个扩展程序。这被添加到 react-native 以设置这些:
disableDevInDebug: true
:在调试构建类型中禁用开发服务器bundleInDebug: true
:添加 jsbundle 以调试 buildType。因此,project.ext.react
中的最终 android/app/build.gradle
应如下所示
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
]
答案 10 :(得分:1)
我对原生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,您可以使用开发服务器正常运行
注意:
index.js
gradlew assembleRelease
仅生成无法安装的未发布的apks 答案 11 :(得分:1)
以防万一其他人最近也遇到了同样的问题,我正在使用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
运行cd android/ && ./gradlew assembleDebug
在android / app / build / outputs / apk / debug文件夹中获取您的app-debug.apk
祝你好运!
答案 12 :(得分:0)
我找到了一个更改buildType的解决方案,
CREATE TABLE "users" (
"id" varchar(36) NOT NULL,
"email" varchar(30) NOT NULL UNIQUE,
"password" varchar(200) NOT NULL,
"created_at" TIMESTAMP NOT NULL,
"updated_at" TIMESTAMP NOT NULL,
CONSTRAINT users_pk PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);
答案 13 :(得分:0)
聚会很晚,但没有一个解决方案 at least on page 1
对我不起作用。因此,我更深入地研究了 react 注册的成绩任务;
只需添加 bundleInDebug : true
,如@Er Suman G 的回答所示
根据您的 android 文件夹中的环境运行 ./gradlew assembleDebug
或 gradle.bat assembleDebug
。
这将在 android/app/build/outputs/apk/debug
答案 14 :(得分:-1)
转到android文件夹并运行./gradlew assembleRelease
以构建可以发送到Android设备的发布文件。发布apk不需要运行dev服务器。