Bug 26074 - [Android 5.0] InvalidCastException: Cannot cast from source type to destination type
Summary: [Android 5.0] InvalidCastException: Cannot cast from source type to destinati...
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.20.0
Hardware: PC Windows
: Normal normal
Target Milestone: 5.1
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-01-15 13:09 UTC by Jon Douglas [MSFT]
Modified: 2015-05-07 10:50 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Jon Douglas [MSFT] 2015-01-15 13:09:28 UTC
*Description:

JNIEnv.GetArray returns InvalidCastException when running on Lollipop Android 5.0. This doesn't happen on previous versions of Android.

*Reproduction

https://www.dropbox.com/s/sx8k8vrs36y8mkf/SMSTestProject.zip?dl=0

*Images

KitKat - https://www.dropbox.com/s/wx4spcn22igo8hq/Screen%20Shot%202015-01-14%20at%204.11.38%20pm.png?dl=0 (Object[])

Lollipop - https://www.dropbox.com/s/m6f485hyymj5q1f/Screen%20Shot%202015-01-14%20at%204.02.17%20pm.png?dl=0 (Notice a byte[][])

*Further Resources:

http://stackoverflow.com/questions/27544145/cannot-cast-from-source-type-to-destination-type-jnienv-getarrayjava-lang-obje

http://forums.xamarin.com/discussion/26529/jnienv-getarray-return-invalidcastexception-cannot-cast-from-source-type-to-desti

*Version Information

=== Xamarin Studio ===

Version 5.7 (build 661)
Installation UUID: d972ac10-8990-401f-8547-e38dcbd6882d
Runtime:
Mono 3.12.0 ((detached/a813491)
GTK+ 2.24.23 (Raleigh theme)

Package version: 312000068

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/ronaldli/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
2.1 (API level 7)
2.2 (API level 8)
2.3 (API level 10)
3.1 (API level 12)
4.0 (API level 14)
4.0.3 (API level 15)
4.1 (API level 16)
4.2 (API level 17)
4.3 (API level 18)
4.4 (API level 19)
4.4.87 (API level 20)
5.0 (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.6.0.51 (Business Edition)
Hash: dfb682f
Branch:
Build date: 2015-01-08 13:39:32-0500

=== Xamarin.Mac ===

Not Installed

=== Build Information ===

Release ID: 507000661
Git revision: b70bab61da996da29045ea8ee8aed1a6faedbe78
Build date: 2015-01-05 16:31:31-05
Xamarin addins: 82f6c71490562d6cd125a09287f441902fdac3d7

=== Operating System ===

Mac OS X 10.9.5
Darwin 192-168-1-143.tpgi.com.au 13.4.0 Darwin Kernel Version 13.4.0
Sun Aug 17 19:50:11 PDT 2014
root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 1 Udham Singh 2015-01-16 09:40:31 UTC
I have checked this issue with help of sample app given in description and able to reproduce this issue. To reproduce this issue I have followed the steps mentioned below.

1. Open sample 'SMSTestProject' in XS.
2. Set debugger on method 'OnReceive()' in class 'SMSBroadcastReceiver'.
3. Run the sample on android device Google Nexus 5 (Android 5.0).
4. Send a SMS to the same phone  and observed that debugger point get hit.
5. Debug the app and got the exception.

Exception Log : https://gist.github.com/Udham1/86b26c4a8ef18882b99c
Screencast : http://www.screencast.com/t/4CqkDdn69U

Environment Info :

=== Xamarin Studio ===

Version 5.7.1 (build 5)
Installation UUID: ce927b2a-2c07-44c5-b186-09cfdafba6dc
Runtime:
	Mono 3.12.0 ((detached/1538a59)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000072

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.6.0.52 (Enterprise Edition)
Hash: 7c4c2c5
Branch: 
Build date: 2015-01-08 22:29:16-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Enterprise Edition)
Android SDK: /Users/xamarin76/Desktop/android-sdk-macosx
	Supported Android versions:
		1.6    (API level 4)
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.Mac ===

Version: 1.11.3.0 (Enterprise Edition)

=== Build Information ===

Release ID: 507010005
Git revision: ef2b158f8e0495f6b696d97f1dc0b8208577a5a8
Build date: 2015-01-15 03:32:55-05
Xamarin addins: 12ef44f5cd3bba34b1982969f0d6a8d881ed00c9

=== Operating System ===

Mac OS X 10.9.5
Darwin Xamarin76s-Mac-mini.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 2 Thomas Van Machelen 2015-01-30 04:34:58 UTC
For what it's worth, I managed to extract the pdu byte array by using the following piece of code:

Object rawPdus = extras.Get("pdus");

// check the length using JNIEnvt
int length = JNIEnv.GetArrayLength(rawPdus.Handle);

if (length < 1)
    return;

// get the first item out manually
byte[] pdus = JNIEnv.GetArrayItem<byte[]>(rawPdus.Handle, 0);                
var msg = SmsMessage.CreateFromPdu(pdus);
Comment 3 Jonathan Pryor 2015-01-30 11:14:28 UTC
JNIEnv.GetArray<T>() allows T to be an array; this is much easier:

> byte[][] items = JNIEnv.GetArray<byte[]>(pdus.Handle);

That said, the InvalidCastException is worrying. Investigating...
Comment 4 Jonathan Pryor 2015-01-30 13:11:55 UTC
The cause for the InvalidCastException is that JNIEnv.CopyArray() was ignoring the elementType parameter if the contents of the array were themselves an array. Because of that, a "deep copy" was attempted by JNIEnv.GetArray<T>(), causing it to create a `byte[][]` instead of a `Java.Lang.Object[], hence failing the cast.

It worked prior to Lollipop because the actual runtime type of the array was java.lang.Object[], so the default "deep marshal" behavior was avoided. In Lollipop the array was instead a byte[][]. This change can be visible in Images from Comment #0:

Pre-Lollipop:
> pdus   {[Ljava.lang.Object;@42d93270}


Lollipop:
> pdus   {[[B@38ea2aae}

[[B is byte[][]; [Ljava.lang.Object; is java.lang.Object[].
Comment 5 Jonathan Pryor 2015-01-30 13:53:54 UTC
Fixed in monodrodi/64a45621.
Comment 6 Thomas Van Machelen 2015-01-30 14:07:42 UTC
Thanks for handling this so quickly! 

Just a small side note until the fix lands in stable: my workaround using GetArrayItem might be a bit more convoluted but it works on both lollipop and pre-lollipop.
Comment 7 Ram Chandra 2015-05-07 10:50:54 UTC
I have checked this issue and with the help of attached project I am able to reproduce this issue with following builds:

Windows 8.1
XS: 5.9.1.2
XA: 4.20.0
XVS:3.9.236.0

Screencast: http://www.screencast.com/t/TF60kcdKugsb

To verify this issue I have checked this issue with following builds:

Windows 8.1
XS:5.9.1.2
XA: 5.1.1

Screencast: http://www.screencast.com/t/GfeVNX7FYUfm

Now I am not getting any error/exception. I am able to run the attached application successfully.

This issue has been fixed. Hence I am closing this issue.