Bug 55050

Summary: Improved Fast Deployment doesn't work with Application subclasses
Product: Android Reporter: Jonathan Pryor <jonp>
Component: MSBuildAssignee: dean.ellis
Status: Tracked in Related Links ---    
Severity: normal CC: alex, brendan.zagaeski, brubo2, cody.beyer, dev, jimmy.garrido, mixal11, mono-bugs+monodroid, philipg
Priority: ---    
Version: 7.3 (15.2)   
Target Milestone: 15.6   
Hardware: PC   
OS: Mac OS   
Related Links: https://github.com/xamarin/xamarin-android/pull/609
Tags: bb Is this bug a regression?: ---
Last known good build:
Attachments: Scratch.Bxc55050.zip

Description Jonathan Pryor 2017-04-11 20:28:21 UTC
Setting the `$(AndroidFastDeploymentType)` MSBuild property to `Assemblies:Dexes` enables enhanced fast deployment, which reduces the circumstances in which a .apk needs to be rebuilt:

https://developer.xamarin.com/releases/android/xamarin.android_7/xamarin.android_7.2/#Fast_Deployment

If the project contains an `Android.App.Application` subclass, the application will fail to start.
Comment 1 Jonathan Pryor 2017-04-11 20:34:01 UTC
Created attachment 21444 [details]
Scratch.Bxc55050.zip
Comment 2 Jonathan Pryor 2017-04-11 20:46:37 UTC
To reproduce:

> $ curl -o Scratch.Bxc55050.zip  https://bugzilla.xamarin.com/attachment.cgi?id=21444
> $ unzip Scratch.Bxc55050.zip
> $ cd Scratch.Bxc55050/Scratch.Bxc55050
> # Scratch.Bxc55050.csproj already sets $(AndroidFastDeploymentType)
> $ xbuild /t:Install
> $ xbuild /t:_Run

The app runs! No error is generated.

Next, reinstall the app so that it has an Application subclass:

> $ rm -Rf bin obj
> $ xbuild /t:Install /p:DefineConstants=INCLUDE_APP
> $ xbuild /t:_Run

When the `Scratch.Bxc55050.MyApp` type is included in the build -- by #defining INCLUDE_APP -- then the app crashes on startup:

> I ActivityManager: Start proc 22425:scratch.bxc55050/u0a226 for activity scratch.bxc55050/md5f8388200ff3b3c32e4e5b0e6a95ff75f.MainActivity
> I art     : Rejecting re-init on previously-failed class java.lang.Class<md5f8388200ff3b3c32e4e5b0e6a95ff75f.MyApp>: java.lang.NoClassDefFoundError: Failed resolution of: Lmono/android/IGCUserPeer;
> ...
> I art     : Caused by: java.lang.ClassNotFoundException: Didn't find class "mono.android.IGCUserPeer" on path: DexPathList[[zip file "/data/app/scratch.bxc55050-1/base.apk"],nativeLibraryDirectories=[/data/app/scratch.bxc55050-1/lib/arm64, /system/fake-libs64, /data/app/scratch.bxc55050-1/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
> ...
> D AndroidRuntime: Shutting down VM
> E AndroidRuntime: FATAL EXCEPTION: main
> E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate application md5f8388200ff3b3c32e4e5b0e6a95ff75f.MyApp: java.lang.ClassNotFoundException: Didn't find class "md5f8388200ff3b3c32e4e5b0e6a95ff75f.MyApp" on path: DexPathList[[zip file "/data/app/scratch.bxc55050-1/base.apk"],nativeLibraryDirectories=[/data/app/scratch.bxc55050-1/lib/arm64, /system/fake-libs64, /data/app/scratch.bxc55050-1/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
> ...
> E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "md5f8388200ff3b3c32e4e5b0e6a95ff75f.MyApp" on path: DexPathList[[zip file "/data/app/scratch.bxc55050-1/base.apk"],nativeLibraryDirectories=[/data/app/scratch.bxc55050-1/lib/arm64, /system/fake-libs64, /data/app/scratch.bxc55050-1/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
> ...
> E AndroidRuntime:        Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Lmono/android/IGCUserPeer;
> ...
> E AndroidRuntime:        Caused by: java.lang.ClassNotFoundException: Didn't find class "mono.android.IGCUserPeer" on path: DexPathList[[zip file "/data/app/scratch.bxc55050-1/base.apk"],nativeLibraryDirectories=[/data/app/scratch.bxc55050-1/lib/arm64, /system/fake-libs64, /data/app/scratch.bxc55050-1/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
> ...

Not setting `$(AndroidFastDeploymentType)` to `Assemblies:Dexes` allows the app to launch when `INCLUDE_APP` is #defined.

# Analysis:

The problem appears to be that when a C# `Application` subclass is used -- when `INCLUDE_APP` is #defined -- then the resulting `md5f8388200ff3b3c32e4e5b0e6a95ff75f/MyApp.java` Java Callable Wrapper references the `mono.android.IGCUserPeer ` type:

> public class MyApp
> 	extends mono.android.incrementaldeployment.StubApplication
> 	implements
> 		mono.android.IGCUserPeer
> {
> ...

The `IGCUserPeer` type is defined in `mono.android.dex`, which isn't loaded or available at the time that `MyApp` is loaded, resulting in the `java.lang.NoClassDefFoundError`.

This isn't a problem with `INCLUDE_APP` isn't defined, as the bundled `StubApplication` type *doesn't* implement from `IGCuserPeer`. `MainActivity` *does* implement `IGCUserPeer`, but by the time we're trying to load `MainActivity`, `StubApplication` has finished its initialization and will load `IGCUserPeer` from `mono.android.dex`.
Comment 3 Jonathan Pryor 2017-04-11 20:48:06 UTC
Plausible fix? Special-case `mono.android.dex` and *always* include it into `classes.dex`. Only side-load *other* `.dex` files.

Alternate plausible fix: don't make `classes.dex` contain the app's Java byte code, but instead have it include `mono.android.dex` + `StubApplication`/etc. Then side-load the application-containing .dex file.
Comment 4 Jimmy [MSFT] 2017-06-28 15:51:56 UTC
I am confirming this report as I was still able to reproduce this crash with the current version of Xamarin.Android.

### VS Version Info

Microsoft Visual Studio Enterprise 2017 
Version 15.2 (26430.14) Release
VisualStudio.15.Release/15.2.0+26430.14
Microsoft .NET Framework
Version 4.7.02046

Installed Version: Enterprise

Visual Basic 2017   00369-60000-00001-AA795
Microsoft Visual Basic 2017

Visual C# 2017   00369-60000-00001-AA795
Microsoft Visual C# 2017

Visual C++ 2017   00369-60000-00001-AA795
Microsoft Visual C++ 2017

Visual F# 4.1   00369-60000-00001-AA795
Microsoft Visual F# 4.1

Application Insights Tools for Visual Studio Package   8.6.00404.2
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017   15.0.30503.0
ASP.NET and Web Tools 2017

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

Azure App Service Tools v3.0.0   15.0.30209.0
Azure App Service Tools v3.0.0

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

JavaScript Language Service   2.0
JavaScript Language Service

JavaScript Project System   2.0
JavaScript Project System

JavaScript UWP Project System   2.0
JavaScript UWP Project System

KofePackagePackage Extension   1.0
KofePackagePackage Visual Studio Extension Detailed Info

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.50131.1

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

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   Mono.Debugging.VisualStudio
Support for debugging Mono processes with Visual Studio.

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

SQL Server Data Tools   15.1.61702.140
Microsoft SQL Server Data Tools

TypeScript   2.2.2.0
TypeScript tools for Visual Studio

Visual Studio Tools for Universal Windows Apps   15.0.26430.06
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin   4.5.0.486 (fec6f88)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK   7.3.1.2 (9dbc4c5)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   10.10.0.37 (ad35de4)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Comment 5 dean.ellis 2017-06-30 10:09:50 UTC
This should be resolved with https://github.com/xamarin/xamarin-android/pull/609 once it gets merged
Comment 6 Al Clark [MSFT] 2017-08-24 01:19:41 UTC
*** Bug 51480 has been marked as a duplicate of this bug. ***
Comment 7 alex 2017-09-19 07:21:30 UTC
I see that the pull request is still not merged. So sad.
Comment 8 Jonathan Pryor 2017-11-27 15:26:22 UTC
*** Bug 55704 has been marked as a duplicate of this bug. ***
Comment 9 Jon Douglas [MSFT] 2017-12-11 22:45:31 UTC
*** Bug 61073 has been marked as a duplicate of this bug. ***