react-native-qrcode-generator在Android端崩溃,求替代hardwareAcceleration的方案
React Native Android二维码崩溃解决方案(替代全局硬件加速禁用)
问题背景
使用react-native-qrcode-generator生成二维码时,iOS端运行正常,Android端出现Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR)崩溃。禁用全局硬件加速可修复崩溃,但会导致应用其他部分卡顿,需寻求替代方案。
可行解决方案
1. 仅给二维码页面/组件禁用硬件加速
无需全局关闭硬件加速,只针对包含二维码的页面或组件单独设置:
- 针对Activity:修改
AndroidManifest.xml
<activity android:name=".MainActivity" android:hardwareAccelerated="false"> <!-- 保留原有其他配置 --> </activity>
- 针对单个View:在原生代码中给二维码对应的View禁用硬件加速(适合Fragment或动态加载场景)
// 在Fragment的onViewCreated方法中调用 qrCodeView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
2. 替换为纯JS实现的二维码库
放弃依赖原生渲染的react-native-qrcode-generator,改用纯JS库彻底避免原生层崩溃:
示例:使用react-native-qrcode-svg
安装:
npm install react-native-qrcode-svg
使用代码:
import QRCode from 'react-native-qrcode-svg'; function QrCodeGenerator() { return ( <QRCode value="你的二维码内容" size={200} color="#000" backgroundColor="#fff" /> ); }
示例:使用qrcode生成base64图片
安装:
npm install qrcode
使用代码:
import React, { useState, useEffect } from 'react'; import { Image } from 'react-native'; import qrcode from 'qrcode'; function QrCodeImage({ value }) { const [qrBase64, setQrBase64] = useState(''); useEffect(() => { qrcode.toDataURL(value, (err, url) => { if (!err) setQrBase64(url); }); }, [value]); return qrBase64 ? ( <Image source={{ uri: qrBase64 }} style={{ width: 200, height: 200 }} /> ) : null; }
3. 升级依赖版本
检查react-native-qrcode-generator的最新版本,看是否有修复Android崩溃的补丁;同时升级React Native到稳定版本(如0.70+),可能解决底层渲染的兼容性问题。
崩溃日志
2023-03-02 19:30:34.608 20117-20341 HostConnection com.example D HostConnection::get() New Host Connection established 0x7a8d1d29ae50, tid 20341 2023-03-02 19:30:34.608 20151-20186 cr_LibraryLoader com.example I Loaded native library version number "91.0.4472.114" 2023-03-02 19:30:34.609 20151-20186 cr_CachingUmaRecorder com.example I Flushed 1 samples from 1 histograms. 2023-03-02 19:30:34.612 20117-20172 cr_media com.example W Requires BLUETOOTH permission 2023-03-02 19:30:34.622 20117-20341 HostConnection com.example D HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_vulkan_queue_submit_with_commands ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_2 2023-03-02 19:30:34.625 20117-20341 chromium com.example E [ERROR:gl_surface_egl.cc(584)] eglChooseConfig failed with error EGL_SUCCESS 2023-03-02 19:30:34.644 20117-20341 EGL_emulation com.example D eglCreateContext: 0x7a8d1d29b690: maj 2 min 0 rcv 2 2023-03-02 19:30:34.654 20117-20341 EGL_emulation com.example D eglMakeCurrent: 0x7a8d1d29b690: ver 2 0 (tinfo 0x7a8f315ef100) (first time) 2023-03-02 19:30:34.656 20151-20186 SystemServiceRegistry com.example W No service published for: uimode 2023-03-02 19:30:34.678 20117-20341 chromium com.example E [ERROR:gl_surface_egl.cc(584)] eglChooseConfig failed with error EGL_SUCCESS 2023-03-02 19:30:34.724 20117-20341 EGL_emulation com.example D eglCreateContext: 0x7a8d1d29a3d0: maj 2 min 0 rcv 2 2023-03-02 19:30:34.794 20117-20341 EGL_emulation com.example D eglCreateContext: 0x7a8d1d29af10: maj 2 min 0 rcv 2 2023-03-02 19:30:34.822 20117-20341 EGL_emulation com.example D eglCreateContext: 0x7a8d1d298690: maj 2 min 0 rcv 2 2023-03-02 19:30:35.102 20117-20188 libc com.example A Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x20 in tid 20188 (RenderThread), pid 20117 (ect.example) 2023-03-02 19:30:35.497 20357-20357 DEBUG pid-20357 A Cmdline: com.example 2023-03-02 19:30:35.498 20357-20357 DEBUG pid-20357 A pid: 20117, tid: 20188, name: RenderThread >>> com.example <<< 2023-03-02 19:30:35.540 603-20362 ActivityTaskManager system_process W Force finishing activity com.example/.MainActivity 2023-03-02 19:30:35.729 603-2461 ActivityManager system_process W Scheduling restart of crashed service com.example/org.chromium.content.app.SandboxedProcessService0:0 in 1000ms for connection 2023-03-02 19:30:35.737 603-3342 WindowManager system_process I WIN DEATH: Window{e2f6367 u0 com.example/com.example.MainActivity} 2023-03-02 19:30:35.738 603-3342 InputManager-JNI system_process W Input channel object 'e2f6367 com.example/com.example.MainActivity (client)' was disposed without first being removed with the input manager! 2023-03-02 19:30:35.743 603-1762 ActivityManager system_process I Process com.example (pid 20117) has died: prcp TOP 2023-03-02 19:30:35.752 603-748 ConnectivityService system_process D releasing NetworkRequest [ REQUEST id=267, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 10158 RequestorUid: 10158 RequestorPkg: com.example] ] (release request) 2023-03-02 19:30:35.753 603-748 ConnectivityService system_process D releasing NetworkRequest [ REQUEST id=265, [ Capabilities: INTERNET&NOT_RESTRICTED&TRUSTED&NOT_VCN_MANAGED Uid: 10158 RequestorUid: 10158 RequestorPkg: com.example] ] (release request) 2023-03-02 19:30:36.043 603-637 ActivityTaskManager system_process W Activity top resumed state loss timeout for ActivityRecord{31932b7 u0 com.example/.MainActivity t-1 f}
内容的提问来源于stack exchange,提问作者Anupriya
相关产品推荐
相关产品推荐

