Sqlite and C# on android

Godot Version

4.5.1

Question

So, I have been developing an app with Godot using C# and SQLite. Everything works as intended as a linux desktop application. However, as I tried to test the mobile version on android, I ran into a problem…the app would crash trying to find the SQLite libs. I made sure to package them up with the build and i can see them inside the apk. I can also see the libs that got included automatically from the build process. However, no matter what I try, I get some error about:

 ERROR: Failed to initialize database: System.TypeInitializationException: The type initializer for 'SQLite.SQLiteConnection' threw an exception.
  ERROR:  ---> System.DllNotFoundException: e_sqlcipher
  ERROR:    at SQLitePCL.SQLite3Provider_e_sqlcipher.SQLitePCL.ISQLite3Provider.sqlite3_libversion_number()
  ERROR:    at SQLitePCL.raw.SetProvider(ISQLite3Provider imp)
  ERROR:    at SQLitePCL.Batteries_V2.Init()
  ERROR:    at SQLite.SQLiteConnection..cctor()
  ERROR:    --- End of inner exception stack trace ---

I went on a wild goose chase all weekend because I foolishly followed the advice of LLMs(im still mad at myself for that!) so Im hoping that I can get this working so I can move on with the rest of my life…

Anyway, I tried to add in Android only code at start up to help it find the libs like so:

[DllImport("libsqliteX")]
private static extern int sqlite3_libversion_number();

    
[DllImport("libsqliteX", CallingConvention = CallingConvention.Cdecl, EntryPoint = "sqlite3_open_v2")]
private static extern int sqlite3_open_v2(byte[] filename, out IntPtr db, int flags, IntPtr zvfs);
...
if (OS.GetName() == "Android")
{
    int version = sqlite3_libversion_number();
    GD.Print($"SQLite version loaded: {version}");
}

I can see the version number printed to the console, but other than that I cant get the lib load properly.

I tried many packages in the csproj file. Here is my current iteration:

<Project Sdk="Godot.NET.Sdk/4.5.1">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <EnableDynamicLoading>true</EnableDynamicLoading>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="SixLabors.ImageSharp" Version="3.1.10" />
    <PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
    <PackageReference Include="SQLitePCLRaw.bundle_e_sqlcipher" Version="2.1.10" />
  </ItemGroup>
  <ItemGroup>
    <Compile Remove="dev docs\**\*.cs" />
  </ItemGroup>
</Project>

I saw an old post on reddit of someone using the SQLite GD script extension to bridge to C#, but I would rather do things in pure C# if possible. Any hints to make this work on Mobile with C# only?

okay, i figured out what was going on. It took me a few days, but in a nutshell:

  1. The NuGet SQLite package didn’t have Android-specific binaries…or if they did exist, they didnt get downloaded during the build process. Either way, the linux binaries was being added to the apk and not android ones.

  2. Once I figured that out, I downloaded the android binary from the sqlite website so I could modify the NuGet cache to include the custom downloaded SQLite library in the android-arm64/native folder

  3. I then had to edit the .csproj and create a.csproj.user to automatically copy the downloaded SQLite during build/publish

  4. I also made a shell script to kinda make all of this happen, but im not sure if the shell script was needed…or maybe not needed anymore? At first the wrong sqlite lib was still being included in the apk and it wasnt until i made the shell script that would kick off the build process and move the proper sqlite lib at the right time before i got things to work. Now, I can build from the editor and things work fine? We will see how long that lasts, lol

    Im also not sure if this is a unique problem that only a linux/godot/c# user would have or if theres an issue with NuGet and godot in general. Either way I figured i would update the thread in case anyone has issues in the future.

1 Like

SQLite is already installed on every Android phone.

1 Like

Yes it is, but since the build process was including the wrong version it wouldnt look for the libs already on the phone. I also tried to not include the libs from the build in order to find the libs on the device, but that didnt work either

Believe me, i tried what felt like a million different things. Different sql packages and versions, different code structures, and even using 3 different android devices…and nothing was working. It was odd to me since my project doest require some exotic setup. It just did not work out of the box. Theres a bug somewhere, and im not sure where it is or whos responsible. I can only share what happened and how i overcame it so others can avoid my frustration.

2 Likes

Thank you for sharing your experience and solution. Its always nice to google something and see the OP cared enough to comment how they fixed the problem

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.