How to add external libraries (a .so file) to godot 4.2 .net project

Godot Version

4.2

Question

Let’s I have an android library such as mylib.so. How can I include it into the exported apk/aab?

1 Like

I’m not sure if it’s possible to use Android specific libraries, but your best bet is either to write a wrapper in the .NET version of Godot with C#, or take a look at this docs page:

Basically I am using sqlite-net (a c# wrapper) for sqlite. In Unity I just need to add the libsqlite3.so and this is correctly placed into the .aab file under …/lib.

Obviously the libsqlite3.so is necessary for the sqlite-net to work (as from Android N).

How can I add the libsqlite3.so file to the godot project in order to have it bundled correctly into the apk/aab?

If you want to use a C# wrapper, you almost definitely need to use the .NET version of Godot as far as I know.

My specific issue is how to add the libsqlite3.so library in the project and have it exported in the apk…

My question remains…

Since in the Godot version you did not specify if you’re using the .NET version or not, I assume you didn’t, I apologize, hence why I suggested using the .NET version, where, if you use a wrapper like that, you can simply include it in your project as a NuGet package, and it’ll be included with your executable on all platforms, including Android. I do not know of any other way to include a library, unless you are willing to compile from source, and use C++ to include it somehow, but I have very limited knowledge on how to do that.

I maybe a bit late but after hours of trial and error. I found a way to do this using custom builds for Android. After following the guide.

You should have the gradle Android project and should export the project to android make sure its set up correctly if not go here.

Now in the “android/build/” folder on root of your project there will be a “libs/” folder inside is where native library will be loaded. Inside of it we have “debug/” and “release/” inside of each of these will be “arm64-v8a/” and “armeabi-v7a/”(now available since 4.4) or what ever cpu architecture you supporting. Simply place your native library in the correct cpu folder the and dllimport artribute should be able to find it and load it.

Also make sure you do this for debug and release so it works for both configurations.
You may want this automated using a python or any other scripting language.

Hopes this helps anyone in the future.

1 Like

Big thanks to @lostbblizzard, works like a charm!

To conveniently store your .so libs in git for any configuration, you can do the following:

  1. Make sure godot completes first gradle build successfully (You do not need to add .so libs yet). This will give you a complete gradle project structure.
  2. Create a new libs folder in the newly generated android folder, and place your .so libs under the correct architecture. (See the structure below).
  3. In the build.gradle include your new folder as a source for libs (Starts on line 231 for Godot 4.4)
debug.jniLibs.srcDirs = ['libs/debug', 'libs/debug/vulkan_validation_layers', '../libs']
dev.jniLibs.srcDirs = ['libs/dev', '../libs']
release.jniLibs.srcDirs = ['libs/release', '../libs']
  1. In git you can ignore the following folders:
GodotGame/android/build/assetPacks
GodotGame/android/build/assets
GodotGame/android/build/build
GodotGame/android/build/libs
GodotGame/android/build/res
GodotGame/android/build/src

These steps effectively leave your Git repository with sensitive files only, and Gradle will use the libs folder as a source.
Final structure:

GodotGame/
├── android/
│   ├── build/
│   │   └── build.gradle
│   ├── libs/
│   │   ├── arm64-v8a/
│   │   │   └── myLib.so
│   │   └── armeabi-v7a/
│   │       └── myLib.so