luca-piccioni / OpenGL.Net

Modern OpenGL bindings for C#.
MIT License
570 stars 109 forks source link

Android - Release build crashes on startup #23

Closed matyasf closed 7 years ago

matyasf commented 7 years ago

When I make a release build with shared runtime turned off in the build settings (I could not reproduce with debug builds) the app crashes. The issue is in GetProcAddress line 42:

bool eglOnAndroid = Type.GetType("Android.OS.Build, Mono.Android.dll") != null;

evaluates to false in this case. I have no idea why it happens on release builds only.

luca-piccioni commented 7 years ago

I wonder too. Could it be an optimization issue? The assembly is not loaded at that point?

http://stackoverflow.com/questions/10519078/detecting-different-operating-systems-and-platforms

Maybe other checks are more portable. Essentially you need to detect Android runtime.

matyasf commented 7 years ago

After lots of experimenting I've figured out how to do it:

        public static bool IsAndroid()
        {
            Assembly[] assems = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var item in assems)
            {
                if (item.GetName().Name == "Mono.Android")
                {
                    return true;
                }
            }
            return false;
        }

When I replace this function call with your check it works, even when I dont use shared runtime, it works in static constructors. (I've got no idea why). Feel free to use this code; although this feels to me like a hack because I have no idea why it works, so when it could break in the future. (btw your updated code still does not work on my Xperia when the project does not use shared runtime)

Instead I suggest to either use compiler flags + ifdefs (you define the ANDROID flag in OpenGL.NET.Xamarin.Android and use an ifdef in Platform.cs) or do it like Xamarin.Forms, where the platform's value is set in the android project: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Forms.cs#L123

luca-piccioni commented 7 years ago

If you're still interested, now the project has a Xamarin project configuration.