react-native 运行-android build 在没有红屏的情况下启动时崩溃

react-native run-android build crashes on launch without red screen

我正在尝试在 android 上构建我的应用程序的调试版本。离线版本工作正常。 (使用 react-native bundle ...)。但当然,它不会触发地铁捆绑器。此外,捆绑器正在监听构建,当在浏览器上调用 https://localhost:8081/index.bundle?platform=android 时,它开始捆绑。 我试过:

  1. 正在删除 build 文件夹
  2. 正在生成新的 debug.keystore
  3. 运行宁./gradlew clean
  4. watchman watch-del-all
  5. rm -rf .gradle
  6. rm -rf node_modules
  7. rm -rf package-lock.json
  8. npm i
  9. 运行 与 npx react-native run-android

以及几乎所有的清理和重置。但是还是没有机会。这是一个高度定制的项目,我无法更新 react-native 版本或清理 android 文件夹。

这是我的 react-native info 结果:

System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    Memory: 417.64 MB / 8.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 12.14.1 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.7 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 28, 29
      Build Tools: 28.0.3, 29.0.2
      System Images: android-28 | Intel x86 Atom_64, android-29 | Intel x86 Atom_64
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6392135
    Xcode: 11.3.1/11C504 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0 
    react-native: 0.61.5 => 0.61.5

这是我在模拟器上启动应用程序时的 logcat:

09-03 14:48:11.523  8400  9820 I ActivityTaskManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.myapp.demo/com.myapp.MainActivity bnds=[35,503][237,780]} from uid 10083
09-03 14:48:11.751  8400  8440 I ActivityManager: Start proc 12348:com.myapp.demo/u0a111 for activity {com.myapp.demo/com.myapp.MainActivity}
09-03 14:48:11.751 12348 12348 I m.myapp.dem: Not late-enabling -Xcheck:jni (already on)
09-03 14:48:11.782 12348 12348 E m.myapp.dem: Unknown bits set in runtime_flags: 0x8000
09-03 14:48:11.783 12348 12348 W m.myapp.dem: Unexpected CPU variant for X86 using defaults: x86_64
09-03 14:48:11.857 12348 12348 I m.myapp.dem: The ClassLoaderContext is a special shared library.
09-03 14:48:12.303  1652  1652 E Layer   : [Surface(name=AppWindowToken{71e8676 token=Token{370511 ActivityRecord{b1c4138 u0 com.myapp.demo/com.myapp.MainActivity t37}}})/@0x93ca15a - animation-leash#0] No local sync point found
09-03 14:48:12.303  1652  1652 E Layer   : [Surface(name=AppWindowToken{71e8676 token=Token{370511 ActivityRecord{b1c4138 u0 com.myapp.demo/com.myapp.MainActivity t37}}})/@0x93ca15a - animation-leash#0] No local sync point found
09-03 14:48:12.321  1652  1652 E Layer   : [Surface(name=AppWindowToken{71e8676 token=Token{370511 ActivityRecord{b1c4138 u0 com.myapp.demo/com.myapp.MainActivity t37}}})/@0x93ca15a - animation-leash#0] No local sync point found
09-03 14:48:12.422 12348 12376 I FA      :   adb shell setprop debug.firebase.analytics.app com.myapp.demo
09-03 14:48:12.452 12348 12348 D SoLoader: adding application source: com.facebook.soloader.DirectorySoSource[root = /data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64 flags = 0]
09-03 14:48:12.453 12348 12348 D SoLoader: adding backup source from : com.facebook.soloader.ApkSoSource[root = /data/data/com.myapp.demo/lib-main flags = 1]
09-03 14:48:12.453 12348 12348 D SoLoader: Preparing SO source: com.facebook.soloader.DirectorySoSource[root = /data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64 flags = 0]
09-03 14:48:12.453 12348 12348 D SoLoader: Preparing SO source: com.facebook.soloader.ApkSoSource[root = /data/data/com.myapp.demo/lib-main flags = 1]
09-03 14:48:12.455 12348 12348 V fb-UnpackingSoSource: locked dso store /data/user/0/com.myapp.demo/lib-main
09-03 14:48:12.456 12348 12348 I fb-UnpackingSoSource: dso store is up-to-date: /data/user/0/com.myapp.demo/lib-main
09-03 14:48:12.456 12348 12348 V fb-UnpackingSoSource: releasing dso store lock for /data/user/0/com.myapp.demo/lib-main
09-03 14:48:12.583 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform-api, linking, allowed)
09-03 14:48:12.584 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
09-03 14:48:12.584 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
09-03 14:48:12.584 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
09-03 14:48:12.587 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
09-03 14:48:12.588 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
09-03 14:48:12.589 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->allocateInstance(Ljava/lang/Class;)Ljava/lang/Object; (greylist, linking, allowed)
09-03 14:48:12.589 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->arrayBaseOffset(Ljava/lang/Class;)I (greylist,core-platform-api, linking, allowed)
09-03 14:48:12.589 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->arrayIndexScale(Ljava/lang/Class;)I (greylist, linking, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->peekLong(JZ)J (greylist, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->pokeLong(JJZ)V (greylist, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->pokeInt(JIZ)V (greylist, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->peekInt(JZ)I (greylist, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->pokeByte(JB)V (greylist, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->peekByte(J)B (greylist, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->pokeByteArray(J[BII)V (greylist, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->peekByteArray(J[BII)V (greylist, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, reflection, allowed)
09-03 14:48:12.592 12348 12404 W m.myapp.dem: Accessing hidden field Ljava/nio/Buffer;->address:J (greylist, reflection, allowed)
09-03 14:48:12.593 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, reflection, allowed)
09-03 14:48:12.593 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, reflection, allowed)
09-03 14:48:12.593 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, reflection, allowed)
09-03 14:48:12.593 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, reflection, allowed)
09-03 14:48:12.593 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, reflection, allowed)
09-03 14:48:12.593 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, reflection, allowed)
09-03 14:48:13.115 12348 12393 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
09-03 14:48:13.121 12348 12393 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
09-03 14:48:13.121 12348 12393 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
09-03 14:48:13.122 12348 12393 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
09-03 14:48:13.122 12348 12393 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
09-03 14:48:13.122 12348 12393 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
09-03 14:48:13.122 12348 12393 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
09-03 14:48:13.127 12348 12393 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
09-03 14:48:13.129 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
09-03 14:48:13.139 12348 12348 W RenderThread: type=1400 audit(0.0:54): avc: denied { write } for name="property_service" dev="tmpfs" ino=8201 scontext=u:r:untrusted_app:s0:c111,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.myapp.demo
09-03 14:48:13.263 12348 12348 D SoLoader: libjscexecutor.so not found on /data/data/com.myapp.demo/lib-main
09-03 14:48:13.263 12348 12348 D SoLoader: libjscexecutor.so found on /data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64
09-03 14:48:13.277 12348 12413 D SoLoader: libhermes.so not found on /data/data/com.myapp.demo/lib-main
09-03 14:48:13.277 12348 12413 D SoLoader: libhermes.so found on /data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64
09-03 14:48:13.295 12348 12413 D SoLoader: libhermes-executor-release.so not found on /data/data/com.myapp.demo/lib-main
09-03 14:48:13.295 12348 12413 D SoLoader: libhermes-executor-release.so found on /data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64
09-03 14:48:13.317 12348 12413 D SoLoader: libfb.so not found on /data/data/com.myapp.demo/lib-main
09-03 14:48:13.317 12348 12413 D SoLoader: libfb.so found on /data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64
09-03 14:48:13.320 12348 12413 D SoLoader: libfb.so not found on /data/data/com.myapp.demo/lib-main
09-03 14:48:13.320 12348 12413 D SoLoader: libfb.so found on /data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64
09-03 14:48:13.320 12348 12413 I m.myapp.dem: Thread[39,tid=12413,Native,Thread*=0x7d6527e8b800,peer=0x16f77c58,"create_react_context"] recursive attempt to load library "/data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64/libfb.so"
09-03 14:48:13.320 12348 12348 W m.myapp.dem: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
09-03 14:48:13.321 12348 12348 W m.myapp.dem: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
09-03 14:48:13.329 12348 12348 D FirebasePerformance: onResume(): com.myapp.MainActivity: 1089086 microseconds
09-03 14:48:13.332 12348 12348 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
09-03 14:48:13.332 12348 12348 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
09-03 14:48:13.335 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
09-03 14:48:13.335 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
09-03 14:48:13.336 12348 12404 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
09-03 14:48:13.403 12348 12413 W InstanceID: Instance ID SDK is deprecated, com.myapp.demo should update to use Firebase Instance ID
09-03 14:48:13.437 12348 12413 I m.myapp.dem: The ClassLoaderContext is a special shared library.
09-03 14:48:13.598  8400  8438 I ActivityTaskManager: Displayed com.myapp.demo/com.myapp.MainActivity: +2s74ms
09-03 14:48:13.622 12348 12420 W m.myapp.dem: Accessing hidden method Landroid/content/Context;->bindServiceAsUser(Landroid/content/Intent;Landroid/content/ServiceConnection;ILandroid/os/Handler;Landroid/os/UserHandle;)Z (greylist, reflection, allowed)
09-03 14:48:13.658  8400  8440 I ActivityManager: Start proc 12425:com.android.webview:sandboxed_process0:org.chromium.content.app.SandboxedProcessService0/u0i12 for  {com.myapp.demo/org.chromium.content.app.SandboxedProcessService0}
    09-03 14:48:13.815 12348 12413 D SoLoader: libreactnativejni.so not found on /data/data/com.myapp.demo/lib-main
09-03 14:48:13.815 12348 12413 D SoLoader: libreactnativejni.so found on /data/app/com.myapp.demo-tzqejRbWBgXm8igk7j-Npw==/lib/x86_64
09-03 14:48:13.910 12348 12361 I m.myapp.dem: Background young concurrent copying GC freed 72355(9202KB) AllocSpace objects, 26(776KB) LOS objects, 65% free, 3229KB/9373KB, paused 2.318ms total 125.689ms
09-03 14:48:13.912 12348 12413 E AndroidRuntime: Process: com.myapp.demo, PID: 12348
09-03 14:48:13.939 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
09-03 14:48:13.939 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
09-03 14:48:13.939 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
09-03 14:48:13.939 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
09-03 14:48:13.944 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, linking, allowed)
09-03 14:48:13.946 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, linking, allowed)
09-03 14:48:13.954 12348 12376 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->pokeByte(JB)V (greylist, reflection, allowed)
09-03 14:48:13.954 12348 12376 W m.myapp.dem: Accessing hidden method Llibcore/io/Memory;->peekByte(J)B (greylist, reflection, allowed)
09-03 14:48:13.955 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, reflection, allowed)
09-03 14:48:13.955 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, reflection, allowed)
09-03 14:48:13.956 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, reflection, allowed)
09-03 14:48:13.956 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getLong(Ljava/lang/Object;J)J (greylist,core-platform-api, reflection, allowed)
09-03 14:48:13.956 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putLong(Ljava/lang/Object;JJ)V (greylist, reflection, allowed)
09-03 14:48:13.956 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, reflection, allowed)
09-03 14:48:13.956 12348 12376 W m.myapp.dem: Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, reflection, allowed)
09-03 14:48:13.980  8400 10689 W ActivityTaskManager:   Force finishing activity com.myapp.demo/com.myapp.MainActivity
09-03 14:48:14.039  8400  8493 W InputDispatcher: channel 'a546a57 com.myapp.demo/com.myapp.MainActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
09-03 14:48:14.039  8400  8493 E InputDispatcher: channel 'a546a57 com.myapp.demo/com.myapp.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
    09-03 14:48:14.062  8400 10382 I ActivityManager: Process com.myapp.demo (pid 12348) has died: vis+99 TOP
09-03 14:48:14.096  8400  8841 I WindowManager: WIN DEATH: Window{a546a57 u0 com.myapp.demo/com.myapp.MainActivity}
09-03 14:48:14.096  8400  8841 W InputDispatcher: Attempted to unregister already unregistered input channel 'a546a57 com.myapp.demo/com.myapp.MainActivity (server)'

这是调试的 build.gradle 设置:

buildTypes {
        debug {
            signingConfig signingConfigs.debug
            applicationIdSuffix ".demo"
            versionNameSuffix " - Dev"
            resValue "string", "app_name", "My App"
        }

我相信自定义以某种方式进行了一些更改,构建版本实际上不是显示调试红屏并连接到开发服务器的调试版本。但是我找不到要更改的源。

我还在我的笔记本电脑上安装了一个 react-native init --version 0.61.5,它可以安装、运行并连接到 Metro 服务器,非常棒。 因此,如果您有任何进一步深入检查配置和 android 文件夹的建议,我将不胜感激。我已经尝试了很多关于 Whosebug 的问题以及关于 react-native、react-native-CLI 和 metro repos 的问题,但找不到我需要的任何高级指南。

在 Whosebug 上发布问题的魔力奏效了,经过几次尝试后我找到了答案。 显然,问题出在 MainApplication.java 中导入的 BuildConfig 文件。这会导致程序使用导入的构建配置而不是此文件中的调试配置:

@Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

因为库在其构建配置中使用生产配置,并且当我们导入它时,编译器会阻止调试功能。这就是它无法连接到地铁捆绑器的原因。

因此,如果您正在查看此问题并遇到同样的问题,请务必检查 MainApplication.java 并删除您找到的任何外部 BuildConfig,例如:

import com.facebook.react.BuildConfig;
import androidx.multidex.BuildConfig;

在我的例子中,第二个在代码中。 执行此操作后,请确保删除离线包(如果有):

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

并删除 android 构建文件夹:

rm -rf android/app/build

或:

cd android && ./gradlew clean

现在您可以 运行 react-native run-android 它有望成功构建 运行 并连接到 bunder。