简体   繁体   English

是否可以在Android Studio中调试C / C ++?

[英]Is it possible to debug C/C++ in Android Studio?

I'm writing an Android app that includes C/C++ code that is compiled alongside the Java code. 我正在编写一个Android应用程序,其中包含与Java代码一起编译的C / C ++代码。 My app is crashing during the execution of the native code. 我的应用程序在执行本机代码期间崩溃。

Is there a way to step through Android C/C++ source and inspect variables as it is possible with Java source? 有没有办法逐步完成Android C / C ++源代码并检查变量,因为Java源代码可以实现?

I am not necessarily looking for a way to step through the source from within Android Studio . 我不一定在寻找从Android Studio中逐步浏览源代码的方法。 That is obviously the ideal solution, however, if I need to use an external tool after starting the app from Studio, that's fine. 这显然是理想的解决方案,但是,如果我需要在从Studio启动应用程序后使用外部工具,那很好。 I want to be able to step through the execution line-by-line and inspect variables. 我希望能够逐行执行并检查变量。

Android Studio 1.0.2 Android Studio 1.0.2
Android NDK r10d Android NDK r10d

Edit: Stemming from the immediate response of, "Use Eclipse with ADT plugin", let me add more requirements. 编辑:根据“使用带ADT插件的Eclipse”的即时响应,让我添加更多要求。
This project started as an Eclipse ADT project. 该项目最初是作为Eclipse ADT项目开始的。 However, I have now migrated to Android Studio. 但是,我现在已迁移到Android Studio。 My project is now built using Gradle, and my project directory structure reflects this. 我的项目现在使用Gradle构建,我的项目目录结构反映了这一点。 I am willing to use Eclipse; 我愿意使用Eclipse; I am not willing to change my project structure in order to revert to Eclipse from Android Studio. 舍得换为了我的项目结构,以恢复从Android的工作室到Eclipse。 If there is a way to open the Android Studio project in Eclipse and debug it that way, please elaborate on the process. 如果有办法在Eclipse中打开Android Studio项目并以这种方式进行调试,请详细说明该过程。

[UPDATE] [UPDATE]

As of July 2015, Android Studio DOES support NDK. 截至2015年7月,Android Studio DOES支持NDK。

You can learn more about it on this link . 您可以在此链接上了解有关它的更多信息。


[OLD] NDK is not yet supported in Android Studio. Android Studio尚不支持[OLD] NDK。

When we have to deal with NDK, the only solution is to use Eclipse. 当我们必须处理NDK时,唯一的解决方案是使用Eclipse。

EDIT 编辑

We basically keep Eclipse project with NDK feature (map in our case) and Gradle project with other (non-map) features. 我们基本上将Eclipse项目与NDK功能(在我们的例子中为map)和Gradle项目与其他(非地图)功能保持在一起。 So everything that has to be done with NDK, we do in Eclipse and then include changes into Gradle project. 所以必须用NDK完成所有工作,我们在Eclipse中完成,然后将更改包含在Gradle项目中。

To be more precise, inside directory /src/main/ we created another directory called jniLibs and put the compiled *.so file inside architecture-specific folders (armeabi-v7a, armeabi, x86...). 更确切地说,在目录/src/main/我们创建了另一个名为jniLibs目录,并将已编译的*.so文件放在特定于体系结构的文件夹(armeabi-v7a,armeabi,x86 ...)中。 Then in the main class of Gradle project simply added a line 然后在Gradle项目的主类中简单地添加一行

static {
    System.loadLibrary("OurNDKLibName");
}

You can add this line only once per project. 每个项目只能添加一行此行。 Maybe I am wrong but we did not have any issues for doing so. 也许我错了,但我们没有任何问题。

As of version 1.3+ Preview Android Studio supports C++ debugging, quoting Android M Developer Preview & Tools : 从版本1.3+开始预览Android Studio支持C ++调试,引用Android M开发人员预览和工具

Most notable is a much requested feature from our Android NDK & game developers: code editing and debugging for C/C++ code. 最值得注意的是我们的Android NDK和游戏开发人员要求的功能:C / C ++代码的代码编辑和调试。 Based on JetBrains Clion platform, the Android Studio NDK plugin provides features such as refactoring and code completion for C/C++ code alongside your Java code. Android Studio NDK插件基于JetBrains Clion平台,提供诸如C / C ++代码的重构和代码完成等功能以及Java代码。 Java and C/C++ code support is integrated into one development experience free of charge for Android app developers. Java和C / C ++代码支持已集成到Android应用程序开发人员免费的一个开发体验中。

It isn't wasn't isn't working very well. 不是 不是 很好。

hello-jni fails failed with: hello-jni 失败 失败:

Starting LLDB server: run-as com.example.sample 
/data/data/com.example.sample/lldb/bin/start_lldb_server.sh 
    /data/data/com.example.sample/lldb 
        /data/data/com.example.sample/lldb/tmp/platform.port1442695333842 "lldb 
    process:gdb-remote packets"
Error while launching debug server on device: 
    com.android.tools.ndk.run.DebuggerContext$StartServerException: 
        java.lang.IllegalStateException: Failed to read platform port 
            /data/data/com.example.sample/lldb/tmp/platform.port1442695333842

I'm using 1.4RC1 我正在使用1.4RC1

compileSdkVersion 23
buildToolsVersion "23.0.1"

And the "app-native" run configuration @Gerry mentioned. 而@Gerry提到的“app-native”运行配置。 It's nearly there, but I can find no trace of an lldb directory in my app's private data folder. 它几乎就在那里,但我在我的应用程序的私有数据文件夹中找不到lldb目录的痕迹。 I do find a 400kb gdbserver in the lib directory linked from there, with my own *.so right besides. 我在lib目录中找到一个400kb的gdbserver ,从那里链接到我自己的*.so Unfortunately the option for debugging with gdb instead of lldb has gone with the upgrade from 1.3 to 1.4. 不幸的是,使用gdb而不是lldb进行调试的选项已经从1.3升级到1.4。 NDK 10e ships a 400kb gdbserver binary in ndk\\prebuilt\\android-arm\\gdbserver which it is (I guess). NDK 10e在ndk\\prebuilt\\android-arm\\gdbserver中发布了一个400kb的gdbserver二进制文件(我猜)。 On September 15th the gdb debug howto https://github.com/mapbox/mapbox-gl-native/wiki/Android-debugging-with-remote-GDB was updated. 9月15日,gdb调试如何更新https://github.com/mapbox/mapbox-gl-native/wiki/Android-debugging-with-remote-GDB This is harder than logcat debugging IMO, but a current topic, sorry to be so negative but I don't believe lldb actually works yet. 这比logcat调试IMO更难,但是当前的主题,遗憾的是如此消极,但我不相信lldb实际上有效。

I have run hello-jni and reached the same conclusion. 我已经运行了hello-jni并得出了同样的结论。

So in answer to your question, it is not yet possible to debug C/C++ in Android Studio. 因此,在回答您的问题时,尚无法在Android Studio中调试C / C ++。 Sorry this wasn't the answer you hoped for and I hope to be proved wrong with conclusive evidence to the contrary, but right here, right now, no chance! 对不起,这不是你所希望的答案,我希望用相反的确凿证据证明是错的,但就在这里,现在,没有机会!

UPDATE I raised an issue where I was given a fix for one phone. 更新我提出了一个问题 ,我给了一个手机修复。

UPDATE2 Too early to tell, it is experimental after all, but I am occasionally failing to connect, often if not always getting out of order stepping and breakpoints with nonsense parameter values. UPDATE2太早告诉它,它毕竟是实验性的,但我偶尔会无法连接,通常如果不总是出现故障的步进和断点与无意义的参数值。 I just set a conditional breakpoint that didn't fire but the logcat shows it should. 我只是设置了一个没有触发的条件断点但是logcat显示它应该。 Initially I assumed it was my logical error, but it goes deeper. 最初我认为这是我的逻辑错误,但它更深入。

Simply hitting breakpoints at all is very helpful but it is still a million miles short of Java debugging, or Vis Studio C++ debugging. 简单地点击断点非常有用,但它仍然比Java调试或Vis Studio C ++调试还要短一百万英里。

UPDATE3 Gone back to stable 1.3.1 as the canaray channel version was pausing for a toilet break every keystroke. 更新3回到稳定的1.3.1,因为canaray频道版本暂停每次击键的马桶休息。 While native debug build at least launches with my "fixed" phone, no breakpoints are hit even in hellojni, or a slightly extended version to give more opportunity. 虽然本机调试版本至少使用我的“固定”手机启动,但即使在hellojni中也没有出现断点,或者稍微扩展版本以提供更多机会。 I'm used to logcat now but have had better degbugging experiences with Android, NDK and Eclipse. 我现在习惯logcat,但是在Android,NDK和Eclipse方面有更好的去尾经验。 A more elaborate solution is also easier to break, but at least it works . 一个更精细的解决方案也更容易打破,但至少它是有效的

Yes , Android Studio does support C++ debugging at least with Android studio 1.5.1 and android-ndk-r10e. 是的 ,Android Studio确实支持至少使用Android studio 1.5.1和android-ndk-r10e进行C ++调试。

In the old days, you will have to write make files Android.mk and Application.mk in order to build the C++ code in Android Studio. 在过去,您必须编写make文件Android.mkApplication.mk才能在Android Studio中构建C ++代码。 You don't need them any more. 你不再需要它们了。 As long as you put them under the jni folder, gradle will be able to pick it up and compile it. 只要你把它们放在jni文件夹下,gradle就可以把它拿起并编译它。 You should see something like the following: 您应该看到如下内容:

在此输入图像描述

And also allow you to set break point and debug it. 并且还允许您设置断点并进行调试。

在此输入图像描述

You should follow this step-by-step tutorial and learn more about it. 您应该按照此分步教程进行操作,并了解有关它的更多信息。 And read more about the documentation here . 并在此处阅读有关文档的更多信息。 I've uploaded a complete working example so that you can download and try it out on GitHub . 我上传了一个完整的工作示例,以便您可以在GitHub上下载并试用它。


With Android Studio 1.4 from tools.android.com, debugging is possible. 使用tools.android.com中的Android Studio 1.4,可以进行调试。 When you open/create a project with C++[jni] code in it, Android studio would create a new configuration, something like $(module_name)-native 当你打开/创建一个包含C ++ [jni]代码的项目时,Android studio会创建一个新配置,比如$(module_name)-native

I would select this configuration, sync gradle, build, set breakpoint etc and run it with Android Studio. 我会选择此配置,同步gradle,构建,设置断点等,并使用Android Studio运行它。 After a long wait, my debugger is connected, app is broken in[I think you could also set breakpoint at this time]; 经过漫长的等待,我的调试器已连接,应用程序已中断[我认为您此时也可以设置断点]; this break is not at my set break points, I let it go in android studio and my breakpoint is hit. 这个突破不在我的设定断点,我让它进入android工作室,我的断点被击中。 Hope this helps 希望这可以帮助
Some samples are supposed to work with android debugger 一些示例应该与android调试器一起使用

yes! 是! it is possible to debug native code in android studio. 可以在android studio中调试本机代码。 As from the updates Android Studio 2.2+ support in built tools for debugging native code. 从更新Android Studio 2.2+支持的内置工具中调试本机代码。

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

相关问题 如何在Android Studio中调试c ++代码? - How to debug c++ code in Android Studio? Android:在 android 工作室中进行 NDK-Build C/C++ 调试 - Android: NDK-Build C/C++ debug in android studio 是否可以在Android Studio中编写普通的Java / C ++程序 - Is it possible to write plain java/c++ programs in android studio 无法正常运作-使用平板电脑HUAWEI MediaPad M5的Android Studio上的调试模式本机代码(c / c ++) - Not working - Debug Mode native code (c/c++) on Android studio with tablet HUAWEI MediaPad M5 从C ++调用Android调试方法 - Calling Android debug method from C++ 是否可以使用Android Studio调试本机C代码(ndk构建项目)? - Is it possible to debug native C code (an ndk-build project) with Android Studio? 如何在Android中调试Visual Studio 2017生成的C ++代码.SO文件和其他第三方库? - How to debug visual studio 2017 generated C++ code .SO file and others third party libraries in android? 具有Android Studio版本2.2的C / C ++ - C/C++ with Android Studio version 2.2 更新至3.1.2后无法在android studio中调试C ++代码 - Can't debug c++ code in android studio after updating to 3.1.2 Android Studio NDK“hello from C++”项目无法调试(无法设置断点)? - Android Studio NDK "hello from C++" project can't debug (can't set breakpoint)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM