我已经构建了我的应用程序,我可以在我的本地模拟器上运行它(也可以在同一网络内通过更改调试服务器在我的android设备上运行)。
然而,我想构建一个APK,我可以发送给没有访问开发服务器的人,我希望他们能够测试应用程序。
我看到有一节使用离线捆绑在iOS部分的文档。但我不知道如何在android上实现同样的功能。这可能吗?如果有,怎么做?
更新:关于这个问题的答案(Android无法加载JS包),据说离线包可以从开发服务器下载。但是当我从开发服务器获取包时,图像文件无法加载。
我已经构建了我的应用程序,我可以在我的本地模拟器上运行它(也可以在同一网络内通过更改调试服务器在我的android设备上运行)。
然而,我想构建一个APK,我可以发送给没有访问开发服务器的人,我希望他们能够测试应用程序。
我看到有一节使用离线捆绑在iOS部分的文档。但我不知道如何在android上实现同样的功能。这可能吗?如果有,怎么做?
更新:关于这个问题的答案(Android无法加载JS包),据说离线包可以从开发服务器下载。但是当我从开发服务器获取包时,图像文件无法加载。
当前回答
参考react-native关于生成签名APK的官方文档
原生生成签名APK
其他回答
一些有用的脚本为windows用户
在包中复制粘贴这些代码。“script”下的Json文件:{}
* VS Code
--Enable NPM SCRIPT
--Now you can directly run scripts from here
"android": "react-native run-android",
"clean-run-android": "cd android && ./gradlew clean && cd.. && react-native run-android ",
"start": "node node_modules/react-native/local-cli/cli.js start --reset-cache",
"reset": "rm -rf node_modules/ && npm cache clear && watchman watch-del-all && npm i",
"debug-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 && cd app/src/main/res/ && rmdir /S /Q raw && rmdir /S /Q drawable-mdpi",
"release-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/build/intermediates/res/merged/release/ && cd android && ./gradlew assembleRelease && cd..",
"install-apk": "cd android && ./gradlew installRelease",
"release-aab": "cd android && ./gradlew bundleRelease"
您必须创建一个密钥来签署apk。使用下面的方法创建你的密钥:
keytool -genkey -v -keystore my-app-key.keystore -alias my-app-alias -keyalg RSA -keysize 2048 -validity 10000
在提示时输入密码
生成密钥后,使用它来生成可安装的版本:
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/
使用gradle生成构建
cd android && ./gradlew assembleRelease
上传APK到你的手机。-r标志将替换现有的应用程序(如果它存在的话)
adb install -r ./app/build/outputs/apk/app-release-unsigned.apk
这里提到了更详细的描述:https://facebook.github.io/react-native/docs/signed-apk-android.html
更新:根据@shashuec和@Fallen的评论
如果出现错误
ENOENT:没有这样的文件或目录,打开'android/app/src/main/assets/index.android.bundle'
执行mkdir android/app/src/main/assets命令
你应该使用android studio来完成这个过程。这样更简单。但是首先在react原生应用目录下运行这个命令:
对于react-native的更新版本(例如:React原生0.49.0等等…)
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
对于react-native的旧版本(0.49.0及以下)
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/
然后使用android studio在你的react原生应用目录中打开“android”文件夹,它会要求升级gradle和其他一些东西。去build->生成签名APK,并按照那里的指示。这真的很直接。
根据Aditya Singh的回答,生成的(未签名的)apk不会安装在我的手机上。我必须使用这里的说明生成一个签名apk。
下面的方法对我很有效:
$ keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
把我的释放钥匙放上去。android/app下的Keystore文件 目录。然后编辑文件 ~ / .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=****
如果你使用的是MacOS,你可以使用这里的说明将密码存储在钥匙链中,而不是以明文形式存储。
然后编辑app/build。gradle并确保存在以下内容(可能需要添加带有signingConfigs signingConfig的部分):
...
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 assemblerrelease命令,
对于Windows, cd android,然后执行gradlew assemblerrelease命令,在android/app/build/outputs/apk/app-release.apk或android/app/build/outputs/apk/release/app-release.apk下找到签名的apk
Android Studio中的GUI方式
Have the Android app part of the React Native app open in Android Studio (this just means opening the android folder of your react-native project with Android Studio) Go to the "Build" tab at the top Go down to "Build Bundle(s) / APK(s)" Then select "Build APK(s)" This will walk you through a process of locating your keys to sign the APK or creating your keys if you don't have them already which is all very straightforward. If you're just practicing, you can generate these keys and save them to your desktop. Once that process is complete and if the build was successful, there will be a popup down below in Android Studio. Click the link within it that says "locate" (the APK) That will take you to the apk file, move that over to your android device. Then navigate to that file on the Android device and install it.