Search Unity

AssetBundle with IL2CPP and reflection of a dll

Discussion in 'Android' started by drorriov, Aug 17, 2019.

  1. drorriov

    drorriov

    Joined:
    Jun 7, 2014
    Posts:
    43
    I am trying to load a DLL using AssetBundle, it actually works, but I am getting an error in the 'Assembly.Load' line:

    Code (CSharp):
    1. TextAsset txt = bundle.LoadAsset("libvideo", typeof(TextAsset)) as TextAsset;
    2.  var assembly = Assembly.Load(txt.bytes);
    The Error:
    08-17 12:46:09.189 9121 9307 E Unity : NotSupportedException: C:\Program Files\Unity\Hub\Editor\2019.1.11f1\Editor\Data\il2cpp\libil2cpp\icalls\mscorlib\System\AppDomain.cpp(182) : Unsupported internal call for IL2CPP:AppDomain::LoadAssemblyRaw - "This icall is not supported by il2cpp."
    08-17 12:46:09.189 9121 9307 E Unity : at System.AppDomain.Load (System.Byte[] rawAssembly, System.Byte[] rawSymbolStore, System.Security.Policy.Evidence securityEvidence, System.Boolean refonly) [0x00000] in <00000000000000000000000000000000>:0
    08-17 12:46:09.189 9121 9307 E Unity : at AssetBundlesManager+<Start>d__1.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
    08-17 12:46:09.189 9121 9307 E Unity : at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0

    Using Unity 2019.1.11
    Any workaround for this?
    Assistance would be appreciated.
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    You will need to make sure that any managed DLL which will be loaded at run time is present at compile time. IL2CPP is an ahead-of-time compiler, so it does not support loading of assemblies at run time.
     
  3. drorriov

    drorriov

    Joined:
    Jun 7, 2014
    Posts:
    43
    Hello,

    Thank you for your reply.
    I understand IL2CPP is an ahead-of-time compiler and would not work as it can't load assemblies at run time.
    Is there a workaround for this or a way that this can be done with IL2CPP?
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    There is not way to load assemblies at run time with IL2CPP. If you have code that you want to deliver in an asset bundle, you will either need to use the Mono scripting backend or build that code with the original app, and deliver non-code assets only in the asset bundle.
     
  5. Ubrano

    Ubrano

    Joined:
    Jul 23, 2017
    Posts:
    16
    @JoshPeterson

    Went through 10 pages of "Search Results for Query: assembly.load" results and threads and this thread still seemed the least inappropriate. It's been 2 years, so maybe something changed.

    Does this mean that with IL2CPP one can still do the following with a managed dll present at compile time:
    Code (CSharp):
    1.         [DllImport("my.dll")]
    2.         public static extern void MyFunction(arguments);
    But still cannot do anything in a sense of "Assembly.Load" at run time, even if we are talking about that assembly already being compiled and available in form of bytes?

    I have a managed dll available in bytes (which are also compressed), which I would like to load. It works with Mono as Scripting Backend (i.e. Assembly.Load(byte[] decompressedBytes)), but I need this with IL2CPP.

    Is there something to do at compile time perhaps?

    Any help would be appreciated. Thank you.
     
  6. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Not possible, when the app is built into IL2CPP, it's no longer capable of running or loading any kind of managed code because it was converted into something completely different.
     
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,938
    As @Neto_Kokku mentioned, this is not possible with IL2CPP. The managed assembly must be present at compile time, since IL2CPP must compile all assemblies ahead-of-time.