2

Trying ndk-build C code debug in Android studio(v4.1.2).Below is build.gradle setup.

  1. jni , C/C++ Source files Dirs. Actual C files are not under jni folder but outside it but referred in android.mk file.

    sourceSets.main.jniLibs.srcDirs = ['D:/ccodefolder/jni/']

2)Android.mk Builds a shared library from C , c++ code and also links inbuilt shared and static libraries.

 externalNativeBuild {
           ndkBuild {
               path file('D:/ccodefolder/jni/Android.mk')
           }
       }
       ndkVersion '21.1.6352462'
debug {
        debuggable true
        jniDebuggable true
        minifyEnabled false
        shrinkResources false
       //ndk.debugSymbolLevel = 'FULL'
    }

4)Project structure is as in the image.

enter image description here

Able to run the project and shared library get generated along with other prebuilt .SOs and apk works, also CPP folder is created and able to see C code files of my project.

DEBUGGING ISSUE:

LLDB server gets started and Debugger attached to process, But debug any C file is failing with below error.

Breakpoint will not currently be hit. No executable code is associated with this line

enter image description here

Thanks

NitZRobotKoder
  • 1,046
  • 8
  • 44
  • 74
  • Can you show us what code and at which line are you putting the breakpoint? – Iscle Feb 05 '21 at 19:16
  • There is genuine code which gets executed. Added a sample. – NitZRobotKoder Feb 05 '21 at 19:30
  • 1
    That's a weird issue indeed. Only thing I can think of is that the compiler might be optimizing the code and getting rid of that line. That, or debug symbols are not present so the debugger can't keep track of the line that it's currently executing. – Iscle Feb 05 '21 at 19:35
  • thanks for the suggestion.i checked many files none are debugable.does the placing for C files outside the jni folder matters? trying to update the gradle and generate full debug symbols. – NitZRobotKoder Feb 05 '21 at 19:48

3 Answers3

5

There are a couple things you can try, I've had success with each solution.

From the NDK Bug Tracker:

Select Run -> Edit Configurations

On the left side, select Android App -> app

On the right side, select the Debugger tab

Under the Symbol Directories sub-tab, press the + button

Browse to the following path:

libModule/build/intermediates/cmake/debug/obj In the Build Variants window, select the "debug" configuration for the app

The other solution I've found that sometimes works is to only add breakpoints inside C++ code ONLY. The Android Studio debugger does not handle the transition from Java/Kotlin to C++ well. I have found that by removing all Java breakpoints, restarting Android Studio, and launching debug mode again, the C++ breakpoints will now be hit (though any Java breakpoints now added will be missed).

Software2
  • 2,358
  • 1
  • 18
  • 28
  • Thanks, this solution also did it for me. I am debugging a Kotlin app that links in a module written in Kotlin and C++. – Alessandro Mulloni Jun 13 '22 at 13:34
  • Makes no difference to me? But it might be due to long files, and my question is is it possible to set AS debugger to be able to handle large files, I got a good computer? – Jan Bergström Jan 25 '23 at 04:13
1
  1. Have you used Proguard before? Check these SO answers, specially the one regarding adding the lines to proguard-rules.pro

  2. Also check if you have debug type set to Java Only. For this go to the top bar, in the 'app' selector go to Edit Configurations. Select 'app' then go to Debugger.

  3. It can also be because of Instant Run. To disable Instant run go to File > Settings > Build, execution, deployment > Instant run > Uncheck Enable Instant Run

  4. Another issue might be Android Studio is in offline mode. To disable Offline mode: File > Settings (Ctrl-Alt-S) > Build, Execution, Deployment > Gradle Under Global gradle settings: Offline work checkbox

  5. Last but not least. Have you tried Invalidate Caches/Restart? Go to File -> Invalidate Caches/Restart. This will clean the cache that might be messing up the debugger.

therock24
  • 89
  • 3
1

Other answers are useful.

But the real problem for me was in my android.mk file having LOCAL_LDFLAGS --strip-all which didnt generate symbols for debugging. Once it was removed debugging is working.

NDK team comments: https://issuetracker.google.com/issues/179634983

For the .so file(s) built by ndk-build:
Android Studio can automatically identify the location of the debugging symbol files.
Breakpoints should work without any additional effort.
For the .so file(s) that are included as prebuilt libraries using jniLibs.srcDirs:
Android Studio does not know where to find the debugging symbol files.
Breakpoints set on C++ files that were compiled into these .so libraries will show up as Breakpoint will not be hit. No executable code is associated with this line warnings, and won't hit.
You need to tell Android Studio where to find the symbol files for these .so files.
You can achieve this by adding the directory of your symbol files to Edit Configurations... | Debugger | Symbol Directories.
NitZRobotKoder
  • 1,046
  • 8
  • 44
  • 74