Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question SQLite (native) DLL not working in Mac-Editor and Android Build

Discussion in 'Editor & General Support' started by ManuBera, Oct 17, 2022.

  1. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    Hey people,

    I desperately need your help. I've been working for weeks on a project that is based on SQLite + Entity Framework 6 and I reached a point where it works great on Windows (the platform I'm using for my work). Now I need this to work cross-platform and I just found out that it doesn't work inside the editor on Mac and it doesn't work in an Android build. It's a complete disaster.

    Inside the editor on Mac I get a runtime error when the connection to SQLite should be established:

    DllNotFoundException: SQLite.Interop.dll assembly:<unknown assembly> type:<unknown type> member: (null)
    System.Data.SQLite.SQLite3.StaticIsInitialized () (at <33e9c369d13c449ba79f1798811d3583>:0)
    System.Data.SQLite.SQLiteLog.PrivateInitialize (System.String className) (at <33e9c369d13c449ba79f1798811d3583>:0)
    System.Data.SQLite.SQLiteLog.Initialize (System.String className) (at <33e9c369d13c449ba79f1798811d3583>:0)
    System.Data.SQLite.SQLiteConnection..ctor (System.String connectionString, System.Boolean parseViaFramework) (at <33e9c369d13c449ba79f1798811d3583>:0)
    System.Data.SQLite.SQLiteConnection..ctor (System.String connectionString) (at <33e9c369d13c449ba79f1798811d3583>:0)
    System.Data.SQLite.SQLiteConnection..ctor () (at <33e9c369d13c449ba79f1798811d3583>:0)
    (wrapper remoting-invoke-with-check) System.Data.SQLite.SQLiteConnection..ctor()
    (omitted rest of project code that opens sqlite connection)

    I've noticed that the SQLite.Interop.dll is the only dependency that is a native dll and not a managed one. I suppose that's the problem. But how do I fix this? Even if I had the necessary .dylib and .so files, how would I tell Unity to use them instead of my dlls?

    And why is there so little to find around the internet about this? I can't be the only one trying to get this to work? Am I missing something simple?

    Please, if you have anything to help me out here, I would more than appreciate it. Even if it is just a lecture on native dlls and why this can't work the way I'm trying to.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    I've only used SQLite from here when working with Unity3D:

    https://github.com/codecoding/SQLite4Unity3d

    Works a treat. You gotta pull the database OUT of your project and write it to local storage on Android, that's about it.
     
    ManuBera likes this.
  3. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    I thank you once again for your help Kurt-Dekker! I need to use EF6 (or even better, EFCore) along with it for DLLs I build outside of Unity, so SQLite4Unity3d is more of a last resort. It seems I have no other choice, rendering weeks of work unusable :'(
     
    Kurt-Dekker likes this.
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,506
    You modify the "[DllImport]" attribute to specify the dll name without extension, like "[DllImport("SQLite.Interop")]". And you also need to configure each native so and dylib file in the plugin inspector to be included for the platforms they are built for.
     
    ManuBera likes this.
  5. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    Thank you very much! I tried it again with Sqlite and EF Core this time, copied all necessary dll files in the plugin folder and now it works inside the Unity editor on windows and mac, which seems like a huge success, but it won't work on Android and iOS even though the .so and .dylib files can be seen in the apk and the xcode build. I configured the linux-arm and linux-arm64 runtime .so files for android and the osx-arm64 runtime for iOS. On android I get the same DllNotFound exception, I suppose it's the same for iOS.

    It feels like I'm so close! Any idea what else I could try or what I could be doing wrong?
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,506
    You cannot use linux binaries on android and you cannot use macOS binaries on iOS. You need to compile those binaries for android and iOS specifically.
     
    ManuBera likes this.
  7. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    Thanks again, now I understand :) With the .so files found in the depths of this git (https://github.com/ericsink/cb) I was able to make it run on android and you wouldn't believe how happy I was - until I tried the dylib files on iOS and it didn't work. It is so frustrating! If I could get it running, weeks of my work would be saved.

    When I run it on iOS, I get the following error message

    InvalidOperationException: A suitable constructor for type 'Microsoft.EntityFrameworkCore.Internal.DbSetInitializer' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite (Microsoft.Extensions.DependencyInjection.ServiceLookup.ResultCache lifetime, System.Type serviceType, System.Type implementationType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact (Microsoft.Extensions.DependencyInjection.ServiceDescriptor descriptor, System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain, System.Int32 slot) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory+<>c__DisplayClass7_0.<GetCallSite>b__0 (System.Type type) [0x00000] in <00000000000000000000000000000000>:0
    at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteChain callSiteChain) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor (System.Type serviceType) [0x00000] in <00000000000000000000000000000000>:0
    at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService (System.Type serviceType, Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope serviceProviderEngineScope) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService (System.Type serviceType) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService (System.Type serviceType) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService (System.IServiceProvider provider, System.Type serviceType) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T] (System.IServiceProvider provider) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.DbContext..ctor (Microsoft.EntityFrameworkCore.DbContextOptions options) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.DbContext..ctor () [0x00000] in <00000000000000000000000000000000>:0
    at BBE.SqliteTest.SqliteContext..ctor (System.String path) [0x00000] in <00000000000000000000000000000000>:0
    at BBE.SqliteTest.DataAccess.CreateUser (System.String firstName) [0x00000] in <00000000000000000000000000000000>:0
    at SQLiteEFCoreTest.Start () [0x0001c] in /Users/macbookair/Documents/My Unity Projects/221018_Test/Assets/SQLiteTest0/SQLiteEFCoreTest.cs:12

    It seems to be the same problem another User is facing here: https://forum.unity.com/threads/ent...editor-crashes-when-ran-from-a-build.1255527/

    I tried disabling code stripping, but the same error message is produced. Do you have any idea what the issue could be or what else I could try?
     
  8. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    After realizing that iOS is IL2CPP only and disabling code stripping has no effect, I added a link.xml file that I found here: https://stackoverflow.com/questions/72598734/entity-framework-core-in-net-maui-xamarin-forms to preserve some attributes Entity Framework depends on. That maybe even worked, because now I got a whole new Exception:

    ExecutionEngineException: Attempting to call method 'Microsoft.EntityFrameworkCore.Metadata.Internal.ClrPropertySetterFactory[[Microsoft.EntityFrameworkCore.Metadata.IClrPropertySetter, Microsoft.EntityFrameworkCore, Version=5.0.17.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]::CreateGeneric<BBE.SqliteTest.SqliteContext,Microsoft.EntityFrameworkCore.DbSet`1[[BBE.SqliteTest.User, SqliteTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]],Microsoft.EntityFrameworkCore.DbSet`1[[BBE.SqliteTest.User, SqliteTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]>' for which no ahead of time (AOT) code was generated.
    at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrAccessorFactory`1[TAccessor].Create (System.Reflection.MemberInfo memberInfo, Microsoft.EntityFrameworkCore.Metadata.IPropertyBase propertyBase) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrAccessorFactory`1[TAccessor].Create (System.Reflection.MemberInfo memberInfo) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.Internal.DbSetFinder+<>c__DisplayClass2_0.<FindSetsNonCached>b__2 (System.Reflection.PropertyInfo p) [0x00000] in <00000000000000000000000000000000>:0
    at System.Linq.Enumerable+WhereSelectEnumerableIterator`2[TSource,TResult].MoveNext () [0x00000] in <00000000000000000000000000000000>:0
    at System.Linq.Buffer`1[TElement]..ctor (System.Collections.Generic.IEnumerable`1[T] source) [0x00000] in <00000000000000000000000000000000>:0
    at System.Linq.Enumerable.ToArray[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.Internal.DbSetFinder.FindSetsNonCached (System.Type contextType) [0x00000] in <00000000000000000000000000000000>:0
    at System.Collections.Concurrent.ConcurrentDictionary`2[TKey,TValue].GetOrAdd (TKey key, System.Func`2[T,TResult] valueFactory) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.Internal.DbSetFinder.FindSets (System.Type contextType) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.Internal.DbSetInitializer.InitializeSets (Microsoft.EntityFrameworkCore.DbContext context) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.DbContext..ctor (Microsoft.EntityFrameworkCore.DbContextOptions options) [0x00000] in <00000000000000000000000000000000>:0
    at Microsoft.EntityFrameworkCore.DbContext..ctor () [0x00000] in <00000000000000000000000000000000>:0
    at BBE.SqliteTest.SqliteContext..ctor (System.String path) [0x00000] in <00000000000000000000000000000000>:0
    at BBE.SqliteTest.DataAccess.CreateUser (System.String firstName) [0x00000] in <00000000000000000000000000000000>:0
    at SQLiteEFCoreTest.Start () [0x0001c] in /Users/macbookair/Documents/My Unity Projects/221018_Test/Assets/SQLiteTest0/SQLiteEFCoreTest.cs:12

    I'm so deep down the rabbit hole and if anyone could make sense of any of this, I would be forever grateful
     
    Last edited: Oct 21, 2022
  9. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,506
    Try switching IL2CPP build options to use "Faster (smaller) builds", that usually allows getting around that exception.
     
    ManuBera likes this.
  10. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    First of all, thank you so much for your continuous help, you just might be my last chance to succeed here! I switched to "Faster (smaller) builds", but now it won't even start my test-scene anymore, throwing the following error in Xcode:

    UnityFramework`il2cpp::vm::GlobalMetadata::GetGenericParameterFromIndex:
    -> 0x10c15d3dc <+0>: ldr w8, [x0, #0xc] = Thread 1: EXC_BAD_ACCESS (code=1, address = 0xc)
    0x10c15d3e0 <+4>: add w8, w8, w1
    0x10c15d3e4 <+8>: cmn w8, #0x1
    0x10c15d3e8 <+12>: b.eq 0x10c15d40c ; <+48> [inlined] GetGenericParameterFromIndexInternal(int) at GlobalMetadata.cpp
    0x10c15d3ec <+16>: adrp x9, 8295
    0x10c15d3f0 <+20>: ldr x9, [x9, #0xc98]
    0x10c15d3f4 <+24>: adrp x10, 8295
    0x10c15d3f8 <+28>: ldr x10, [x10, #0xca0]
    0x10c15d3fc <+32>: ldrsw x10, [x10, #0x68]
    0x10c15d400 <+36>: add x9, x9, x10
    0x10c15d404 <+40>: add x0, x9, w8, sxtw #4
    0x10c15d408 <+44>: ret
    0x10c15d40c <+48>: mov x0, #0x0
    0x10c15d410 <+52>: ret

    My link.xml looks like this:

    <?xml version="1.0" encoding="utf-8" ?>
    <linker>
    <!-- Entity Framework Core -->
    <assembly fullname="mscorlib">
    <type fullname="System.String">
    <method name="Compare"></method>
    <method name="CompareTo"></method>
    <method name="ToUpper"></method>
    <method name="ToLower"></method>
    </type>
    <type fullname="System.Reflection.AssemblyInformationalVersionAttribute" preserve="all" />
    </assembly>
    <assembly fullname="System.Core" />
    <assembly fullname="Microsoft.EntityFrameworkCore" />
    <assembly fullname="Microsoft.EntityFrameworkCore.Sqlite" />
    <assembly fullname="Microsoft.EntityFrameworkCore.Relational" />
    <assembly fullname="SQLitePCLRaw.core" />
    <assembly fullname="SQLitePCLRaw.batteries_v2" />
    <assembly fullname="SQLitePCLRaw.lib.e_sqlite3.ios" />
    </linker>

    Unity Editor version 2021.3.0f

    Again, thank you so much for your help!
     
    Last edited: Oct 21, 2022
  11. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,506
    I'd try updating to latest 2021.3.x version (currently 2021.3.11f1), as that might address this crash.
     
    ManuBera likes this.
  12. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    Thanks again, alas it did not work. I tried again with EF Core 3.1, which should also technically work and Dapper, but none of those worked due to AOT problems with the heavy use of reflection. Sadly I have to give up and think of another solution that will be less elegant for now.
     
  13. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,506
    Can you file a bug report on that? That error you mentioned about Xcode shouldn't be happening.
     
    ManuBera likes this.
  14. ManuBera

    ManuBera

    Joined:
    Aug 19, 2021
    Posts:
    70
    I will try my best to do it over the weekend :) I would still very much like to be able to use EF Core with SQLite so I don't have to code everything twice for the offline local variant of my plugin ^^
     
  15. Oksana-Iashchuk

    Oksana-Iashchuk

    Joined:
    Sep 10, 2012
    Posts:
    126
    I made my project 10 years ago, as result of compilation & build problem. you could use it https://u3d.as/3ka if performance is not your requirement. Maybe you will like it.
     
    ManuBera likes this.