Search Unity

AWS SDK Bug when compiling to IL2CPP on Android

Discussion in 'Formats & 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:
    9
    In the github issue you can find a workaround that's working for me.
     
  3. samuel-levine

    samuel-levine

    Joined:
    Sep 1, 2012
    Posts:
    11
    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!
     
    Piroshi001 and NeatWolf like this.
  4. NeatWolf

    NeatWolf

    Joined:
    Sep 27, 2013
    Posts:
    924
    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
     
    travlake and samuel-levine like this.
  5. Piroshi001

    Piroshi001

    Joined:
    May 16, 2014
    Posts:
    5
    Still super useful. Thank you :)
     
  6. Fbrzd

    Fbrzd

    Joined:
    Feb 18, 2021
    Posts:
    2
    still works in 2021, unity 2019.2.6f.

    by curiosity... this "fix" the exception issue of il2cpp? the async/await behaviour? or just helps the aws-sdk?
     
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    The issue here is that the AWD code is hitting a corner case for ahead-of-time code generation. IL2CPP cannot "see" that this generic code will be used at run time, so it does not generate the proper code for it.

    Note that in the next 2021.1 alpha (which should be released soon) IL2CPP has optional support to work around this issue by changing the way it handles generic code. Look for more details about how to enable this coming soon on the Experimental Scripting Previews forums section.
     
    asus4 likes this.
  8. AnkitSharma_7898

    AnkitSharma_7898

    Joined:
    Mar 4, 2022
    Posts:
    1
    Hi @JoshPeterson,
    I am getting the issue when working up with AWS SDK at this part of code:
    AmazonCognitoIdentityProviderClient provider = new AmazonCognitoIdentityProviderClient(new Amazon.Runtime.AnonymousAWSCredentials(), Amazon.RegionEndpoint.USWest2);

    Followed this link for login:
    https://github.com/aws-samples/serv...gnito User Pools Authentication Lab/README.md

    and the error comes when i run the APK on the device :

    System.TypelnitializationException: The type initializer for"Amazon.AWSConfigs' threw

    an exception.

    -> System.NotSupportedException:

    System.Configuration.ConfigurationManager..get_AppSettings

    linker file added in the project
    Using AWSSDK.Core (3.3.0)
    So there is no unityinitializer class available.

    Also I am building it through MacOS (12.2).
    Unity Version : 2020.3.27f1

    *Doing Scripting backend to Mono is working fine though.

    Please let me know if there is some solution available to this problem.
     
  9. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,936
    The System.Configuration.ConfigurationManager API is not supported by IL2CPP. I'm not familiar with the AWS code, but you may need to come up with some way to work around this limitation by modifying the code or by catching the exception and continuing using another code path.
     
  10. jalajshah

    jalajshah

    Joined:
    Mar 5, 2018
    Posts:
    62
    Switch to .NET Standard 2.0 DLLs and it should work just fine.
    https://forum.unity.com/threads/iss...-get_appsettings-on-ios.1026877/#post-6656764