23

I am developing android app using NDK. I have two projects. One is for my native library which uses NDK and generates .so file.

I am using Android Studio but disabling auto build and enabled build using ndk-build. I am using Windows 7.

Now after generating .so file I copy those in my main application project which also uses ndk-build to compile JNI functions in which I am calling functions of my library. I hope I am clean till this point. If not then I will give more detail on request.

Now I am running my application in device using Android Studio and I can put break point in java code and debug that code but I am not able to debug JNI call and also native code which I have in separate project. I need to debug inside my library code. So is there any way to achieve this?

I have seen VisualGDB but it is paid. So let me know if there is any alternative to full fill my debugging requirements.I have searched lot but did not get any concrete solution.

I can see option in Android Studio for attaching to android process where I can see my running device but I am not sure how to use it so I can debug by native library code (which is in separate project without any activity).

Let me know if more detail is required

Milad Nouri
  • 1,587
  • 1
  • 21
  • 32
Premal
  • 551
  • 1
  • 6
  • 25
  • AFAIK, native debugging is not possible in android studio. BTW, if you add some formatting to your question and make it less 'dense' , more people will read it . – Jonas Czech Feb 19 '15 at 09:59
  • @JonasCz Actually, it is now, see Sistr's answer below – Shlublu Feb 24 '16 at 12:47
  • @Premal, I see there's no accepted answer, did any of the proposed solutions work when debugging a .so from one project in the 2nd project? – Deemoe Mar 21 '16 at 18:50

5 Answers5

14

In Android Studio 2.0 preview the process is a bit different (ans easier I think):

  1. Install "LLDB": Go in Tools->Android->SDK Manager, then go in the "SDK Tools" tab and make sure to check "LLDB" (Near the end of the list usually)
  2. Select a/the "Debug" variant: In the lower left corner of AS, click on "Build Variants", the default debug variant is called "Debug"
  3. Select a native configuration: left side of the run button (The green triangle),The default native configuration is called "app-native"
  4. Set a breakpoint
  5. Launch your app in debug by clicking on the little bug on the right side of the run button.

If it doesn't work, check that your native configuration has debug type "Hybrid" selected: On the right of the play button, click the little triangle, select "Edit Configurations", select your "app-native" configuration, go in the "Debugger" tab and select "Debug type: Hybrid".

Sistr
  • 1,175
  • 10
  • 26
  • 1
    BTW if you try using graddle version 2.9 and above, you might have an **null pointer exception** at compile time. If it's the case just go into [preferences] -> [Build, Execution, Deployment] -> [Instant Run] and check that instant run is desactivated. Graddle 2.9 has an issue with instant run. – Sistr Dec 20 '15 at 08:55
  • HAAAAAAAAhahaha !!! Sorry. Believe it or not, THE STEP 1 was what I missed! Now it works (Studio 2.0 Beta 4, Gradle 2.10, not the experimental one). Thanks ! So much! This should be the accepted answer ! – Shlublu Feb 24 '16 at 12:03
  • Glad to see it helped :) – Sistr Feb 25 '16 at 13:06
  • 1
    My debugging stopped working randomly one day. The solution was to set the debug type to "Dual" (Android Studio 3.0.2) from "Auto". – Chris Watts Dec 18 '17 at 20:49
  • @CJxD You are a life saver. Thank you! – Richard Lalancette May 06 '19 at 19:28
4

Android Studio 1.3+ supports native debugging.

To set it up, follow these steps:

  1. Modify your gradle-wrapper.properties, local.properties, and both build.gradle files as shown in this guide
  2. Sync gradle
  3. Create and select new build configuration:

    Click on drop down next to run button) -> Edit configurations, click plus sign, choose Android Native, fill in options on the right (I used LLDB in native debugger tab), and you should be set.

  4. Set breakpoints in C++

  5. Hit debug button and be patient (sometimes the debugger takes a while)

I have been able to debug native code under Lubuntu 14.04 with Android Studio 1.3 (stable channel). Although others have supposedly been successful under Windows, I haven't been able to debug natively in Windows 8.1 (I have tried with Android Studio 1.3, 1.3.2, and 1.4 preview 3).

Update Android Studio 1.4 Beta just came out. I tested it and was able to debug natively on Windows 8.1.

Community
  • 1
  • 1
Nate
  • 2,449
  • 3
  • 21
  • 29
  • For me everything seemed fine, no errors, debugger was attaching in the console. But my breakpoint simply wasn't getting hit. My breakpoint was in a piece of code being called in my Activity onCreate. Turns out there is a bug in Android Studio 1.3 where breakpoints would not get hit in the app launch. Upgraded my IDE to 1.4 beta in the Canary channel and it works fine. – Dean Wild Sep 15 '15 at 08:34
2

I was able to set breakpoints and step into native code but only when all of the following were true:

  1. The generated native library .so files retained their debug symbols
  2. Sources were supplied (see "Library Properties" menu)
  3. I set the run/build configuration to use a native debugger (as described here)

It is working for me right now in Android Studio 1.5, using the Gradle Experimental Plugin

Jeff Trull
  • 1,236
  • 11
  • 16
2

Another check point if breakpoints in native codes don't work:

  • remove android:extractNativeLibs="false" line or set it true in AndroidManifest.xml

extractNativeLibs="false" causes 1 or 2, occasionally.

  1. Install will fail with message "INSTALL_FAILED_INVALID_APK"
  2. Breakpoints in native codes don't work
Toris
  • 2,348
  • 13
  • 16
0

If your native breakpoint still can't get hit after you tried everything AND by any chance your native library had been referenced across multiple AS projects, then there is a simple solution.

Just rename your native library in setting.gradle and your build.gradle

Befor:

//in setting.gradle
include ":myNativeLib"
project(":myNativeLib").projectDir = new File("...")
//in app's build.gradle
api project(':myNativeLib')

After:

//in setting.gradle
include ":myNativeLib2"
project(":myNativeLib2").projectDir = new File("...")
//in app's build.gradle
api project(':myNativeLib2')
wdanxna
  • 10,699
  • 2
  • 23
  • 24