Is it possible to debug C/C++ in Android Studio? - android

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.

[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.

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.

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.

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:
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.

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

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.

Related

Building Symbols for NDK project takes too long in Android Studio

I am working on an Android Project which makes use of the NDK and binds the rather large Boost C++ library. Upon every startup of Android Studio, the IDE takes a rather long time of about 1 hour (more or less, on an i7 quad-core machine) during the Building Symbols stage, during which it is effectively impossible to use the IDE. I guess the bottleneck is directly related to the huge number of symbols included in Boost.
Is there a known remedy to this problem? I have not seen many complaints about this problem, but this forum post seems to ask for help for the same issue: https://forum.xda-developers.com/tools/android-studio/android-studio-2-2-add-cpp-files-using-t3499634
I am facing the exact same issue - after upgrading my Android Studio installation to 2.3.3 yesterday, opening my project now faces me with at least 30 mins of "building symbols" at which point the IDE is effectively useless.
I can build the project via gradle commands in terminal, so I have no idea why this step is mandatory in AS.
I read that this may have something to do with using the NDK build (Android.mk?) vs. CMake (CMakeLists.txt), but I have not yet been able to convert over my .mk file to test this out. This is a legacy project, and the NDK portion is still somewhat of a mystery to me :(

Google launcher building

I tried to build this project using JetBrains IntelliJ IDEA but it was unsuccessful (there were a lot of errors). The project has an Android.mk file, but I didn't see the C++ code.... How can I build this project? I've tried to use another IDE, then I tried building from the console but it was not useful. Perhaps, does Launcher3 need NDK to build it?
You'll need to isolate the launcher's code and then identify all of it's dependencies. If the code has any C++ code then it definitely requires the NDK and currently IntelliJ/Android Studio does not have any stable support for NDK based app development (though it is a work in progress) else if it is pure Java (most probably) then you'll have to manually add the classes and build the app from Android Studio! Either way you will have to dig deeper into the source and know for yourself. Unfortunately there seems to be no easier option, for now. I wouldn't mind being proved wrong for the matter.
EDIT :
I glanced through the code and I noticed there are no C++ source files or library (*.so) files. This isn't a gradle project and hence cannot be built as one. You'll need to setup a gradle based Android project and then begin adding the sources (src, tests etc.) and their dependencies (res etc.). You can start of here : Getting Started with Android development if you're new, else you can move to this : Gradle and Android

What do I need to do to compile Android NDK (C++) aps in Visual Studio 2010?

I've read in different places that this is possible. I do not need to debug, just compile. Any walkthrus out there? Thx.
Found links:
Android NDK with Visual Studio - "we've got it partially working, we use Visual Studio to build, using the proper android headers and whatnot, then we call the NDK build scripts. we're currently working on automating the second half as a post-build step"
http://groups.google.com/group/android-ndk/browse_thread/thread/9f3a55366ba08f2a/cb539c80e5729032 - "You have to dig out proper parameters for all the tools, but I assure you
that this all works for me on command-line (well actually in various bat
files called from MS Visual Studio)."
Building and debugging Android apps within Visual Studio is OK. No need to mess with Eclipse, or post build steps.
I've got it running myself, here is a screenshot:
http://www.gavpugh.com/2011/02/04/vs-android-developing-for-android-in-visual-studio/
In case you get "Unable to locate tools.jar. Expected to find it in C:\Program Files (x86)\Java\jre6\lib\tools.jar" you can add an environment variable JAVA_HOME that points to your Java JDK path, for example c:\sdks\glassfish3\jdk (restart MSVC afterwards)
If you like you can also debugg Android apps within MSVC using this:
http://ian-ni-lewis.blogspot.com/2011/01/its-like-coming-home-again.html
Download WinGDC for Android from http://www.wingdb.com/wgMobileEdition.htm
Microsoft (obviously) doesn't seem interested in supporting this:
http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/6641879e-6f47-403a-9ed5-28bb6e040563
Why not stick with Eclipse since that IDE will integrate with the compiler/debugger for the android target (including debugging the native library, I think).

Android NDK with Visual Studio

I'd like to build some native libraries for android using the NDK (native development kit), but i'd like to use Visual Studio 2010 to do it. I've googled quite a bit but haven't found any information on it. Does anyone have any experience with this and know the steps necessary to make this happen? I have CYGWin installed, made sure i get Make (per the NDK instructions), but i'm not really sure of the next steps in terms of setting up the project, compiler in visual studio, etc.
If anyone knows of any write-ups, tutorials, or links to sample projects, that would be awesome, as there isn't much on google yet.
thanks!
Here's another solution, which integrates the NDK fully within Visual Studio. No makefiles. It behaves like a proper Win32 project:
Here's an excellent blog post about how you can configure your environment to debug android NDK code using Visual Studio.
Visual Studio is officially not supported.
Some problems (but not limited to):
MSVS cannot create the proper ARM binaries
Android makefiles (.mk) are not supported by MSVS
There are however, third party solutions:
vs-android
VisualGDB
You might want to check out DS-5 as well, though it's not Visual Studio.
The answer depends on what kind of integration you require.
To just build the native Android code from Visual Studio you can create a new Makefile project, and make it run ndk-build.cmd when you press "build". If you would like to get the error messages mapped as well, you will need to parse the output of ndk-build.cmd and convert it to a format that Visual Studio can udnerstand.
If you want to debug your native Android code from Visual Studio, you will need a third party tool that will control ndk-gdb on behalf of Visual Studio and provide workarounds for several bugs (e.g. rebind breakpoints when libraries are loaded).
You can try our VisualGDB for Android tool that does exactly that - creates projects that wrap ndk-build and controls NDK debugger on behalf of Visual Studio. If you need more information, there is a step-by-step tutorial available.
I have not found a direct clean solution, here is my workaround.
I develop my native code on VS as a static library, and use some test project to try it as a console aplication.
When it is ok, from cygwin I use a little bash script that copies all needed files to the jni folder and launch the standard android ndk make command. (also copy some file to assets folder when needed), producing the executable in the right folder.
To use the pthreads I have linked my projects to pthreads-win32.
The only files I do not compile in VS are the jni code.
I hope this can help you.
There's also a couple of other third party alternatives for developing ANdroid apps with VS in addition to those above. There is the open source dot42 located at http://www.dot42.com and Remobjects, http://www.remobjects.com , which allows you to program in c#, Pascal, and Swift. I have played with the trial of Remobjects and am now starting to play around with dot42.
My use of the remobjects trial allowed me to recreate in c# the first five chapter projects in the textbook of the Android course I took. I haven't had the same success with dot42 so far but I need to find some time to really give it a chance.

Installing & using the Android NDK in Eclipse

I've been running the Android SDK for a while now in Eclipse (MAC OSX). I've downloaded the NDK and installed the C/C++ tools in Eclipse, but could anyone guide me on using the NDK? For example, do I just create an Android project like normal and build it with the NDK instead?
Really could do with a decent tutorial if anyone know of any.
EDIT: OK so I have the NDK installed now (I think) but does anyone have any idea how to use it? I got as far as this (taken from here):
Run Terminal
cd ~/android-ndk-1.5_r1
make APP=hello-jni
In order to run the hello-jni sample application, but I get an error in terminal saying:
Android NDK: APP variable defined to
unknown applications: hellojni
Android NDK: You might want to use
one of the following:
build/core/main.mk:81: *** Android
NDK: Aborting . Stop.
Any ideas why?
As simply as I can describe it, building an Android app from within Eclipse that uses the NDK requires two steps.
First, inside your terminal you need to run the NDK build script on your project. cd into the root of your project directory and then execute the ndk-build script within that directory.
For example:
cd ~/workspace/hello-jni
./~/android-ndk-1.5_r1/ndk-build
After doing this, you should see some output that results in the creation of a *.SO file within the obj directory within your project directory.
Once you have the *.SO file, the final step to building an application with the Android NDK through Eclipse is to build it with Eclipse like you would any other application and then deploy it for testing.
If you make any changes to the C/C++ code you'll need to repeat step one and regenerate your *.SO file before building and deploying your application from within Eclipse again.
I would like to note that by using the Android NDK your android apps are still based upon Java. They're just communicating with code written in C/C++ by way of the Java Native Interface.
Finally, I am not aware of any Eclipse plugins that will aid with NDK development. Everything I know about the NDK I have learned the official Android NDK documentation. Please feel free to comment and let me know if there anything I can clear up in my response.
Native development and debugging support came into Eclipse environment as of ADT version 20. http://tools.android.com/recent/usingthendkplugin
Set path to NDK from Eclipse Preferences -> Android -> NDK
Right-click on your project and choose Android Tools -> Add Native Support
developer.android.com states you also need Cygwin.
http://developer.android.com/tools/sdk/ndk/index.html#Contents
Required development tools
For all development platforms, GNU Make 3.81 or later is required. Earlier versions of GNU Make might work but have not been tested.
A recent version of awk (either GNU Awk or Nawk) is also required.
For Windows, Cygwin 1.7 or higher is required. The NDK will not work with Cygwin 1.5 installations.
The docs directory in the NDK has some pretty good information on how to use the NDK itself. Read the overview, Application.mk, and Android.mk HTML docs. You'll want to google for the Sun JNI PDF, download it, and learn what JNI is all about before you go any further. This is because simply compiling a bunch of C/C++ code into libraries with the NDK is only part of the process. You have to write native Java code that calls your C/C++, and you have to create wrapper functions in C/C++ that adhere to JNI conventions that the native Java code can invoke. JNI has been around a long time, it's not Android specific by any means. So, you can, to learn about it, go quite far following tutorials geared towards JNI, using command line tools like javah and javac, and then return to integrating with the NDK after you know the basics. (For an example of what these C shims look like, take a look at the hello-jni sample in the NDK; the C source file there shows you typically what the shims look like. Using javah to generate these shims is the way to go, you create Java classes that have native methods, process them with javah, and it generates the C headers for you, then you code up C functions that adhere to the generated function prototypes).
Note: while the NDK docs would have you manually building from command line and then going into Eclipse to build your app (a laborious sequence of steps, to be sure, especially if you are changing the C/C++ code), it turns out you can integrate easily with Eclipse so that the NDK is run each time you build from Eclipse. To see how, read here.
This is for benefit of others who want to create the project from scratch from within eclipse: I followed steps mentioned here in this blog here and it works fine: http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/
To attempt to answer the question directly - you need to run ndk-build in the folder with the native code within your project folder. This creates the .so files found in the file explorer/resources tree under jni in Eclipse. These functions, if the syntax in the code is correct, can now be called from your java code.
I found many sources of help when install and getting to grips with Android Developer Tools and the NDK. I wrote a blog post to share my experiences and hopefully give back to the community that helped me get there which may help understand my answer: http://workingmatt.blogspot.co.uk/2013/03/set-up-android-sdk-and-ndk.html

Categories

Resources