简体   繁体   English

Android:在 android 工作室中进行 NDK-Build C/C++ 调试

[英]Android: NDK-Build C/C++ debug in android studio

Trying ndk-build C code debug in Android studio(v4.1.2).Below is build.gradle setup.在 Android 工作室(v4.1.2)中尝试 ndk-build C 代码调试。下面是 build.gradle 设置。

  1. jni, C/C++ Source files Dirs. jni, C/C++ 源文件 Dirs. Actual C files are not under jni folder but outside it but referred in android.mk file.实际的 C 文件不在 jni 文件夹下,而是在它之外,但在 android.mk 文件中引用。

    sourceSets.main.jniLibs.srcDirs = ['D:/ccodefolder/jni/'] sourceSets.main.jniLibs.srcDirs = ['D:/ccodefolder/jni/']

2)Android.mk Builds a shared library from C, c++ code and also links inbuilt shared and static libraries. 2)Android.mk 从 C、c++ 代码构建共享库,并链接内置共享库和 static 库。

 externalNativeBuild {
           ndkBuild {
               path file('D:/ccodefolder/jni/Android.mk')
           }
       }
       ndkVersion '21.1.6352462'
debug {
        debuggable true
        jniDebuggable true
        minifyEnabled false
        shrinkResources false
       //ndk.debugSymbolLevel = 'FULL'
    }

4)Project structure is as in the image. 4) 项目结构如图所示。

在此处输入图像描述

Able to run the project and shared library get generated along with other prebuilt.SOs and apk works, also CPP folder is created and able to see C code files of my project.能够运行项目并生成共享库以及其他 prebuilt.SO 和 apk 作品,还创建了 CPP 文件夹并能够查看我的项目的 C 代码文件。

DEBUGGING ISSUE:调试问题:

LLDB server gets started and Debugger attached to process, But debug any C file is failing with below error. LLDB 服务器启动并且调试器附加到进程,但是调试任何 C 文件失败并出现以下错误。

Breakpoint will not currently be hit.断点当前不会被命中。 No executable code is associated with this line没有可执行代码与此行相关联

在此处输入图像描述

Thanks谢谢

There are a couple things you can try, I've had success with each solution.您可以尝试几件事,每个解决方案我都取得了成功。

From the NDK Bug Tracker :来自NDK 错误跟踪器

Select Run -> Edit Configurations Select 运行 -> 编辑配置

On the left side, select Android App -> app在左侧,select Android App -> app

On the right side, select the Debugger tab在右侧,select 调试器选项卡

Under the Symbol Directories sub-tab, press the + button在符号目录子选项卡下,按 + 按钮

Browse to the following path:浏览到以下路径:

libModule/build/intermediates/cmake/debug/obj In the Build Variants window, select the "debug" configuration for the app libModule/build/intermediates/cmake/debug/obj在 Build Variants window, select 应用程序的“调试”配置

The other solution I've found that sometimes works is to only add breakpoints inside C++ code ONLY.我发现有时可行的另一个解决方案是仅在 C++ 代码中添加断点。 The Android Studio debugger does not handle the transition from Java/Kotlin to C++ well. Android Studio 调试器不能很好地处理从 Java/Kotlin 到 C++ 的转换。 I have found that by removing all Java breakpoints, restarting Android Studio, and launching debug mode again, the C++ breakpoints will now be hit (though any Java breakpoints now added will be missed). I have found that by removing all Java breakpoints, restarting Android Studio, and launching debug mode again, the C++ breakpoints will now be hit (though any Java breakpoints now added will be missed).

  1. Have you used Proguard before?你以前用过 Proguard 吗? Check these SO answers , specially the one regarding adding the lines to proguard-rules.pro检查这些SO 答案,特别是关于将行添加到proguard-rules.pro的答案

  2. Also check if you have debug type set to Java Only .还要检查您是否将调试类型设置为Java Only For this go to the top bar, in the 'app' selector go to Edit Configurations.对于此 go 到顶部栏,在“应用程序”选择器 go 中编辑配置。 Select 'app' then go to Debugger. Select 'app' 然后 go 到调试器。

  3. It can also be because of Instant Run.也可能是因为 Instant Run。 To disable Instant run go to File > Settings > Build, execution, deployment > Instant run > Uncheck Enable Instant Run要禁用即时运行 go 到文件 > 设置 > 构建、执行、部署 > 即时运行 > 取消选中启用即时运行

  4. Another issue might be Android Studio is in offline mode.另一个问题可能是 Android Studio 处于离线模式。 To disable Offline mode: File > Settings (Ctrl-Alt-S) > Build, Execution, Deployment > Gradle Under Global gradle settings: Offline work checkbox要禁用离线模式:文件 > 设置 (Ctrl-Alt-S) > 构建、执行、部署 > Gradle 在全局 gradle 设置下:离线工作复选框

  5. Last but not least.最后但并非最不重要的。 Have you tried Invalidate Caches/Restart?您是否尝试过使缓存无效/重新启动? Go to File -> Invalidate Caches/Restart. Go 到文件 -> 使缓存无效/重新启动。 This will clean the cache that might be messing up the debugger.这将清除可能会弄乱调试器的缓存。

Other answers are useful.其他答案很有用。

But the real problem for me was in my android.mk file having LOCAL_LDFLAGS --strip-all which didnt generate symbols for debugging.但对我来说真正的问题是在我的 android.mk 文件中,该文件具有 LOCAL_LDFLAGS --strip-all ,它没有生成用于调试的符号。 Once it was removed debugging is working.一旦它被删除,调试就可以工作了。

NDK team comments: https://issuetracker.google.com/issues/179634983 NDK 团队评论: https://issuetracker.google.com/issues/179634983

For the .so file(s) built by ndk-build:
Android Studio can automatically identify the location of the debugging symbol files.
Breakpoints should work without any additional effort.
For the .so file(s) that are included as prebuilt libraries using jniLibs.srcDirs:
Android Studio does not know where to find the debugging symbol files.
Breakpoints set on C++ files that were compiled into these .so libraries will show up as Breakpoint will not be hit. No executable code is associated with this line warnings, and won't hit.
You need to tell Android Studio where to find the symbol files for these .so files.
You can achieve this by adding the directory of your symbol files to Edit Configurations... | Debugger | Symbol Directories.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM