AWS SDK Bug when compiling to IL2CPP on Android

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

UnityInitializer.AttachToGameObject(this.gameObject);

which results in this error at runtime

I/Unity   ( 7756): (Filename: ./artifacts/generated/Android/runtime/UnityEngineDebugBindings.gen.cpp Line: 45)
I/Unity   ( 7756):
I/Unity   ( 7756): ExecutionEngineException: Attempting to call method 'UnityEngine.AndroidJavaObject::Get' for which no ahead of time (AOT) code was generated.
I/Unity   ( 7756):
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 (On Unity, compiling the Android version to il2cpp causes an ExecutionEngineException at runtime · Issue #477 · aws/aws-sdk-net · GitHub) 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.

In the github issue you can find a workaround that’s working for me.

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:

   #if UNITY_ANDROID
    public void UsedOnlyForAOTCodeGeneration() {
        //Bug reported on github https://github.com/aws/aws-sdk-net/issues/477
        //IL2CPP restrictions: https://docs.unity3d.com/Manual/ScriptingRestrictions.html
        //Inspired workaround: https://docs.unity3d.com/ScriptReference/AndroidJavaObject.Get.html

        AndroidJavaObject jo = new AndroidJavaObject("android.os.Message");
        int valueString = jo.Get<int>("what");
    }
    #endif

Hope this helps!

2 Likes

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

2 Likes

Still super useful. Thank you :slight_smile:

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?

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.

1 Like

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.

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.

Switch to .NET Standard 2.0 DLLs and it should work just fine.
https://forum.unity.com/threads/issue-with-system-configuration-configurationmanager-get_appsettings-on-ios.1026877/#post-6656764