Search Unity

AWS SDK Bug when compiling to IL2CPP on Android

Discussion in 'External Tools' started by Jesmond_Chia, Dec 5, 2016.

  1. Jesmond_Chia

    Jesmond_Chia

    Joined:
    Dec 11, 2015
    Posts:
    2
    Hi guys,

    My Unity project builds fine on Android using Mono and iOS using IL2CPP but when I tried to run my game after building to Android using IL2CPP, it seems that there is an issue with one of the core AWS functions. I managed to isolate the problem to this particular line of code

    Code (CSharp):
    1. UnityInitializer.AttachToGameObject(this.gameObject);
    which results in this error at runtime

    Code (CSharp):
    1. I/Unity   ( 7756): (Filename: ./artifacts/generated/Android/runtime/UnityEngineDebugBindings.gen.cpp Line: 45)
    2. I/Unity   ( 7756):
    3. I/Unity   ( 7756): ExecutionEngineException: Attempting to call method 'UnityEngine.AndroidJavaObject::Get' for which no ahead of time (AOT) code was generated.
    4. I/Unity   ( 7756):
    5. I/Unity   ( 7756): Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    After hitting this error, the AWS object will return null when attempting to access it later. This test was done using Unity 5.4.2 and using the latest version of AWS. When attempting to look into this error on the AWS side, I found this issue report on their GitHub (https://github.com/aws/aws-sdk-net/issues/477) but the AWS side have insisted that this issue has to do with IL2CPP itself so I'm raising this issue on the Unity side so that Unity is made aware of this issue and, if needed, liaise with Amazon to fix this issue.
     
  2. jotamaza

    jotamaza

    Joined:
    Apr 12, 2015
    Posts:
    5
    In the github issue you can find a workaround that's working for me.
     
  3. samuel-levine

    samuel-levine

    Joined:
    Sep 1, 2012
    Posts:
    2
    Confirming for posterity, that workaround in the github issue worked for me - even almost two years after the last comment, and almost 3 after the original post. And it's still not fixed in Unity 2018.4.4f1 with the latest AWS SDK.

    In case it saves anyone else some stress, the fix is to include this code block in any class which loads before UnityInitializer.AttachToGameObject is called. You don't actually call this method, you just include it in the class:

    Code (CSharp):
    1.    #if UNITY_ANDROID
    2.     public void UsedOnlyForAOTCodeGeneration() {
    3.         //Bug reported on github https://github.com/aws/aws-sdk-net/issues/477
    4.         //IL2CPP restrictions: https://docs.unity3d.com/Manual/ScriptingRestrictions.html
    5.         //Inspired workaround: https://docs.unity3d.com/ScriptReference/AndroidJavaObject.Get.html
    6.  
    7.         AndroidJavaObject jo = new AndroidJavaObject("android.os.Message");
    8.         int valueString = jo.Get<int>("what");
    9.     }
    10.     #endif
    Hope this helps!
     
    NeatWolf likes this.
  4. NeatWolf

    NeatWolf

    Joined:
    Sep 27, 2013
    Posts:
    846
    Still useful after years!

    On desktop it was working as a charme, but on android it was failing silently (no code generated, discovered via logcat).

    It's VERY annoying and I would love if an exception was actually thrown... took a while to debug!
    Anyway, the funny thing is:

    It's in the official docs, lol
    https://docs.unity3d.com/Manual/ScriptingRestrictions.html
     
    samuel-levine likes this.