I have this code to load list of contacts from device:
ICursor cursor = null;
try {
cursor = (ICursor) new CursorLoader (activity, uri, projection, null, null, null).LoadInBackground ();
if (cursor.MoveToFirst ()) {
do {
try {
list.Add (new Contact (cursor.GetInt (cursor.GetColumnIndex (projection [0])), cursor.GetString (cursor.GetColumnIndex (projection [1]))));
} catch (Exception e) {
Log.Error ("TCH_error", "Contacts - GetListPermissionsDetermined - contact error:\n" + e.Message);
}
} while (cursor.MoveToNext ());
}
cursor.Close ();
} catch (Exception e) {
Log.Error ("TCH_e", "Contacts - RequestPermissionsDone - Exception:\n" + e.StackTrace);
Crashes.TrackError (e, new Dictionary<string, string> {
{"FailureAt", "Cursor"}
});
} finally {
try {
if (cursor != null) {
cursor.Close ();
}
} catch (Exception e) {
Log.Error ("TCH_e", "Contacts - RequestPermissionsDone - Exception:\n" + e.StackTrace);
}
}
Everything works fine when I am debugging the app, but Release builds of the app show System.InvalidCastException: Specified cast is not valid error on some devices. For example on Samsung phones both Debug and Release works, but e.g. Honor 8 shows this error.
I tried to debug it changing build settings and found out that if I enable Linking to SDK Assemblies Only I get this error. But if I leave Linking to None, then there is no error.
Is this an issue with Xamarin? Or is there something I can do, other then having to not use linking?
@tomaschyly , should something wrong with Linking
.
To resolve this issue, you could Custom Linker Configuration for your project. Create a myLink.xml
in your project, set the Build Action
to LinkDescription
, add the following code to the myLink.xml
file:
<linker>
<assembly fullname="Mono.Android">
<type fullname="Android.Database.*" />
</assembly>
<assembly fullname="Xamarin.Android.Support.Core.Utils">
<type fullname="Android.Support.V4.Content.*" />
</assembly>
</linker>
It did resolve this issue on my side.
Answers
In which line did this issue occurred? Please post the entire error trace.
I removed the try catch finally block to make sure I get the correct line and Visual Studio tells me it is on this line.
This happens only when Linking - SDK Assemblies Only is enabled. If I do not do any Linking, then it works.
Did this issue only happened on Honor 8 device? Have you tested it on emulator?
And would you mind sharing a basic demo that can reproduce the problem? Troubleshooting will be much convenient if we can reproduce this issue on our side.
Sorry, forgot to add, I retested it and it happens on all devices that I have access to. I noticed it first on Honor 8, but it happens on Samsungs with different Android versions.
Dont know if I will have time today, but I will put something together and share it with you.
Here is demo project link
@tomaschyly , should something wrong with
Linking
.To resolve this issue, you could Custom Linker Configuration for your project. Create a
myLink.xml
in your project, set theBuild Action
toLinkDescription
, add the following code to themyLink.xml
file:It did resolve this issue on my side.
Thank you, it works. I should have searched for a solution like this sooner myself.