当前位置: 首页 > 知识库问答 >
问题:

Fastlane android building: index.android.bundle missing

翟元凯
2023-03-14

我正在构建一个应用捆绑包(。aab)为我们的react原生应用。

当我通过Android Studio构建时,应用程序构建,部署到Play Store。我下载并运行——一切正常。

当我通过FastLane构建时,应用程序构建,部署到Play Store。但是我下载并运行,我得到以下错误:

E AndroidRuntime: java.lang.RuntimeException: Unable to load script. Make sure you're either running a Metro server (run 'react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.
E AndroidRuntime:   at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
E AndroidRuntime:   at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:2)
E AndroidRuntime:   at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:1)
E AndroidRuntime:   at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:3)
W ActivityTaskManager:   Force finishing activity com.friday.pax.dev/com.friday.pax.MainActivity
RN: 0.63.4
Gradle: 4.1.1

FastLane日志:

https://gist.githubusercontent.com/HardlyMirage/4426e411b6ec1e8abac5e7a620d979ae/raw/37daead0b55ee77b7ef7d6ddd8631d311dbc80aa/txt

index.android.bundle似乎已正确生成。

> Task :app:bundleDevReleaseJsAndAssets
warning: the transform cache was reset.
Welcome to React Native!
Learn once, write anywhere
info Writing bundle output to:, /home/runner/work/fridayapp-pax/fridayapp-pax/android/app/build/generated/assets/react/dev/release/index.android.bundle
info Writing sourcemap output to:, /home/runner/work/fridayapp-pax/fridayapp-pax/android/app/build/generated/sourcemaps/react/dev/release/index.android.bundle.map
info Done writing bundle output
info Done writing sourcemap output
info Copying 88 asset files
info Done copying assets

任务:app:mergeDevReleaseResources之后运行任务:app:copyDevReleaseBundledJs

快速文件:


def getVersionCode
  # Instead of managing the version code manually it is simply based on a timestamp in sec
  # Any build done more recently is considered to be a higher version
  # versionCode increase every minute (aka max 1 build per minute)
  # versionCode can't be smaller than legacyVersionCode
  thirtySeptemberTwentyTwenty = 1601480940 / 60
  legacyVersionCode = 10902
  versionCode = legacyVersionCode + (Time.now.to_i / 60) - thirtySeptemberTwentyTwenty

  if versionCode > 2100000000
    raise "versionCode cannot be higher than 2100000000"
  end

  versionCode.floor()
end

platform :android do

  desc "Friday (Dev)"
  lane :playstoreDevInternal do |options|
    
    # Retrieve version of my app in package.json (React Native case)
    package = load_json(json_path: "../package.json")

    # Clean build folder
    gradle(
      task: "clean"
    )

    # Bump android.defaultConfig.versionCode
    # bump_build_number
    android_set_version_code(
      version_code: package["versionName"]
    )

    # Do the bundle of the application  
    gradle(
      # task: "assemble",
      task: 'bundle',
      flavor: "dev",
      build_type: "Release",
      properties: {
        "android.injected.signing.store.file" => Dir.pwd + "/release.keystore",
        "android.injected.signing.store.password" => options[:RELEASE_KEYSTORE_PASSWORD], # keystore password
        "android.injected.signing.key.alias" => options[:RELEASE_KEYSTORE_ALIAS], # alias
        "android.injected.signing.key.password" => options[:RELEASE_KEYSTORE_KEY_PASSWORD], # key password
        "vname" => package["version"]
      }
    )

    # Upload Android App Bundle to PlayStore like Internal testing Release
    upload_to_play_store(
      package_name: 'com.friday.pax.dev',
      track: 'internal',
      release_status: 'draft',
      skip_upload_apk: true,
      version_name: package["version"]
    )
  end
end

有人能指出为什么Android Studio的aab包可以工作,但通过FastLane生成的包不行吗?即使index.android.bundle是明确生成的。

我唯一的想法是,也许index.android.bundle的道路是不正确的。但是我看不到修改这条路的方法

共有2个答案

龙承德
2023-03-14

对不起,我的英语很差..

编译 aab 时,RN 编译脚本 (react.gradle) 包含一个由 bundleJsAndAssets 命名的任务,它是异步的。当 copyBundledJs 和 mergeResources 的任务被执行时,bundleJsAndAssets 仍在执行。所以 AAB 中错过了 JS 捆绑包

解决方案:

1.如果您的资产目录有捆绑文件,请将其删除。

2 .将这段代码添加到您的app/build.gradle中

project.afterEvaluate {
def isAndroidLibrary = plugins.hasPlugin("com.android.library")
def variants = isAndroidLibrary ? android.libraryVariants : android.applicationVariants
variants.all { def variant ->
    def targetName = variant.name.capitalize()
    def mergeResourcesTask = tasks.findByName("merge${targetName}Resources")
    def assetsCopyTask = tasks.findByName("copy${targetName}BundledJs")
    def bundleAndroidTask = tasks.findByName("bundle${targetName}JsAndAssets")
    mergeResourcesTask.dependsOn(assetsCopyTask)
    assetsCopyTask.dependsOn(bundleAndroidTask)
    logger.warn("Making task ${mergeResourcesTask} depend on ${assetsCopyTask}")
    logger.warn("Making task ${assetsCopyTask} depend on ${bundleAndroidTask}")
}}
漆雕升
2023-03-14

结果是,gradle脚本正在将捆绑包生成到特定于构建变体的文件夹中,但无论构建变体如何,应用程序都在默认位置查找捆绑包:

Gradle脚本生成的包位置(风格:开发,类型:发布):

android/app/build/generated/assets/react/dev/release/index.android.bundle

应用程序查看位置:

android/app/src/main/assets/index.android.bundle

所以我在包中添加了一个脚本。json来生成:

"generate:bundle": "react-native bundle --platform android --dev false --bundle-output android/app/src/main/assets/index.android.bundle --entry-file index.js"

在运行runner脚本中的build命令之前,我运行脚本来生成build:

npm run generate:build

应用现在可以找到捆绑包。

 类似资料:

相关问答

相关文章

相关阅读