2

I was using Visual Studio and it's Debug mechanism is really useful to me. With many header and C files in 1 project, just a simple F10 it could take me a tour around from the beginning of the main() function so that I could see the sequence of the code being executed.

Now I'm jumping into Android Studio and start with a project using NDK, JNI (this project for example: https://github.com/googlesamples/android-ndk/tree/master/gles3jni), it makes me confused because there're many .java files and others C++ (native code) files and I don't know which code from which file is executed first and how it goes on.

My Question is: I'm looking for a way to debug in Android Studio line by line from the beginning to see its workflow likes how Visual Studio did but all I got from searching is how to start from a breakpoint when debugging.

I have tried putting the break point in the onCreate() method of the launcher activity and use F8 to Step Over and F7 to Step Into but it doesn't work as i expect. It keeps taking me to the super class Activity.java and GLSurafaceView.java instead of taking me to the C++ code. Is there any way to do so in Android Studio and how to do it?

I have tried with others project but the problem still remain the same. Hope someone could help.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Toan Tran
  • 476
  • 6
  • 28
  • How did you build your native library? A small snippet will be helpful. – Moved Feb 22 '17 at 00:33
  • @Moved, Everything is set up in the project above already. All I have to do is installing CMake, NDK and LLBD using the SDK Manager. Once all of them is installed, the project is ready to run. More information could be found here: https://developer.android.com/ndk/guides/index.html – Toan Tran Feb 22 '17 at 01:53

2 Answers2

1

This won't happen. Your app (the Java boilerplate code) is set to respond to many system events that happen when the user works with the app and the device on which the app is installed. If your c++ code is a piece of monolythic straightforward algorithm, you can set s breakpoint in the beginning and really follow step by step. But with Android Studio, even this process is not as easy as with Vusial Studio. To begin with, it's always remote debugging, dependent on delicate communication protocols.

This said, you can try to set up Microsoft Visual Studio as your IDE for debugging native Android code. It is surprisingly robust, and also may be more familiar to you.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • thanks a lot for your reply. However, my project that I'm working with is kind of complicated. It contains an average amount of java code and a bunch of C++ code (likes 40 .cpp and .h files) called through the JNI, is it possible to debug such project with Visual Studio? By the way, is there any other way to see the workflow using Android Studio? Or the only way is to read the code line by line from the begining? – Toan Tran Feb 23 '17 at 05:35
  • I don't think 40 files will put a significant pressure on Visual Studio. Calls through JNI pose a more serious challenge: they essentially disrupt the flow of C++ execution,and it's not very easy to switch debugging context between Java and C++. I would recommend to rely more on standalone tests that can be followed in 'conventional' debugger, and less on debugging the mess a.k.a. Android APK. – Alex Cohn Feb 23 '17 at 11:40
  • As it is often the case with cross-compilers and remote debugging, you can make your life easier if you can create significant tests that you can debug in VS on Windows, and only when your core is fully reliable, move to Android and only have to worry about the glue code and system dependent workflow. – Alex Cohn Feb 23 '17 at 11:46
1

Since you've asked about Android Studio, I will answer about that - I am not familiar with Visual Studio for Android and perhaps it is better for what you are asking.

In Android studio such debugging is very hard - although some people said they managed to get it to work, I wasn't able to. Especially in a complex environment. So, what I'm doing is extensive logging, with as many details as possible, and visual debugging - put some text view on the screen and update it with the info you need. This could be quite powerful in some cases. Or, alternatively, make some changes in your ui or your scene as a result of certain conditions you define - so you could see the visual effect. Again, much less useful than debugging, but can still be quite powerful.

yakobom
  • 2,681
  • 1
  • 25
  • 33
  • thanks for your reply, very helpful tips. By the way, may I ask where the compiler start from once the app run? It's would be easier for me to follow if I know where does it start at the begining. – Toan Tran Feb 23 '17 at 07:41
  • It's not very clear what it is you are asking - do you want to know the first point where you can put your log? Java? C++? – yakobom Feb 23 '17 at 08:55
  • I want to read the code from the beginning to see how to goes. Therefore, I mean where does the compiler start at once the run it. For example, in C++, it always start at the ``main()`` function. – Toan Tran Feb 23 '17 at 09:04
  • It is still not clear what you are asking about - Java? in c++ standalone program, it is 'main', but on JNI it is JNI_OnLoad, if you implement it. On Android's Java you are using SDK classes and infrastructure, so the start is no visible to you. Basically, onCreate of the Application class is the start point for you. Also have a look here: http://stackoverflow.com/questions/10057448/entrypoint-of-android-application – yakobom Feb 23 '17 at 09:42
  • thanks, I got it now. I'll try with the ``onCreate`` function then. By the way, are you using facebook, LINE or something that I could contact? You were very kind to me and I just want to make friend :) – Toan Tran Feb 23 '17 at 09:58