New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling proguard in d15-7 causes crash on startup #1331

Closed
pjcollins opened this Issue Feb 22, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@pjcollins
Member

pjcollins commented Feb 22, 2018

Projects which enable proguard are crashing on startup when using d15-7. This is a regression as compared to d15-6.

Steps to Reproduce

  1. msbuild /t:Install /p:Configuration=Release /p:EnableProguard=true *.csproj

Version Information

Xamarin.Android SDK 8.3.0.0 (HEAD/a13b88d76)

Log File

Diagnostic installation output:
https://gist.github.com/pjcollins/c9a04e5da40e3b82379434abb07d69a0

02-22 14:52:56.680  1163  2477 I ActivityManager: Start proc 16357:mono.samples.button/u0a311 for activity mono.samples.button/md5e2fd3b03328de3df97c99ffd13b29e67.ButtonActivity
02-22 14:52:56.692  1859  1859 V StatusBar: setLightsOn(true)
02-22 14:52:56.728  1859  1859 V StatusBar: setLightsOn(true)
02-22 14:52:56.763 16357 16357 I chatty  : uid=10311(mono.samples.button) expire 1 line
02-22 14:52:56.794 16357 16357 W monodroid-gc: GREF GC Threshold: 46080
02-22 14:52:56.800 16357 16357 W monodroid: Calling into managed runtime init
02-22 14:52:56.829 16357 16357 W .samples.button: type=1400 audit(0.0:50169): avc: denied { read } for name="uptime" dev="proc" ino=4026532080 scontext=u:r:untrusted_app_25:s0:c512,c768 tcontext=u:object_r:proc:s0 tclass=file permissive=0
02-22 14:52:56.938 16357 16357 E mono    :
02-22 14:52:56.938 16357 16357 E mono    : Unhandled Exception:
02-22 14:52:56.938 16357 16357 E mono    : System.TypeInitializationException: The type initializer for 'Java.Interop.ManagedPeer' threw an exception. ---> System.ArgumentException: Handle must be valid.
02-22 14:52:56.938 16357 16357 E mono    : Parameter name: type
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.JniEnvironment+Types.IsInstanceOf (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type) [0x00022] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Android.Runtime.JNIEnv.IsInstanceOf (System.IntPtr obj, System.IntPtr clazz) [0x0000e] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.Runtime.IsGCUserPeer (System.IntPtr value) [0x0000f] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.Runtime.IsGCUserPeer (Android.Runtime.IJavaObject value) [0x0000b] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00127] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000b9] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Android.Runtime.AndroidRuntime.GetExceptionForThrowable (Java.Interop.JniObjectReference& value, Java.Interop.JniObjectReferenceOptions transfer) [0x00006] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.JniEnvironment+Types.FindClass (System.String classname) [0x0010c] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.JniType..ctor (System.String classname) [0x00006] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.JniType.GetCachedJniType (Java.Interop.JniType& cachedType, System.String classname) [0x00018] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.JniPeerMembers.get_JniPeerType () [0x0000c] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.ManagedPeer..cctor () [0x00019] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.938 16357 16357 E mono    :    --- End of inner exception stack trace ---
02-22 14:52:56.938 16357 16357 E mono    :   at Java.Interop.JniRuntime..ctor (Java.Interop.JniRuntime+CreationOptions options) [0x00229] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Android.Runtime.AndroidRuntime..ctor (System.IntPtr jnienv, System.IntPtr vm, System.Boolean allocNewObjectSupported, System.IntPtr classLoader, System.IntPtr classLoader_loadClass) [0x0000d] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.938 16357 16357 E mono    :   at Android.Runtime.JNIEnv.Initialize (Android.Runtime.JnienvInitializeArgs* args) [0x0017c] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt : [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'Java.Interop.ManagedPeer' threw an exception. ---> System.ArgumentException: Handle must be valid.
02-22 14:52:56.939 16357 16357 E mono-rt : Parameter name: type
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.JniEnvironment+Types.IsInstanceOf (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type) [0x00022] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Android.Runtime.JNIEnv.IsInstanceOf (System.IntPtr obj, System.IntPtr clazz) [0x0000e] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.Runtime.IsGCUserPeer (System.IntPtr value) [0x0000f] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.Runtime.IsGCUserPeer (Android.Runtime.IJavaObject value) [0x0000b] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType) [0x00127] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type) [0x000b9] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00017] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Android.Runtime.AndroidRuntime.GetExceptionForThrowable (Java.Interop.JniObjectReference& value, Java.Interop.JniObjectReferenceOptions transfer) [0x00006] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.JniEnvironment+Types.FindClass (System.String classname) [0x0010c] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.JniType..ctor (System.String classname) [0x00006] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.JniType.GetCachedJniType (Java.Interop.JniType& cachedType, System.String classname) [0x00018] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.JniPeerMembers.get_JniPeerType () [0x0000c] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.ManagedPeer..cctor () [0x00019] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.939 16357 16357 E mono-rt :    --- End of inner exception stack trace ---
02-22 14:52:56.939 16357 16357 E mono-rt :   at Java.Interop.JniRuntime..ctor (Java.Interop.JniRuntime+CreationOptions options) [0x00229] in <e5da7a7a83124ab3827f81a186993213>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Android.Runtime.AndroidRuntime..ctor (System.IntPtr jnienv, System.IntPtr vm, System.Boolean allocNewObjectSupported, System.IntPtr classLoader, System.IntPtr classLoader_loadClass) [0x0000d] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.939 16357 16357 E mono-rt :   at Android.Runtime.JNIEnv.Initialize (Android.Runtime.JnienvInitializeArgs* args) [0x0017c] in <546fec40177a4027a686650d5da2a824>:0
02-22 14:52:56.972  1163  2477 I ActivityManager: Process mono.samples.button (pid 16357) has died: fore TOP
02-22 14:52:56.972   762   762 I Zygote  : Process 16357 exited cleanly (1)
02-22 14:52:56.973  1163  1179 W zygote64: kill(-16357, 9) failed: No such process
@radekdoulik

This comment has been minimized.

Contributor

radekdoulik commented Feb 28, 2018

Looks like we need to tell proguard to keep ManagedPeer class around.

radekdoulik added a commit to radekdoulik/xamarin-android that referenced this issue Feb 28, 2018

[proguard] Preserve ManagedPeer class
After XA stopped using Java.Interop built in XAIntegration
configuration, we need com.xamarin.java_interop.ManagedPeer to be
present in the mono.android.jar file, as managed ManagedPeer type
requires it.

Thus we need to preserve that class when using proguard.

Fixes xamarin#1331

jonpryor added a commit that referenced this issue Feb 28, 2018

[proguard] Preserve ManagedPeer class (#1351)
Fixes: #1331

Apps built with ProGuard enabled crash on startup:

	$ msbuild /t:Install /p:Configuration=Release /p:EnableProguard=true *.csproj
	# launch the app, and `adb logcat` contains:
	Unhandled Exception:
	System.TypeInitializationException: The type initializer for 'Java.Interop.ManagedPeer' threw an exception. ---> System.ArgumentException: Handle must be valid.
	Parameter name: type
	  at Java.Interop.JniEnvironment+Types.IsInstanceOf (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type)
	  at Android.Runtime.JNIEnv.IsInstanceOf (System.IntPtr obj, System.IntPtr clazz)
	  at Java.Interop.Runtime.IsGCUserPeer (System.IntPtr value)
	  at Java.Interop.Runtime.IsGCUserPeer (Android.Runtime.IJavaObject value)
	  at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType)
	  at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type)
	  at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer)
	  at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer)
	  at Android.Runtime.AndroidRuntime.GetExceptionForThrowable (Java.Interop.JniObjectReference& value, Java.Interop.JniObjectReferenceOptions transfer)
	  at Java.Interop.JniEnvironment+Types.FindClass (System.String classname)
	  at Java.Interop.JniType..ctor (System.String classname)
	  at Java.Interop.JniType.GetCachedJniType (Java.Interop.JniType& cachedType, System.String classname)
	  at Java.Interop.JniPeerMembers.get_JniPeerType ()
	  at Java.Interop.ManagedPeer..cctor ()
	   --- End of inner exception stack trace ---
	  at Java.Interop.JniRuntime..ctor (Java.Interop.JniRuntime+CreationOptions options)
	  at Android.Runtime.AndroidRuntime..ctor (System.IntPtr jnienv, System.IntPtr vm, System.Boolean allocNewObjectSupported, System.IntPtr classLoader, System.IntPtr classLoader_loadClass)
	  at Android.Runtime.JNIEnv.Initialize (Android.Runtime.JnienvInitializeArgs* args)

After Xamarin.Android stopped using Java.Interop built against the
`XAIntegration*` configuration (7d32ef3), we need to ensure that the
type `com.xamarin.java_interop.ManagedPeer` -- found within 
`mono.android.jar` -- is preserved within the final `classes.dex`, as
`Java.Interop.ManagedPeer` requires it.

Thus we need to preserve that class when using ProGuard.

jonpryor added a commit that referenced this issue Feb 28, 2018

[proguard] Preserve ManagedPeer class (#1351)
Fixes: #1331

Apps built with ProGuard enabled crash on startup:

	$ msbuild /t:Install /p:Configuration=Release /p:EnableProguard=true *.csproj
	# launch the app, and `adb logcat` contains:
	Unhandled Exception:
	System.TypeInitializationException: The type initializer for 'Java.Interop.ManagedPeer' threw an exception. ---> System.ArgumentException: Handle must be valid.
	Parameter name: type
	  at Java.Interop.JniEnvironment+Types.IsInstanceOf (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type)
	  at Android.Runtime.JNIEnv.IsInstanceOf (System.IntPtr obj, System.IntPtr clazz)
	  at Java.Interop.Runtime.IsGCUserPeer (System.IntPtr value)
	  at Java.Interop.Runtime.IsGCUserPeer (Android.Runtime.IJavaObject value)
	  at Java.Interop.TypeManager.CreateInstance (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type targetType)
	  at Java.Lang.Object.GetObject (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer, System.Type type)
	  at Java.Lang.Object._GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer)
	  at Java.Lang.Object.GetObject[T] (System.IntPtr handle, Android.Runtime.JniHandleOwnership transfer)
	  at Android.Runtime.AndroidRuntime.GetExceptionForThrowable (Java.Interop.JniObjectReference& value, Java.Interop.JniObjectReferenceOptions transfer)
	  at Java.Interop.JniEnvironment+Types.FindClass (System.String classname)
	  at Java.Interop.JniType..ctor (System.String classname)
	  at Java.Interop.JniType.GetCachedJniType (Java.Interop.JniType& cachedType, System.String classname)
	  at Java.Interop.JniPeerMembers.get_JniPeerType ()
	  at Java.Interop.ManagedPeer..cctor ()
	   --- End of inner exception stack trace ---
	  at Java.Interop.JniRuntime..ctor (Java.Interop.JniRuntime+CreationOptions options)
	  at Android.Runtime.AndroidRuntime..ctor (System.IntPtr jnienv, System.IntPtr vm, System.Boolean allocNewObjectSupported, System.IntPtr classLoader, System.IntPtr classLoader_loadClass)
	  at Android.Runtime.JNIEnv.Initialize (Android.Runtime.JnienvInitializeArgs* args)

After Xamarin.Android stopped using Java.Interop built against the
`XAIntegration*` configuration (7d32ef3), we need to ensure that the
type `com.xamarin.java_interop.ManagedPeer` -- found within 
`mono.android.jar` -- is preserved within the final `classes.dex`, as
`Java.Interop.ManagedPeer` requires it.

Thus we need to preserve that class when using ProGuard.
@jonpryor

This comment has been minimized.

Contributor

jonpryor commented Feb 28, 2018

This fix has been cherry-picked to the d15-7 branch as 544cb6d.

@pjcollins

This comment has been minimized.

Member

pjcollins commented Mar 27, 2018

Verified fixed in XA 8.3.0.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment