23

I'm writing an Android app that includes C/C++ code that is compiled alongside the Java code. 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?

I am not necessarily looking for a way to step through the source from within 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. I want to be able to step through the execution line-by-line and inspect variables.

Android Studio 1.0.2
Android NDK r10d

Edit: Stemming from the immediate response of, "Use Eclipse with ADT plugin", let me add more requirements.
This project started as an Eclipse ADT project. However, I have now migrated to Android Studio. My project is now built using Gradle, and my project directory structure reflects this. I am willing to use Eclipse; I am not willing to change my project structure in order to revert to Eclipse from Android Studio. If there is a way to open the Android Studio project in Eclipse and debug it that way, please elaborate on the process.

Sean Beach
  • 2,050
  • 4
  • 26
  • 35

6 Answers6

7

[UPDATE]

As of July 2015, Android Studio DOES support NDK.

You can learn more about it on this link.


[OLD] NDK is not yet supported in Android Studio.

When we have to deal with NDK, the only solution is to use Eclipse.

EDIT

We basically keep Eclipse project with NDK feature (map in our case) and Gradle project with other (non-map) features. So everything that has to be done with NDK, we do in Eclipse and then include changes into Gradle project.

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...). Then in the main class of Gradle project simply added a line

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.

sandalone
  • 41,141
  • 63
  • 222
  • 338
  • Can you elaborate a bit? I'm looking for specific details of the process. – Sean Beach Feb 03 '15 at 20:08
  • There's also [VSDroid](http://www.wingdb.com/wgAndroid.htm) that supports debugging native Android code from Visual Studio. It is available for free, but hasn't been updated for almost a year. It's still in beta and *"provided as-is"*. – IInspectable Feb 03 '15 at 20:41
  • 2
    I'm developing on a Mac. No Visual Studio for me. – Sean Beach Feb 03 '15 at 21:15
  • @sandalone So you are developing a C++ library in Eclipse, then importing it with Gradle? While that is somewhat helpful, it doesn't provide a way to step through the C++ code when debugging the Android app. – Sean Beach Feb 03 '15 at 21:29
  • @DirtyBeach I am not aware of how you can do that with Android Studio. Check up this video https://software.intel.com/en-us/videos/using-the-ndk-with-android-studio and let me know if it works. I would be interested as well. – sandalone Feb 04 '15 at 12:25
  • Might be worth updating/removing this answer since it's no longer true that NDK debugging isn't supported in Android Studio – donturner Nov 23 '15 at 15:16
5

As of version 1.3+ Preview Android Studio supports C++ debugging, quoting Android M Developer Preview & Tools:

Most notable is a much requested feature from our Android NDK & game developers: code editing and debugging for C/C++ code. 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. Java and C/C++ code support is integrated into one development experience free of charge for Android app developers.

vitaut
  • 49,672
  • 25
  • 199
  • 336
  • 5
    I've installed this build and have no idea how to debug native code. I can see cpp files in my project but cannot set any breakpoints or step into jni calls. Did anyone manage to do it? – serine May 29 '15 at 14:35
  • I think they haven't published the Android Studio update that includes C++ support yet, but according to the announcement they plan to do it in 1.3 (which is still in preview and will be updated). – vitaut May 29 '15 at 20:57
  • http://stackoverflow.com/questions/34959013/ndk-debugging-with-gradle-experimental-plugin – Michael Mar 04 '16 at 22:19
3

It isn't wasn't isn't working very well.

hello-jni fails failed with:

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

compileSdkVersion 23
buildToolsVersion "23.0.1"

And the "app-native" run configuration @Gerry mentioned. It's nearly there, but I can find no trace of an lldb directory in my app's private data folder. I do find a 400kb gdbserver in the lib directory linked from there, with my own *.so right besides. Unfortunately the option for debugging with gdb instead of lldb has gone with the upgrade from 1.3 to 1.4. NDK 10e ships a 400kb gdbserver binary in ndk\prebuilt\android-arm\gdbserver which it is (I guess). On September 15th the gdb debug howto https://github.com/mapbox/mapbox-gl-native/wiki/Android-debugging-with-remote-GDB was updated. 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.

I have run hello-jni and reached the same conclusion.

So in answer to your question, it is not yet possible to debug C/C++ in Android Studio. 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. I just set a conditional breakpoint that didn't fire but the logcat shows it should. 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.

UPDATE3 Gone back to stable 1.3.1 as the canaray channel version was pausing for a toilet break every keystroke. 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. I'm used to logcat now but have had better degbugging experiences with Android, NDK and Eclipse. A more elaborate solution is also easier to break, but at least it works.

John
  • 6,433
  • 7
  • 47
  • 82
  • @TinyGiant ok the phrasing is a little off, but if you read the last paragraph it _is_ an answer to the question. – Segfault Sep 25 '15 at 19:10
  • @AlexCohn maybe for you, but nothing like it should, for me. I should freeze this answer until I get chance to try hellojni again. – John Sep 26 '15 at 08:11
  • Looking at your second update, I completely agree with your findings. People with experience of Visual Studio or Java should lower their expectations. Native debugging is not 100% reliable yet; it often takes three attempts to get started, and sometimes AS requires Force Quit in one of the failed attempts (I am on Mac OS, by the way, which may behave very different from WIndows). Regarding the nonsense frame values, this happened for me mostly when the app crashed in Java. I never tried conditional breakpoints. – Alex Cohn Sep 26 '15 at 14:10
2

Yes, Android Studio does support C++ debugging at least with Android studio 1.5.1 and android-ndk-r10e.

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. 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. You should see something like the following:

enter image description here

And also allow you to set break point and debug it.

enter image description here

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.


Yuchen
  • 30,852
  • 26
  • 164
  • 234
0

With Android Studio 1.4 from tools.android.com, debugging is possible. When you open/create a project with C++[jni] code in it, Android studio would create a new configuration, something like $(module_name)-native

I would select this configuration, sync gradle, build, set breakpoint etc and run it with 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. Hope this helps
Some samples are supposed to work with android debugger

Gerry
  • 1,223
  • 9
  • 17
-1

yes! it is possible to debug native code in android studio. As from the updates Android Studio 2.2+ support in built tools for debugging native code.

Sachin Verma
  • 69
  • 1
  • 4