快车道 android 建筑:index.android.bundle 缺失

Fastlane android building: index.android.bundle missing

我正在为我们的 React 本机应用程序构建一个应用程序包 (.aab)。

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

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

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.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

Task :app:mergeDevReleaseResourcesTask :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 路径可能不正确。但是我没有看到修改该路径的方法。

事实证明,gradle 脚本将包生成到特定于构建变体的文件夹中,但应用程序正在默认位置查找包,而不管构建变体如何:

Gradle 脚本生成的包位置(类型:dev,类型:release):

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

正在查看位置的应用:

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

所以我在 package.json 中添加了一个脚本来生成这个:

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

在我的 运行ner 脚本中 运行 构建命令之前,我 运行 生成构建的脚本:

npm run generate:build

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

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

在编译aab时,RN编译脚本(react.gradle)包含一个名为bundleJsAndAssets的任务,它是异步的。当 copyBundledJs 和 mergeResources 的任务执行完毕后,bundleJsAndAssets 还在执行。所以 JS 包在 aab

中丢失了

解法:

1.If您的资产目录有一个捆绑文件,将其删除

2.add 这个代码在你的 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}")
}}