如何将android上的脱机捆绑包用于react本机项目?
如何在android上使用离线捆绑?如何将android上的脱机捆绑包用于react本机项目?,android,react-native,Android,React Native,如何在android上使用离线捆绑? 我没有看到关于在android上使用离线捆绑包的文档。 我试图取消build.gradle中的代码注释 project.ext.react = [ // the name of the generated asset file containing your JS bundle bundleAssetName: "index.android.bundle", // the entry file for bundle gene
我没有看到关于在android上使用离线捆绑包的文档。
我试图取消build.gradle中的代码注释
project.ext.react = [
// the name of the generated asset file containing your JS bundle
bundleAssetName: "index.android.bundle",
// the entry file for bundle generation
entryFile: "index.android.js",
// whether to bundle JS and assets in debug mode
bundleInDebug: false,
// whether to bundle JS and assets in release mode
bundleInRelease: true,
// the root of your project, i.e. where "package.json" lives
root: "../../",
// where to put the JS bundle asset in debug mode
jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
// where to put the JS bundle asset in release mode
jsBundleDirRelease: "$buildDir/intermediates/assets/release",
// where to put drawable resources / React Native assets, e.g. the ones you use via
// require('./image.png')), in debug mode
resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
// where to put drawable resources / React Native assets, e.g. the ones you use via
// require('./image.png')), in release mode
resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
// by default the gradle tasks are skipped if none of the JS files or assets change; this means
// that we don't look at files in android/ or ios/ to determine whether the tasks are up to
// date; if you have any other folders that you want to ignore for performance reasons (gradle
// indexes the entire tree), add them here. Alternatively, if you have JS files in android/
// for example, you might want to remove it from here.
inputExcludes: ["android/**", "ios/**"]
]
但它没有工作。它仍然从服务器获取JS包。有什么我遗漏的吗?您可以阅读
react.gradle
的源代码,然后您将知道如何进行脱机捆绑
在react.gradle
中,它为每个构建变量创建脱机捆绑gradle任务,并使其在gradle处理资源之前执行,但在某些特殊情况下启用捆绑任务,代码为:
enabled config."bundleIn${targetName}" ||
config."bundleIn${buildTypeName.capitalize()}" ?:
targetName.toLowerCase().contains("release")
而config
对象实际上是定义为的react
对象
def config=project.hasProperty(“react”)?project.react:[]代码>
而targetName
的定义是def targetName=“${productFlavorName.capitalize()}${buildTypeName.capitalize()}”
因此,如果我们在app\build.gradle
中定义适当的react
属性,就可以启用脱机捆绑任务
例如,如果我们想在release
生成类型中启用脱机捆绑包,而不是在debug
生成类型中启用脱机捆绑包,我们可以将react
定义为:
ext.react = [
bundleInDebug : false,
bundleInRelease : true
]
然后在命令行中执行gradleassemblererelease
,gradle将执行bundle任务,js bundle将包含在最终apk中
现在在您的问题中,您已经定义了适当的react
对象,但是您没有提到您运行的构建类型。只有执行gradle assembleerelease
才能执行捆绑作业,可能您执行了gradle assembleedbug
,因此没有任何意义
还有一个问题我需要说(也请参见)。如果您为发布构建类型启用了捆绑任务并从android studio运行应用程序,gradle不会执行bundleReleaseJsAndAssets
,因为android studio默认情况下会启用功能Configure on demand
(位于文件|设置|构建、执行、部署|编译器中)以加快构建速度,在react.gradle
中,当所有项目都被配置(或称为评估)时,捆绑任务被添加到项目中,因为主代码在gradle.projectsEvaluated{}
块中
按需配置模式尝试仅配置以下项目:
与请求的任务相关,即它只执行
参与生成的项目的build.gradle文件
由于某些不清楚的原因,当启用按需配置
功能时,gradle.projectsEvaluated{}
块中定义的任何任务都不会执行。要使其可执行,请禁用android studio设置中的Configure on demand
功能,或将gradle.projectsEvaluated{}
更改为afterEvaluate{}
如果在命令行工具中运行gradleassemblererelease
,一切正常,因为默认情况下configureondemand
处于禁用状态 要将JS离线绑定到android,首先在各自的项目路径中启动服务器:
服务器启动时,打开与项目路径相同路径的下一个终端
复制并粘贴此命令:
在命令propmt中复制和粘贴命令之前,请在项目各自的路径中创建资产文件夹
as:
android/app/src/main/assets
在命令提示符下粘贴此命令并运行:
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/
然后在assets文件夹中,会出现index.android.bundle文件
最后,run命令:react native run android(在构建新的脱机apk时,您不需要启动服务器,您的脱机js文件将帮助您构建apk文件。)
最后,apk现在构建已经准备好在不同的设备上运行(从app/src/build/debug.apk运行apk)
有时新制作的apk运行时不会显示图像,如果应用程序运行时没有图像,则将特定的图像资源文件夹复制并粘贴到android/app/src/main/assets/(图像源文件夹)
再次运行应用程序,这样构建apk就可以运行了
您不必取消对gradle.build文件中的任何内容的注释。它是供参考的,如果需要,您可以覆盖作为默认值存在的任何值
问题是Android Studio没有运行负责生成捆绑包的任务
project.ext.react = [
bundleAssetName: "index.myapp.bundle",
entryFile: "js/index.myapp.js",
bundleInDebug: true
]
要在Android Studio中修复此问题,请转到首选项>构建、执行、部署>构建工具>编译器
,然后取消选中“按需配置”
现在,您标记为需要bundle的任何变体都将自动生成bundle
默认情况下,调试变量不会生成捆绑包:bundleInDebug:false
你可以这样改变。这里我还提供了如何更改入口点的默认位置和包的默认名称的示例
project.ext.react = [
bundleAssetName: "index.myapp.bundle",
entryFile: "js/index.myapp.js",
bundleInDebug: true
]
一旦您进行了这些调整,您可能仍然会进入问题构建,因为Studio无法识别shell配置文件中的路径,所以我可能找不到节点的路径
这是一个稳定而稳健的解决方案。在环境中进行这些更改后,无需手动执行任何操作。当您按下IDE中的按钮时,捆绑包将自动生成,前提是您选择了正确的生成变体(也在IDE中,位于Studio窗口底部左侧栏上)。用于带签名的APK发布。
我尝试使用一个未签名的APK文件并安装它,但当我在Android平板电脑上安装它时,它给了我一个失败的错误[install\u PARSE\u FAILED\u NO\u CERTIFICATES]
我想这是因为APK文件没有签名,平板电脑也没有签名
adb -d install -r <path_to_signed_apk>
@Nullable
@Override
protected String getJSBundleFile() {
return "/your/bundle/path/bundle.file.name.bundle";
}
import { AppRegistry } from "react-native";
import App from "./App";
AppRegistry.registerComponent("VOS", () => App);
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 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 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\