How can I debug my Android native C++ library for Unity3D?

I have created a JUCE based audio library in C++ (originally as a windows DLL)

I have successfully built it into an AAR library with Android Studio

I can successfully run it in a Unity3D build for Android.

I can get the debugger messages back to LogCat

However, I really need to step through some part of the native C++ code with a managed debugger. ( I know how use a debugger for the c# for unity on android)

I’ve searched for days but cannot find an answer.

Unity3D 2018.3.11f1

Android Studio 3.4.1 Build #AI-183.6156.11.34.5522156, built on May 1, 2019 JRE: 1.8.0_152-release-1343-b01 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0

2 Likes

You use Android Studio’s Native Debugger for such cases, from Unity choose Export Project in BuildPlayerWindow, and import that project in Android Studio.

Set debugger type to Native in Android Studio, in project settings set the symbol path, if those were separated from library files. And you’re good to go.

3 Likes

Thanks.

I know how to set the symbols but can you elaborate on “if those were separated from library files” I’m not sure what you’re suggesting

I am talking about stripping symbols from your native library, and were they stripped to separate file, sorry for not being clear

OK, thanks again. I’m with you now. They’re not stripped.

However, I do have another question if you have the patience for it.

When my exported app comes into Android Studio, the native code is the .aar so I’m a little confused how I should set breakpoints?

Should I bring my library into the exported app? or the exported app into the library? etc.

Assuming the library was built on same machine, you drag & drop C/C++ files in your Android Studio project and place breakpoints there.

P.S AAR is simply a package (a zip file), the native library sits in aar itself, which has .so extension. You can actually unzip AAR using 7zip or othere compression program.

OK, I built the AAR. (I know it a zip too - but a good tip!)

So I just need to copy the source files into the exported project into the /libs folder? sorry if this seems obvious, just a complete android nube except for vanilla builds.

You don’t need to copy them, you simply need to open them in Android Studio, either drag & drop, or File->Open, after that you can start placing breakpoints

I really appreciate your replies and I’m so sorry for seeming dumber…

so I have two projects open - my Unity-export app & my c++native library

what am dragging and where am I dropping it?

e.g drag my source folder from the library to the libs folder of the export app, etc

No worries.

You drag & drop C++ files to Android Studio session which currently debugs your application, you don’t need to add those files to project.

4747478--450596--AS.jpg

1 Like

OK, I get you now!

I’ve done that, but it doesn’t hit the breakpoint I set - although I know from the logcat that it must have past that point (a Debug.Log() command from unity to the native code prints successfully)

EDIT: my error, I was setting the wrong path for the symbols.

It works now.

Thanks so much for your help @Tomas1856

1 Like

@garrilla amazing you got this working! Could you share what the path to your symbols was? I seem to have the same setup as you describe. I want to attach the native debugger to my native DLL in my Unity project running on Android. I’m unsure where to point it to for symbols.

  1. go into ‘edit configurations’ of your lib (this is is the drop down menu next to the build-hammer)

  2. under ‘debugger’ add a Symbol Directory - then select ‘armeabi-v7a’ by navigating to

[my root path to my lib]\Builds\Android\lib\build\intermediates\cmake\debug_\obj\armeabi-v7a

hope that helps

1 Like

your post give me courage :slight_smile:

i’m almost give up on similar issue

that i want to break il2cpp files on android studio

i read this post Debugging Android IL2CPP builds

but its not work for me

i try breakpoint but lldb didn’t match the source code

and try to command image list

and there no il2cpp.so

my extra setting is below

rename libil2cpp.so.debug to libil2cpp.so and replace to

\AndroidStudio\appname\src\main\jniLibs\armeabi-v7a\libil2cpp.so

so symbolic file be involved

and specify symbol Directories in Edit Configuration to above directory

how do i debug il2cpp file on lldb of android studio?

@JoshPeterson @Tomas1856

why did you do this? src\main\jniLibs\armeabi-v7a\ should contain executable code only, don’t copy symbol file on top of it. You need to rename ibil2cpp.so.debug to ibil2cpp.so but keep where it is, and tell Android Studio where to look for it.

P.S This might helpful, but in 2020.1 Export Project allows you to easier to debug il2cpp, https://unity3d.com/unity/whats-new/2020.1.0

Android: When exporting a gradle proejct, il2cpp generated native files will be also copied to gradle project, the il2cpp library will be compiled/linked via gradle. Previously il2cpp library was being precompiled/prelinked by Unity when exporting project. This improvement gives you an ability to both debug and profile il2cpp files when needed, it also increases your control over the build pipeline. Note: If you don't export a project, il2cpp library will be linked by Unity as it was before.

You’ll still need to set symbol path after project export.

1 Like

it’s great news

but we are in live service using 2018.4.10f version for 1 year

so i’m afraid to update to 2010 version

at this time. i didn’t touch the directories. and specify symbol directories outside android studio where ilil2cpp.so.debug file exist
and change il2cpp.so.debug to il2cpp.so

run this setup has same result

‘image list’ not showing libil2cpp.so and libmain.so, libunity.so

lookup some symbol is nothing to print @Tomas1856

Just in case, did you set Debugger Type to Native, and did you set symbol path under Native Debugger scope?

Also while paused, you can do

target symbols add <path_to_symbol> to force symbol load

yes. the path inside symbol directories is where libil2cpp.so live and libil2cpp.so is copied from libil2cpp.so.debug

**target symbols add <path_to_symbol>** to force symbol load

add symbols forced has error

2 settings not work. thanks

@Tomas1856

i create simple project for 2020.1.0 to test debugging on android with new feature

and it’s seem to have same issue

6739561--776518--upload_2021-1-19_18-39-46.png

find some function name that generated by il2cpp and copy then paste to lldb

i test it on x86 android device (windows environment)

Sorry if I bothered you. But please help me as I have to solve this problem. thanks

@Tomas1856

Can you clarify? Unity doesn’t support x86, it only supports ARMv7 and ARM64. So is this a typo what you wrote? (Edit: sorry, forgot that 2018 still let’s you select x86)

Also just in case, you’re testing on physical device, not an emulator? Because lldb won’t work on emulator with Unity apps.