- Home /
How do I prevent IL2CPP from Erroring on managed plugin code designed for iOS when I attempt to build for Android?
I’ve been banging my head against a C# coding problem for the last few days.
The problem is this - we're developing a Unity plugin and issuing the code as DLLs for use by game developers. The developers would then plumb code from our DLLs into their Unity programmes by drag-and drop.
So far so good.
Unfortunately one of the compilers that some developers use to build for Android (IL2CPP) breaks if it encounters managed iOS code, and we use some plugin code on iOS and Android to detect hardware capabilites.
Specifically, "IL2CPP requires that all native methods used in managed code be present at link time, where Mono does not,” as quoted in https://forum.unity.com/threads/build-fails-with-il2cpp.523955/ - the native methods in managed code being our iOS plugin in this case.
Now, normally we’d just wrap the code in an #if UNITY_IOS preprocessor directive, job done. But because we’re compiling the code into DLLs to ship, we’d need to make the choice at compile time. Which means that we can either compile a version that works on Android, or a version that works on iOS, but not both.
I’ve tried several routes to get around this, none of which has worked as yet. As soon as there’s a reference to the iOS code (eg in a switch statement to only use it at runtime) the Android compiler will follow the path to the native iOS methods, be unable to parse them, and Error. But if I try stripping out the code with a preprocessor directive, it no longer works on iPhone, because we’ve stripped the code out of the DLL.
I know there must be a solution to this, presumably involving loading the class at runtime, as otherwise how could you create a single package in C# for anything that ran on different types of hardware? But at the moment I still don’t know how to crack it.
Answer by Bunny83 · Jun 13, 2020 at 01:13 PM
Uhm every DLL or library (managed or native) has plugin import settings when you select it in the editor. You can choose which platforms a library should be used for. You generally should have seperate libraries for different platforms and set the import settings on them properly.
However apart from the import settings you should still be able to use the proper platform subfolders in the plugins folder to automatically choose the correct import settings.
Your answer
Follow this Question
Related Questions
IL2CPP Build errors on xCode 0 Answers
Android build failing on 2017.2.0f3 2 Answers
IL2CPP compatibility in iOS 1 Answer
Android build crash - stack corruption detected Unity 2018.3.8f1 2 Answers