C++ 如何让 NDK 调试在 Android Studio 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32413593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:08:14  来源:igfitidea点击:

How to get NDK debugging to work in Android Studio?

c++android-studioandroid-ndklldb

提问by PadThai

Android Studio doesn't stop at breakpoints in C++ code, this is what i've done so far :

Android Studio 不会在 C++ 代码中的断点处停止,这是我迄今为止所做的:

  1. In AndroidManifest.xml :

    android:debuggable="true"
    
  2. In build.gradle (this may be the problem):

    sourceSets.main {
      jniLibs.srcDir 'src/main/libs'
      jni.srcDirs = []
    }
    
    task ndkBuild(type: Exec) {
      commandLine android.ndkDirectory.getAbsolutePath() + '\' + 'ndk-build.cmd', '-C', file('src/main/jni').absolutePath, 'NDK_DEBUG=1'
    }
    
    tasks.withType(JavaCompile) {
      compileTask -> compileTask.dependsOn ndkBuild
    }
    
    1. Configured the application as a native application on Android Studio

    2. Put breakpoints in C++ code

    3. Debug the app

  1. 在 AndroidManifest.xml 中:

    android:debuggable="true"
    
  2. 在 build.gradle 中(这可能是问题所在):

    sourceSets.main {
      jniLibs.srcDir 'src/main/libs'
      jni.srcDirs = []
    }
    
    task ndkBuild(type: Exec) {
      commandLine android.ndkDirectory.getAbsolutePath() + '\' + 'ndk-build.cmd', '-C', file('src/main/jni').absolutePath, 'NDK_DEBUG=1'
    }
    
    tasks.withType(JavaCompile) {
      compileTask -> compileTask.dependsOn ndkBuild
    }
    
    1. 将应用程序配置为 Android Studio 上的本机应用程序

    2. 在 C++ 代码中放置断点

    3. 调试应用程序

This seems to work because it is saying : "Now Launching Native Debug Session" moreover I can pause the app with the stop button but no breakpoint is working.

这似乎有效,因为它说:“现在启动本机调试会话”而且我可以使用停止按钮暂停应用程序但没有断点工作。

Thank you for your help

感谢您的帮助

采纳答案by VitalyD

By the syntax of your build.gradle looks like you don't use the experimental plugin for gradle, without it you wont be able to debug native c/c++ in android studio. For more information read this : Android NDK Preview

根据您的 build.gradle 的语法,您似乎没有使用gradle实验性插件,否则您将无法在 android studio 中调试本机 c/c++。有关更多信息,请阅读:Android NDK 预览版

回答by Martin Zeitler

With LLDBinstalled, one has native / dual debugging available.

随着LLDB安装,一个具有本地/双调试可用。

Run/Debug Configurations

运行/调试配置

回答by Hong

  1. In Run->Debug Configuration "Debugger" tab, choose Debug type as "Native". In the field of "Before launch", if Android studio reports conflicts, accept the recommendation for 'fix' it. Android studio will download lldb library.

  2. click on the 'debug' bottom and wait until debugger attached to the process.

  3. now you can see the variables in the debug windown.

  1. 在运行->调试配置“调试器”选项卡中,选择调试类型为“本机”。在“Before launch”字段中,如果Android Studio 报告冲突,请接受“修复”建议。Android studio 会下载 lldb 库。

  2. 单击“调试”底部并等待调试器连接到进程。

  3. 现在您可以在调试窗口中看到变量。

回答by Jomo Fisher

If you're still looking, Android Studio has recently added support for direct integration of ndk-build and CMake projects: http://tools.android.com/tech-docs/external-c-builds

如果您还在寻找,Android Studio 最近增加了对 ndk-build 和 CMake 项目直接集成的支持:http: //tools.android.com/tech-docs/external-c-builds

Kind regards, Jomo

亲切的问候,乔莫

回答by sunalong

you can try this:

你可以试试这个:

1.in app/build.gradle:

1.在app/build.gradle中:

at dependencies label:

在依赖项标签处:

releaseCompile project(path:':youModuleName',configuration:'release')
debugCompile project(path:':youModuleName',configuration:'debug')

2.in youModule/build.gradle:

2.在youModule/build.gradle中:

at android label:

在 android 标签:

publishNonDefault true

demo:

演示:

https://github.com/sunalong/JNIDemo

https://github.com/sunalong/JNIDemo