如何在 android 上使用离线包来响应本机项目? [英] How to use offline bundle on android for react native project?

查看:12
本文介绍了如何在 android 上使用离线包来响应本机项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 android 上使用离线包?
我没有看到关于在 android 上使用离线包的文档.
我试图取消注释 build.gradle 中的代码.

How to use offline bundle on android?
I didn`t see the document about use offline bundle on android.
I tried to uncomment the code in 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 包.
有什么我错过的吗?

but it didn`t work.It still fetch the JS bundle from the server.
Is there something I missed?

推荐答案

为了将JS离线打包到android中,首先在各自的项目路径下启动服务器:

For offline bunduling of JS into android, first start the server in respective project path:

  1. 当服务器启动时,打开与项目路径相同的下一个终端
  2. 复制并粘贴此命令:在命令 propmt 中复制和粘贴命令之前,在项目各自的路径中创建资产文件夹
    如:
    android/app/src/main/assets

  1. when server start, open next terminal with same path as project path
  2. copy and paste this command: Before you copy and paste command in command propmt, make assets folder in project respective path
    as:
    android/app/src/main/assets

将此命令粘贴到命令提示符中并运行:

paste this command in command prompt and run:

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/

  1. 然后在assets文件夹中会出现index.android.bundle文件
  2. 最后,运行命令:react-native run-android(在构建新的离线apk时您不需要启动服务器,您的离线js文件将帮助您构建apk文件.)
  3. 最终,apk 现在构建已准备好在不同设备上运行(从 app/src/build/debug.apk 运行 apk).
  4. 有时新制作的 apk 运行时不显示图像,如果应用程序运行时没有图像,则将特定的图像资源文件夹复制并粘贴到 android/app/src/main/assets/(图像源文件夹)
  5. 再次重新运行应用程序,这样构建的 apk 就可以运行了.

这篇关于如何在 android 上使用离线包来响应本机项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆