Bug 61073 - ClassNotFoundexception is thrown when deriving from Application class
Summary: ClassNotFoundexception is thrown when deriving from Application class
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: General (show other bugs)
Version: 8.1 (15.5)
Hardware: PC Windows
: High critical
Target Milestone: 15.6
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2017-12-11 21:50 UTC by Ruben Buniatyan
Modified: 2018-01-07 15:51 UTC (History)
15 users (show)

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


Attachments
Project to reproduce the issue (6.35 KB, application/x-zip-compressed)
2017-12-11 21:51 UTC, Ruben Buniatyan
Details
Test project (7.97 KB, application/x-zip-compressed)
2017-12-11 23:48 UTC, Ruben Buniatyan
Details
classes.dex (1.52 MB, application/x-zip-compressed)
2017-12-13 15:30 UTC, Ruben Buniatyan
Details
Repro using randomly named classes. (631.81 KB, application/x-zip-compressed)
2017-12-22 17:40 UTC, Daniel Vaughan
Details
Small repro project (1.03 MB, application/x-7z-compressed)
2017-12-23 05:06 UTC, Roman
Details
Xamarin.Android.Build.Tasks.dll (Based on 15-5 Stable) (833.00 KB, application/x-ms-dos-executable)
2018-01-04 16:15 UTC, dean.ellis
Details

Description Ruben Buniatyan 2017-12-11 21:50:46 UTC
# Steps to reproduce
See the project attached

# Expected behavior
Having a class inherited from Application class and marked [Application] should not throw ClassNotFoundexception. It must simply work.

# Actual behavior
ClassNotFoundexception is thrown

# Supplemental info (logs, images, videos)


# Test environment (full version information)
Microsoft Visual Studio Enterprise 2017 
Version 15.5.1
VisualStudio.15.Release/15.5.1+27130.2003
Microsoft .NET Framework
Version 4.7.02556

Installed Version: Enterprise

Visual Basic 2017   00369-90000-00000-AA049
Microsoft Visual Basic 2017

Visual C# 2017   00369-90000-00000-AA049
Microsoft Visual C# 2017

Visual C++ 2017   00369-90000-00000-AA049
Microsoft Visual C++ 2017

Visual F# 4.1   00369-90000-00000-AA049
Microsoft Visual F# 4.1

Application Insights Tools for Visual Studio Package   8.10.01106.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017   15.0.31125.0
ASP.NET and Web Tools 2017

ASP.NET Core Razor Language Services   1.0
Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2017   5.2.51007.0
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   15.0.31106.0
Azure App Service Tools v3.0.0

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

JavaScript Language Service   2.0
JavaScript Language Service

JetBrains ReSharper Ultimate 2017.2.2    Build 109.0.20171006.122324
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2017 JetBrains, Inc.

Merq   1.1.17-rc (cba4571)
Command Bus, Event Stream and Async Manager for Visual Studio extensions.

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2017 - v2.9.51120.3

Microsoft Continuous Delivery Tools for Visual Studio   0.3
Simplifying the configuration of continuous build integration and continuous build delivery from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   4.8.4-pre (3fe64e3)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   4.5.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Project System Tools   1.0
Tools for working with C#, VisualBasic, and F# projects.

SQL Server Data Tools   15.1.61710.120
Microsoft SQL Server Data Tools

TypeScript Tools   15.5.11025.1
TypeScript Tools for Microsoft Visual Studio

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   4.8.0.753 (6575bd113)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   4.8.183 (2577c82ea)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin.Android SDK   8.1.0.24 (HEAD/9cfa7836b)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   11.6.1.2 (6857dfc)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Comment 1 Ruben Buniatyan 2017-12-11 21:51:20 UTC
Created attachment 25976 [details]
Project to reproduce the issue
Comment 2 Jon Douglas [MSFT] 2017-12-11 22:45:31 UTC
Hello,

Thank you for your report.

The sample you have provided is an iOS project. Please upload an Android one if possible.

I have blogged on this topic a couple times:

https://www.jon-douglas.com/2016/09/05/xamarin-android-multidex/
https://www.jon-douglas.com/2016/09/23/xamarin-android-multidex-keep/

There are two other known issues reported on this item:

https://bugzilla.xamarin.com/show_bug.cgi?id=55268
https://bugzilla.xamarin.com/show_bug.cgi?id=55050

This bug best matches the description of 55050 and thus I am marking this as a duplicate of 55050.

*** This bug has been marked as a duplicate of bug 55050 ***
Comment 3 Ruben Buniatyan 2017-12-11 23:48:37 UTC
Created attachment 25977 [details]
Test project
Comment 4 Ruben Buniatyan 2017-12-11 23:49:00 UTC
Sorry about that, attached the right project.
Comment 5 Ruben Buniatyan 2017-12-12 01:59:04 UTC
@Jon Douglas
I think this bug is a regression as I had no problem with my Application class before updating to 15.5 (from 15.4). Are you sure this is a duplicate? If yes, then please explain the workaround described in #55050. I'm not sure I know how to apply it.
Comment 6 Ruben Buniatyan 2017-12-12 21:19:23 UTC
@Jon Any update on my last question?
Comment 7 Jon Douglas [MSFT] 2017-12-12 21:28:45 UTC
(In reply to Ruben Buniatyan from comment #5)
> @Jon Douglas
> I think this bug is a regression as I had no problem with my Application
> class before updating to 15.5 (from 15.4). Are you sure this is a duplicate?
> If yes, then please explain the workaround described in #55050. I'm not sure
> I know how to apply it.

I do not believe this is a regression. We have not had any work done around multidex since 15.4. The workaround is outlined in my blog article here:

https://www.jon-douglas.com/2016/09/23/xamarin-android-multidex-keep/

I would suspect that in your case, the application subclass happened to make the main dex list and is why you did not encounter this issue until additional classes were added. The solution that we need additional work in the future is to ensure this class ALWAYS makes the main dexlist.

You can alternatively use proguard instead of multidex and shrink your dexlist:

https://www.jon-douglas.com/2016/11/22/xamarin-android-proguard/
Comment 8 Ruben Buniatyan 2017-12-12 22:12:04 UTC
Jon, my app is a single-dex one. Where else can be that class? It's not a multidex app. Didn't you try the test project I attached?
Comment 9 Jon Douglas [MSFT] 2017-12-12 22:27:24 UTC
(In reply to Ruben Buniatyan from comment #8)
> Jon, my app is a single-dex one. Where else can be that class? It's not a
> multidex app. Didn't you try the test project I attached?

No I did not because the first attachment was an iOS project when I last checked this bug report.

I just checked the sample project, and it builds in both Debug and Release just fine.

If this is occuring locally, please try to clear out your bin/obj folders prior to building.

You can additionally attach your "classes.dex" file found in your obj/{Configuration}/android/bin folder which will indicate whether or not this class is making the dex list.

See a similar answer here:

https://stackoverflow.com/questions/47722787/why-is-my-new-activity-class-not-being-found-im-in-xamarin-android

Let me know what you observe.
Comment 10 Ruben Buniatyan 2017-12-13 15:28:54 UTC
Strangely, the demo project started working for me too. However, my main project still have problems. I uninstalled the current app, cleaned the project, even deleted obj and bin folders and rebuilt the project.

From Android Device Monitor:

java.lang.RuntimeException: Unable to instantiate application md5e5e12620a78d9a8810013540030dd9a0.App: java.lang.ClassNotFoundException: Didn't find class "md5e5e12620a78d9a8810013540030dd9a0.App" on path:...

There's no md5e5e12620a78d9a8810013540030dd9a0.App class in classes.dex (see attached). Instead, there's md5a44b0cb37d4b036bc2ffb6ada0d0d4c5.App class. As you can see, hashes are different. However, I do have md5e5e12620a78d9a8810013540030dd9a0.App in obj\Debug\android\bin\classes.

I want to stress again, that my app is a single-dex app. Rolling back to the previous version of Xamarin.Android fixes this. Do you still think this is a duplicate bug?
Comment 11 Ruben Buniatyan 2017-12-13 15:30:15 UTC
Created attachment 25993 [details]
classes.dex
Comment 12 Ruben Buniatyan 2017-12-14 17:28:53 UTC
@Jon Any updates please?
Comment 13 Jon Douglas [MSFT] 2017-12-14 17:38:51 UTC
(In reply to Ruben Buniatyan from comment #12)
> @Jon Any updates please?

I would recommend attempting to "rename" the ACW instead of giving it the default md5 hash as it looks like it's changing. See the following documentation:

https://developer.xamarin.com/guides/android/advanced_topics/java_integration_overview/android_callable_wrappers/#Android_Callable_Wrapper_Naming

Let me know if this fixes your issue. I have REOPENED this bug due to it not falling under the known multdiex limitation but rather a ACW naming one that both have similar symptoms and error messages.
Comment 14 Ruben Buniatyan 2017-12-15 00:25:44 UTC
@Jon
Yes, setting the name with [Application(Name="")] helped.
Honestly, I tried it before opening this issue and it didn't help. This time I uninstalled the app and did a clean.

This is a workaround, of course, but I hope you're gonna address this as soon as possible.
Comment 15 Ruben Buniatyan 2017-12-15 16:19:11 UTC
@Jon
I'm constantly getting various ClassNotFoundException errors for different classes. Clean up helps but they appear after a few builds. This just make it impossible to work with Xamarin 15.5. For newly created projects I don't see this problem but seems for bigger ones this is a blocker.

Clearly, this is a *regression* as 15.4 doesn't have such issues. Can we hope to have a fix with the next service release?
Comment 16 Daniel Vaughan 2017-12-15 16:38:41 UTC
I am also experiencing this in 15.5.2.
First it was a custom Application class that was not found. Then, rebuilding and debugging raised a ClassNotFoundException for a different class.

12-15 17:35:23.391 W/monodroid( 8788): JNIEnv.FindClass(Type) caught unexpected exception: Java.Lang.ClassNotFoundException: md50039d44cbb3b194ba4f4e52eaa252795.BrowserPagerAdapter ---> Java.Lang.ClassNotFoundException: Didn't find class "md50039d44cbb3b194ba4f4e52eaa252795.BrowserPagerAdapter" on path: DexPathList[[zip file "/data/app/com.outcoder.browser-1/base.apk"],nativeLibraryDirectories=[/data/app/com.outcoder.browser-1/lib/arm, /vendor/lib, /system/lib]]
12-15 17:35:23.391 W/monodroid( 8788):    --- End of inner exception stack trace ---
12-15 17:35:23.391 W/monodroid( 8788):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <657aa8fea4454dc898a9e5f379c58734>:0 
12-15 17:35:23.391 W/monodroid( 8788):   at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <54816278eed9488eb28d3597fecd78f8>:0 
12-15 17:35:23.391 W/monodroid( 8788):   at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x0000e] in <28e323a707a2414f8b493f6d4bb27c8d>:0 
12-15 17:35:23.391 W/monodroid( 8788):   at Android.Runtime.JNIEnv.CallStaticObjectMethod (System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00017] in <28e323a707a2414f8b493f6d4bb27c8d>:0 
12-15 17:35:23.391 W/monodroid( 8788):   at Android.Runtime.JNIEnv.FindClass (System.String classname) [0x0003d] in <28e323a707a2414f8b493f6d4bb27c8d>:0 
12-15 17:35:23.391 W/monodroid( 8788):   at Android.Runtime.JNIEnv.FindClass (System.Type type) [0x00015] in <28e323a707a2414f8b493f6d4bb27c8d>:0 
12-15 17:35:23.391 W/monodroid( 8788):   --- End of managed Java.Lang.ClassNotFoundException stack trace ---
12-15 17:35:23.391 W/monodroid( 8788): java.lang.ClassNotFoundException: md50039d44cbb3b194ba4f4e52eaa252795.BrowserPagerAdapter
12-15 17:35:23.391 W/monodroid( 8788): 	at java.lang.Class.classForName(Native Method)
12-15 17:35:23.391 W/monodroid( 8788): 	at java.lang.Class.forName(Class.java:309)
12-15 17:35:23.391 W/monodroid( 8788): 	at mono.java.lang.RunnableImplementor.n_run(Native Method)
12-15 17:35:23.391 W/monodroid( 8788): 	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
12-15 17:35:23.391 W/monodroid( 8788): 	at android.os.Handler.handleCallback(Handler.java:739)
12-15 17:35:23.401 W/monodroid( 8788): 	at android.os.Handler.dispatchMessage(Handler.java:95)
12-15 17:35:23.401 W/monodroid( 8788): 	at android.os.Looper.loop(Looper.java:135)
12-15 17:35:23.401 W/monodroid( 8788): 	at android.app.ActivityThread.main(ActivityThread.java:5254)
12-15 17:35:23.401 W/monodroid( 8788): 	at java.lang.reflect.Method.invoke(Native Method)
12-15 17:35:23.401 W/monodroid( 8788): 	at java.lang.reflect.Method.invoke(Method.java:372)
12-15 17:35:23.401 W/monodroid( 8788): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
12-15 17:35:23.401 W/monodroid( 8788): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
12-15 17:35:23.401 W/monodroid( 8788): Caused by: java.lang.ClassNotFoundException: Didn't find class "md50039d44cbb3b194ba4f4e52eaa252795.BrowserPagerAdapter" on path: DexPathList[[zip file "/data/app/com.outcoder.browser-1/base.apk"],nativeLibraryDirectories=[/data/app/com.outcoder.browser-1/lib/arm, /vendor/lib, /system/lib]]
12-15 17:35:23.401 W/monodroid( 8788): 	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-15 17:35:23.401 W/monodroid( 8788): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-15 17:35:23.401 W/monodroid( 8788): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
12-15 17:35:23.401 W/monodroid( 8788): 	... 12 more
12-15 17:35:23.401 W/monodroid( 8788): 	Suppressed: java.lang.ClassNotFoundException: md50039d44cbb3b194ba4f4e52eaa252795.BrowserPagerAdapter
12-15 17:35:23.401 W/monodroid( 8788): 		at java.lang.Class.classForName(Native Method)
12-15 17:35:23.401 W/monodroid( 8788): 		at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
12-15 17:35:23.401 W/monodroid( 8788): 		at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
12-15 17:35:23.401 W/monodroid( 8788): 		at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
12-15 17:35:23.401 W/monodroid( 8788): 		... 13 more
12-15 17:35:23.401 W/monodroid( 8788): 	Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
12-15 17:35:23.411 F/MainActivity( 8788): PerformFinalUITasks raised an exception.
12-15 17:35:23.411 F/MainActivity( 8788): java.lang.ClassNotFoundException: md50039d44cbb3b194ba4f4e52eaa252795.BrowserPagerAdapter
12-15 17:35:23.411 F/MainActivity( 8788): 	at java.lang.Class.classForName(Native Method)
12-15 17:35:23.411 F/MainActivity( 8788): 	at java.lang.Class.forName(Class.java:309)
12-15 17:35:23.411 F/MainActivity( 8788): 	at mono.java.lang.RunnableImplementor.n_run(Native Method)
12-15 17:35:23.411 F/MainActivity( 8788): 	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
12-15 17:35:23.411 F/MainActivity( 8788): 	at android.os.Handler.handleCallback(Handler.java:739)
12-15 17:35:23.411 F/MainActivity( 8788): 	at android.os.Handler.dispatchMessage(Handler.java:95)
12-15 17:35:23.411 F/MainActivity( 8788): 	at android.os.Looper.loop(Looper.java:135)
12-15 17:35:23.411 F/MainActivity( 8788): 	at android.app.ActivityThread.main(ActivityThread.java:5254)
12-15 17:35:23.411 F/MainActivity( 8788): 	at java.lang.reflect.Method.invoke(Native Method)
12-15 17:35:23.411 F/MainActivity( 8788): 	at java.lang.reflect.Method.invoke(Method.java:372)
12-15 17:35:23.411 F/MainActivity( 8788): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
12-15 17:35:23.411 F/MainActivity( 8788): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
12-15 17:35:23.411 F/MainActivity( 8788): Caused by: java.lang.ClassNotFoundException: Didn't find class "md50039d44cbb3b194ba4f4e52eaa252795.BrowserPagerAdapter" on path: DexPathList[[zip file "/data/app/com.outcoder.browser-1/base.apk"],nativeLibraryDirectories=[/data/app/com.outcoder.browser-1/lib/arm, /vendor/lib, /system/lib]]
12-15 17:35:23.411 F/MainActivity( 8788): 	at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-15 17:35:23.411 F/MainActivity( 8788): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-15 17:35:23.411 F/MainActivity( 8788): 	at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
12-15 17:35:23.411 F/MainActivity( 8788): 	... 12 more
12-15 17:35:23.411 F/MainActivity( 8788): 	Suppressed: java.lang.ClassNotFoundException: md50039d44cbb3b194ba4f4e52eaa252795.BrowserPagerAdapter
12-15 17:35:23.411 F/MainActivity( 8788): 		at java.lang.Class.classForName(Native Method)
12-15 17:35:23.411 F/MainActivity( 8788): 		at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
12-15 17:35:23.411 F/MainActivity( 8788): 		at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
12-15 17:35:23.411 F/MainActivity( 8788): 		at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
12-15 17:35:23.411 F/MainActivity( 8788): 		... 13 more
12-15 17:35:23.411 F/MainActivity( 8788): 	Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
12-15 17:35:23.471 E/RecyclerView( 8788): No adapter attached; skipping layout
Comment 17 Jon Douglas [MSFT] 2017-12-15 17:02:15 UTC
Hi All,

This is the first report we've seen regarding this issue with md5 hash ACWs not being included in the dex list without multidex enabled. At this time we are not sure this is a "regression" in 15.5 because we have not been successful with reproducing this reported issue yet with our QA process that builds and deploys very large samples such as the ones in this repository:

https://github.com/xamarin/monodroid-samples

We would need an isolated reproduction project and reproduction steps that demonstrates this behavior to investigate further.

Without a way to reproduce this and understand the problem, we will not be able to issue a fix. Once we can confirm this bug, we can look into a fix and ultimately include it in a service release or our next major release.

In the meantime, I will be monitoring this bug report and other mediums for more reports to come in, and also attempt to reproduce this locally. 

If anyone CC'd to this bug report has a project that demonstrates this (Please also keep your obj/bin folders when uploading), feel free to email it directly to me or attach to this bug report, that would be very helpful.

Thank you!
Comment 18 Daniel Vaughan 2017-12-15 19:49:50 UTC
Please don't dismiss this issue as being just not reproducible.
I can't repro this in a new project. My solution is large with multiple projects and many resources. Since there are two reports so far that this issue is occurring, I hope you'll investigate further. Until this is fixed, my business is dead in the water.
Comment 20 Daniel Vaughan 2017-12-16 23:21:36 UTC
Thanks Jon.

I was trying to come up with repro solution today and I noticed a couple things. Firstly, the issue pops up intermittently. Rebuilding sometimes fixes the problem for a rebuild cycle. Secondly, the only types that appear to be 'not found' are those deriving from Java.Lang.Object. I haven't seen this issue crop up for a class not deriving from Java.Lang.Object.
Comment 21 Daniel Vaughan 2017-12-17 17:39:53 UTC
This issue pops up for several classes. A common one is a custom ApplicationEx class that I have. Here are some details:

ClassNotFoundException is raised at run-time, complaining that the following class can't be found:
md53d8140f7ca89febe65217269e9d6fc3a.ApplicationEx

Opening the classes.dex file from the APK in a text editor, I see it contains two references to ApplicationEx:
3Lmd5c56c2ea5e9bb3b5f9c426a63927cb64b/ApplicationEx
and
3Lmd53d8140f7ca89febe65217269e9d6fc3a/ApplicationEx

Notice the different packages.

In obj\Debug\android\src there exists two ApplicationEx.java files in the directories:
md53d8140f7ca89febe65217269e9d6fc3a
and
md5c56c2ea5e9bb3b5f9c426a63927cb64b

It looks like the classes.dex file isn't being written correctly. I don't know how the classes.dex file works, and I could be wrong, but it looks like that in one place it's pointing to the wrong ApplicationEx class.

When I opened the APK in ClassyShark I noticed there isn't an entry under classes.dex for md53d8140f7ca89febe65217269e9d6fc3a
There is an entry for md5c56c2ea5e9bb3b5f9c426a63927cb64b.
Comment 22 vincent 2017-12-18 13:33:38 UTC
Hi, 
Also in a single dex configuration and we are experiencing the exact same issue. Started happening with 15.5.1, updated to 15.5.2 without any luck. Exception is intermittent.
Thx
Comment 23 Jon Douglas [MSFT] 2017-12-18 15:33:14 UTC
Another report:

https://forums.xamarin.com/discussion/113320/error-starting-an-activity#latest
Comment 25 Jon Douglas [MSFT] 2017-12-18 18:20:09 UTC
If anyone attached to this bug report has a reproduction project, please attach it to this report, or email it directly to me. I have attempted to reproduce this locally with the project already attached here and am not able to do so.

It looks like the md5 hash is being changed between classes. Is anyone by chance changing their Assembly or Namespace of their project? Or does anyone have incremental assembly version set such as the following in Properties/AssemblyInfo.cs?:

[assembly:AssemblyVersion("1.0.0.*")]

Also ensure that your custom classes have appropriate constructors such as:

        public App(System.IntPtr intPtr, Android.Runtime.JniHandleOwnership jniHandle) : base(intPtr, jniHandle)
        {
            
        }

https://developer.xamarin.com/guides/android/advanced_topics/java_integration_overview/working_with_jni/#Wrapping_with_Java.Lang.Object

https://developer.xamarin.com/guides/android/under_the_hood/architecture/#Java_Activation

Please let me know! Thanks!
Comment 26 Brent 2017-12-18 18:27:06 UTC
Yes, I am using an incremental assembly version.

Note: I was the OP of https://forums.xamarin.com/discussion/113320/error-starting-an-activity#latest
Comment 28 Jon Douglas [MSFT] 2017-12-18 18:47:26 UTC
(In reply to Jon Douglas [MSFT] from comment #25)
> If anyone attached to this bug report has a reproduction project, please
> attach it to this report, or email it directly to me. I have attempted to
> reproduce this locally with the project already attached here and am not
> able to do so.
> 
> It looks like the md5 hash is being changed between classes. Is anyone by
> chance changing their Assembly or Namespace of their project? Or does anyone
> have incremental assembly version set such as the following in
> Properties/AssemblyInfo.cs?:
> 
> [assembly:AssemblyVersion("1.0.0.*")]
> 
> Also ensure that your custom classes have appropriate constructors such as:
> 
>         public App(System.IntPtr intPtr, Android.Runtime.JniHandleOwnership
> jniHandle) : base(intPtr, jniHandle)
>         {
>             
>         }
> 
> https://developer.xamarin.com/guides/android/advanced_topics/
> java_integration_overview/working_with_jni/#Wrapping_with_Java.Lang.Object
> 
> https://developer.xamarin.com/guides/android/under_the_hood/architecture/
> #Java_Activation
> 
> Please let me know! Thanks!

To clarify on this point, the appropriate constructor applies to the "special" Application subclass as per the documentation.
Comment 29 vincent 2017-12-19 06:05:27 UTC
Yup, using incremental assembly version too. Unfortunately, I can't share the code.
Comment 30 Stijn Hoop 2017-12-19 14:25:34 UTC
Also using incremental assembly version. The problem goes away if I pin the version but that does not fit with our deployment requirements.
Comment 31 John Hind 2017-12-19 16:19:06 UTC
I have been desperately struggling with something similar myself (at least people on the forum suggest it is this issue). I am seeing:

"Java.Lang.RuntimeException: Unable to instantiate activity ComponentInfo ... Didn't find class "md59dfe07309ceba9bfe71a67493c5725be.MainActivity" on path: DexPathList[[zip file ..."

I have determined the following:

My code builds and runs fine in VS2015 Update 3 (a fresh install with no updates), Xamarin 4.2.1.62 (680125b).

If I let Xamarin update to 4.8.0.756, it builds fine but falls over with the runtime exception during start-up as described above. It may or may not be relevant but the build emits this disturbingly worded message which is neither an error nor a warning:

"No way to resolve conflict between "mscorlib, Version=4.0.0.0, ... and "mscorlib, Version=2.0.5.0 ... Choosing mscorlib, Version=4.0.0.0 ... arbitrarily."

In a fresh install of VS2017, the results are the same as in the updated VS2015.

I am willing to email my code base to a Xamarin employee if that will help. It has no third party packages or plug-ins, but possibly relevant is the inherited MainActivity:

public class JHMainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
...
[Activity (Label = "Harris Weaving", Icon = "@drawable/icon", Theme="@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : JHMainActivity
...
Comment 32 mailcannon 2017-12-21 07:27:50 UTC
Have some similar problems. Build and Rebuild succeeds every time but when debug starting i get "Didn't find class... MainActivity" for a number of times, sometimes didn't find some random classes. Rarely deleting bin and obj before rebuild is helping. Also obj folder is being used by another process every time so i need to close VS or even reboot.

Workaround I found: 
1) Rolling back to 15.4. Versions 15.5.1 and 15.5.2 are broken.
or 
2) Build android project with msbuild:

"...\MSBuild.exe" MyPrj.Droid.csproj /P:Configuration=Release,SolutionDir=C:\Sources /v:quiet /t:Rebuild,PackageForAndroid,SignAndroidPackage /nologo

building with msbuild is working on 15.5.2. not sure about 15.5.1.

dont have any repro sry
Comment 33 mailcannon 2017-12-21 07:28:39 UTC
also I dont use incremental assembly version.
Comment 34 Daniel Vaughan 2017-12-22 17:30:34 UTC
Based on your message, Jon, I switched from an incremental build version in all my projects, to a static version number. It didn't appear to make a difference.

I verified that the correct constructors are in place. I.e., protected Foo (System.IntPtr intPtr, Android.Runtime.JniHandleOwnership jniHandle) : base(intPtr, jniHandle)

I haven't been able to create a repro that is reliable. I do have a solution in which I can produce a ClassNotFoundException when I re-generate a bunch of randomly named classes (it's using a T4 template). But the exception goes away after a rebuild; unlike my 'main' solution in which the exception goes away only sometimes.

I'll attach the solution to the issue.
Please be sure to use Build/Regenerate T4 Templates before debugging. Regenerating the classes renames them. This seems to produce the issue until a rebuild is performed.
Comment 35 Daniel Vaughan 2017-12-22 17:40:31 UTC
Created attachment 26049 [details]
Repro using randomly named classes.

This solution produces a ClassNotFoundException at run-time.
Please be sure to select Build/Transform all T4 Templates in Visual Studio before beginning a debug session. The T4 templates in the solution generate a bunch of Java.Lang.Object derived classes and a bunch of non-Java.Lang.Object derived classes. All of which are assigned a random class name. The AssemblyTypeLoader class in the XamarinAndroidBuildIssue project raises the ClassNotFoundException in its GetAllTypes method when you Transform all templates and then hit F5. Subsequent debug session will raise the ClassNotFoundException until a rebuild is performed.

If an exception is not raised after following the instructions, please go through the steps again. I.e., select Build/Transform all T4 Templates in Visual Studio and then F5 (Debug).

BTW., the actual exception thrown is a TargetInvocationException. You need to drill down to find the ClassNotFoundException.
Comment 36 Jon Douglas [MSFT] 2017-12-22 17:53:35 UTC
(In reply to Daniel Vaughan from comment #35)
> Created attachment 26049 [details]
> Repro using randomly named classes.
> 
> This solution produces a ClassNotFoundException at run-time.
> Please be sure to select Build/Transform all T4 Templates in Visual Studio
> before beginning a debug session. The T4 templates in the solution generate
> a bunch of Java.Lang.Object derived classes and a bunch of
> non-Java.Lang.Object derived classes. All of which are assigned a random
> class name. The AssemblyTypeLoader class in the XamarinAndroidBuildIssue
> project raises the ClassNotFoundException in its GetAllTypes method when you
> Transform all templates and then hit F5. Subsequent debug session will raise
> the ClassNotFoundException until a rebuild is performed.
> 
> If an exception is not raised after following the instructions, please go
> through the steps again. I.e., select Build/Transform all T4 Templates in
> Visual Studio and then F5 (Debug).
> 
> BTW., the actual exception thrown is a TargetInvocationException. You need
> to drill down to find the ClassNotFoundException.

Hi Daniel,

Thank you very much for the reproduction and what a great use of T4 templating to create a reliable reproduction! Stay tuned as I investigate further with this reliable reproduction.
Comment 37 Roman 2017-12-23 05:06:01 UTC
Created attachment 26050 [details]
Small repro project

As suggested on StackOverflow (https://stackoverflow.com/q/47949434/33080), I'm attaching my small repro project.

The obj directory is important for this issue to reproduce: a clean build appears to permanently fix it. It's not just one buggy cached build though. Changing the code slightly and running the build will still reproduce the issue.

I ran into this issue while implementing GLSurfaceView.IRenderer. After some debugging I created a blank project using the Single-View App template and right away the issue was in it. That's the attached project with its build artifacts.

Once I was told it's a known bug, I tried to reproduce it again in a new blank project but couldn't. Maybe it was a coincidence that my first blank project got the bug, or maybe VS was in a broken state following my GLSurfaceView experiments that carried on into the new project? All later attempts were with a restarted VS, unlike the initial one.

P.S. in case it's relevant, I deleted some directories from obj to make it fit into the bugzilla attachment size limit, namely: obj/Debug/linksrc, obj/Debug/android/assets.
Comment 38 Jon Douglas [MSFT] 2017-12-23 19:09:16 UTC
We have a potential fix in the works:

https://github.com/xamarin/xamarin-android/pull/1142
Comment 39 dean.ellis 2018-01-04 09:39:55 UTC
Fixed in xamarin-android/master/008284a8
Comment 40 dean.ellis 2018-01-04 16:15:55 UTC
Created attachment 26064 [details]
Xamarin.Android.Build.Tasks.dll (Based on 15-5 Stable)

I am attaching a build of the 15-5 Xamarin.Android.Build.Tasks.dll which has the patch applied.

To use you will need to manually replace the existing Xamarin.Android.Build.Tasks.dll. Depending on what Visual Studio version is being used this might be in a number of places.

C:\Program Files (x86)\MSBuild\Xamarin\Android
C:\Program Files (x86)\Microsoft Visual Studio\2017\<version>\MSBuild\Xamarin\Android

On MacOS it will be in 

/Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/xbuild/Xamarin/Android/

Please take a backup of the existing file, just in case this does cause a problem.
Comment 41 Allan Pead 2018-01-04 18:50:26 UTC
Definitely working better, but some more testing is needed to confirm it fully works on our side.

Something we also need to investigate is an apk built in 15.4 yields multiple dex files (2), but in 15.5 with the attached patch yields 1 dex file using the same solution.  There may be a valid explanation, but has anybody else noticed this during testing?
Comment 42 Jon Douglas [MSFT] 2018-01-04 18:58:17 UTC
(In reply to Allan Pead from comment #41)
> Definitely working better, but some more testing is needed to confirm it
> fully works on our side.
> 
> Something we also need to investigate is an apk built in 15.4 yields
> multiple dex files (2), but in 15.5 with the attached patch yields 1 dex
> file using the same solution.  There may be a valid explanation, but has
> anybody else noticed this during testing?

That would seem to only be the case if you have multi-dex enabled in a multi-dex situation (> 64k methods). Without this being enabled, your project should error out with a build error regarding the dex count and a suggestion about enabling multidex.

It is a possibility that using proguard would cut your previous > 64k methods to a much lower count in which only one dex list would need to be used. At this point it's hard to speculate what's going on as we would need some investigation of your APKs. You can use "classyshark" to count the dexlists inside your APKs if need be:

https://www.jon-douglas.com/2017/03/15/apk-tools/

Feel free to email me the different APKs and I can take a look as well.
Comment 43 Allan Pead 2018-01-04 19:24:15 UTC
Both projects are identically set up (both multi-dex, neither proguard), so should in theory generate the same output, hence the question if anyone else has noticed the builds are different.   The multidex.keep files are created differently (counts), but have the same unique classes in them. Still busy investigating the actual dex differences.  Will discuss via email, it could have a valid reason, but is rather odd.
Comment 45 Allan Pead 2018-01-05 09:45:57 UTC
On further analysis I'd say this is actually working more correctly than 15.4.4 did in the comparisons above.

Tested and working on VS 2015 and VS 2017 on this side.
Comment 46 Daniel Vaughan 2018-01-07 15:51:49 UTC
I've tested the 15-5 Xamarin.Android.Build.Tasks.dll and I haven't yet received a ClassNotFoundException. So, it looks to be fixed. 
Thanks Jon and Dean for looking into and resolving this.

Note You need to log in before you can comment on or make changes to this bug.