aws-sdk-net: On Unity, compiling the Android version to il2cpp causes an ExecutionEngineException at runtime

My code is working fine on Android, with Mono, and on iOS with il2cpp but I wanted to try compiling my Unity game on Android with il2cpp, as it is now supported, but I have encountered an error which causes the UnityMainThreadDispatcher to be null.

In my GameController object I have a script which attaches the UnityInitialiser to itself, as instructed:

UnityInitializer.AttachToGameObject(gameObject);

Compiling this and running it will cause this exception to occur:

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.

Which disrupts the initialisation and UnityMainThreadDispatcher is not correctly added as a component to my GameController. (It will be null if I attempt to retrieve it). Searching about this error lead me to this (CTRL+F “ExecutionEngineException”) if that helps.

It is hard for me to debug this more as I am just using the provided DLLs on a non-Windows machine. I am guessing this is just a case of Android il2cpp not being supported, if that is the case then I hope it will be supported in the future.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 29 (4 by maintainers)

Most upvoted comments

Here is a workaround that I use works for my project.

Put below method in one of your GameObject, preferably with one that loads quite early on and in every scene.

	#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

Hi all, I finally got it working. Just to be sure to prevent it from happening again soon, I upgraded my project to 2019.2.13f1 (from 2018.3) I built the latest AWSSDK (aws-sdk-net-3.3.637.0) (btw I had to change the default Unity path in GeneratorOptions.cs, compile and run the generator so it gets the good UnityEngine.dll there might be a better way, NuGet?, but still it worked) with the linq reflection fixes (https://github.com/aws/aws-sdk-net/pull/1295), and added UsedOnlyForAOTCodeGeneration (required). On my side I was also missing the link.xml file that is required when using IL2CPP. If anyone is interested in details I can help if needed, just contact me. Thank you everyone for your help here. I was starting to loose hope in publishing soon. Cheers!

@sefiktemel @SimonStHilaire The AWS unity sdk is not working, is there a way to fix it, or link to a new sdk with instructions on how to impalement it?

I got the sdk from here And get errors as seen here

I also replaced the AWSSDK.Core.DLL file, and it still wont work.

Please help, thank you very much.

Note that you should not execute that method. Just create it and leave it there. And it should be only on android platform so that’s why #if UNITY_ANDROID.

We needed both get<int> and get<string> it might be related to what features is used.

So current fix which works for us looks like this.

#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");
                string stringValue = jo.Get<string>("what");
	}
	#endif