2

在 Android Studio(v4.1.2) 中尝试 ndk-build C 代码调试。下面是 build.gradle 设置。

  1. jni , C/C++ 源文件 Dirs. 实际的 C 文件不在 jni 文件夹下,而是在它之外,但在 android.mk 文件中引用。

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

2)Android.mk 从 C、c++ 代码构建一个共享库,并链接内置的共享库和静态库。

 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) 项目结构如图所示。

在此处输入图像描述

能够运行项目并生成共享库以及其他预构建的 .SO 和 apk 作品,还创建了 CPP 文件夹并能够查看我的项目的 C 代码文件。

调试问题:

LLDB 服务器启动并且调试器附加到进程,但调试任何 C 文件失败并出现以下错误。

断点当前不会被命中。没有可执行代码与此行相关联

在此处输入图像描述

谢谢

4

3 回答 3

2

您可以尝试几件事,每个解决方案我都取得了成功。

来自NDK 错误跟踪器

选择运行 -> 编辑配置

在左侧,选择 Android App -> app

在右侧,选择调试器选项卡

在符号目录子选项卡下,按 + 按钮

浏览到以下路径:

libModule/build/intermediates/cmake/debug/obj 在 Build Variants 窗口中,选择应用程序的“调试”配置

我发现有时可行的另一个解决方案是仅在 C++ 代码中添加断点。Android Studio 调试器不能很好地处理从 Java/Kotlin 到 C++ 的转换。我发现通过删除所有 Java 断点、重新启动 Android Studio 并再次启动调试模式,现在将命中 C++ 断点(尽管现在添加的任何 Java 断点都将丢失)。

于 2021-02-14T10:19:23.350 回答
1

其他答案很有用。

但对我来说真正的问题是在我的 android.mk 文件中有 LOCAL_LDFLAGS --strip-all 没有生成用于调试的符号。一旦它被删除,调试就可以工作了。

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.
于 2021-02-18T22:07:04.787 回答
1
  1. 你以前用过 Proguard 吗?检查这些SO 答案,特别是关于添加行的答案proguard-rules.pro

  2. 还要检查您是否将调试类型设置为Java Only。为此,请转到顶部栏,在“应用程序”选择器中转到编辑配置。选择“应用程序”,然后转到调试器。

  3. 也可能是因为 Instant Run。要禁用即时运行,请转到文件 > 设置 > 构建、执行、部署 > 即时运行 > 取消选中启用即时运行

  4. 另一个问题可能是 Android Studio 处于离线模式。要禁用离线模式:文件 > 设置 (Ctrl-Alt-S) > 构建、执行、部署 > Gradle 在全局 gradle 设置下:离线工作复选框

  5. 最后但并非最不重要的。您是否尝试过使缓存无效/重新启动?转到文件-> 使缓存无效/重新启动。这将清除可能会弄乱调试器的缓存。

于 2021-02-16T19:59:35.773 回答