Android Studio调试含Native C++代码应用时与Realme 8i断开并崩溃
Native C++应用调试时连接断开并崩溃的解决方法
问题描述
在Android Studio中开发包含Native C++代码的应用,使用Realme 8i设备。应用部署后运行正常,但调试时出现异常:启动调试后能命中断点并查看变量,但几秒后与设备连接断开,应用随即崩溃。应用逻辑简单,不应出现此类问题。
调试窗口输出:
Debugger attached to process 29096 destroying when not connected to debugserver Process finished with exit code 22
Logcat警告及以上级别的日志:
2022-11-27 13:57:49.724 29568-29568/com.acme.acmecorptestapp02 W/ActivityThread: Application com.acme.acmecorptestapp02 is waiting for the debugger on port 8100... 2022-11-27 13:57:51.631 29568-29584/com.acme.acmecorptestapp02 W/System: A resource failed to call close. 2022-11-27 13:57:57.787 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: >>> msg's executing time is too long 2022-11-27 13:57:57.787 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: Blocked msg = { when=-8s145ms what=110 target=android.app.ActivityThread$H obj=AppBindData{appInfo=ApplicationInfo{3d00b62 com.acme.acmecorptestapp02}} } , cost = 8083 ms 2022-11-27 13:57:57.787 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: >>>Current msg List is: 2022-11-27 13:57:57.788 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: Current msg <1> = { when=-8s144ms what=9999 target=android.app.ActivityThread$H } 2022-11-27 13:57:57.790 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: Current msg <2> = { when=-8s142ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction LaunchActivityItem,ResumeActivityItem } 2022-11-27 13:57:57.792 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: Current msg <3> = { when=-8s141ms what=159 target=android.app.ActivityThread$H obj=ClientTransaction TopResumedActivityChangeItem, } 2022-11-27 13:57:57.793 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: Current msg <4> = { when=-8s124ms what=0 target=android.app.ActivityThread$H callback=com.android.internal.util.function.pooled.PooledLambdaImpl } 2022-11-27 13:57:57.794 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: Current msg <5> = { when=-7s809ms what=149 target=android.app.ActivityThread$H obj=android.os.BinderProxy@2d8e4f3 } 2022-11-27 13:57:57.794 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: Current msg <6> = { when=-7s124ms what=0 target=android.app.ActivityThread$H callback=android.app.-$$Lambda$ActivityThread$A4ykhsPb8qV3ffTqpQDklHSMDJ0 } 2022-11-27 13:57:57.794 29568-29568/com.acme.acmecorptestapp02 E/ANR_LOG: >>>CURRENT MSG DUMP OVER<<< 2022-11-27 13:57:57.927 29568-29659/com.acme.acmecorptestapp02 E/QT: [QT]file does not exist 2022-11-27 13:57:58.244 29568-29568/com.acme.acmecorptestapp02 E/OplusCustomizeRestrictionManager: sInstance is null, start a new sInstance 2022-11-27 13:57:58.801 29568-29568/com.acme.acmecorptestapp02 W/mecorptestapp0: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed) 2022-11-27 13:57:58.802 29568-29568/com.acme.acmecorptestapp02 W/mecorptestapp0: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
解决方法
1. 延长调试器超时时间
Android Studio默认调试超时较短,结合Realme定制ROM的后台限制,容易触发断开:
- 打开
Run/Debug Configurations,选中你的应用配置 - 切换到
Debugger选项卡,将Debugger timeout数值调大(比如设置为30000ms)
2. 关闭设备后台优化
Realme的ColorOS后台进程限制可能主动终止调试进程:
- 进入设备
设置 > 电池 > 应用耗电管理,找到目标应用,设置为无限制 - 关闭
纯净后台、智能耗电优化等相关功能
3. 检查Native调试配置
- 确认
build.gradle中debug模式开启调试符号:android { buildTypes { debug { jniDebuggable true debuggable true } } } - 执行
Build > Clean Project,再Build > Rebuild Project,清理旧调试缓存
4. 更换调试端口
端口8100可能被占用或受设备防火墙限制:
- 在
Run/Debug Configurations的Debugger选项卡,修改Port为其他值(比如8101) - 重启ADB服务:终端执行
adb kill-server,再执行adb start-server
5. 禁用Instant Run
Instant Run可能与Native调试冲突:
- 进入
File > Settings > Build, Execution, Deployment > Instant Run,取消勾选Enable Instant Run
6. 精简断点设置
若断点设置在频繁执行的代码块(如循环内),会导致调试器响应不及时:
- 暂时移除不必要的断点,只保留关键逻辑位置的断点测试
内容的提问来源于stack exchange,提问作者westman379
相关产品推荐
相关产品推荐

