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
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.
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
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.
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);
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.