Skip to content
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

App builds but fails to launch after VS update #4486

Closed
bolk opened this issue Mar 31, 2020 · 23 comments · Fixed by #4505
Closed

App builds but fails to launch after VS update #4486

bolk opened this issue Mar 31, 2020 · 23 comments · Fixed by #4505
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.

Comments

@bolk
Copy link

bolk commented Mar 31, 2020

As suggested by @brendanzagaeski in #4470 (comment) I open a new issue with diagnostic MSBuild output.

Steps to Reproduce

  1. Installed latest VS2019 stable Update on Mac (Xamarin.Android: 10.2.0.100)
  2. Multidex enabled, DX true
  3. Project build Successfully but Crashes on Launch

Expected Behavior

App to build and Launch

Actual Behavior

App Builds successfully
App Crashes on Launch:

am start -n "it.arcasgr.apparcafondi/crc64117ee1069bfbf50b.SplashActivity"
Starting: Intent { cmp=it.arcasgr.apparcafondi/crc64117ee1069bfbf50b.SplashActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } }

[AndroidRuntime] Caused by: java.lang.ClassNotFoundException: Didn't find class "crc64117ee1069bfbf50b.MainApplication" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/it.arcasgr.apparcafondi-2/lib/arm64, /data/app/it.arcasgr.apparcafondi-2/base.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.arm64_v8a.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.en.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.it.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.xxhdpi.apk!/lib/arm64-v8a, /syst03-31 09:25:05.904 V/EnterpriseBillingPolicyInternal( 3785): EBPInternal Constructor called: true
[Zygote] v2
[libpersona] KNOX_SDCARD checking this for 10579
[libpersona] KNOX_SDCARD not a persona
[Zygote] accessInfo : 0
[SELinux] SELinux selinux_android_compute_policy_index : Policy Index[2], Con:u:r:zygote:s0 SPD:SEPF_SECMOBILE_7.0_0006 RAM:SEPF_SECMOBILE_7.0_0009, [-1 -1 -9 -1 0 1]
[SELinux] SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=it.arcasgr.apparcafondi
[TimaKeyStoreProvider] TimaSignature is unavailable
[System] ClassLoader referenced unknown path:
[ResourceType] ResTable_typeSpec entry count inconsistent: given 88, previously 90
[ResourceType] ResTable_typeSpec entry count inconsistent: given 88, previously 90
[ResourceType] ResTable_typeSpec entry count inconsistent: given 160, previously 162
[ResourceType] ResTable_typeSpec entry count inconsistent: given 158, previously 508
[AndroidRuntime] Shutting down VM
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: it.arcasgr.apparcafondi, PID: 16467
[AndroidRuntime] java.lang.RuntimeException: Unable to instantiate application crc64117ee1069bfbf50b.MainApplication: java.lang.ClassNotFoundException: Didn't find class "crc64117ee1069bfbf50b.MainApplication" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/it.arcasgr.apparcafondi-2/lib/arm64, /data/app/it.arcasgr.apparcafondi-2/base.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.arm64_v8a.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.en.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.it.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.xxhdpi.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
[AndroidRuntime] at android.app.LoadedApk.makeApplication(LoadedApk.java:846)
[AndroidRuntime] at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5859)
[AndroidRuntime] at android.app.ActivityThread.-wrap3(ActivityThread.java)
[AndroidRuntime] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1703)
[AndroidRuntime] at android.os.Handler.dispatchMessage(Handler.java:102)
[AndroidRuntime] at android.os.Looper.loop(Looper.java:154)
[AndroidRuntime] at android.app.ActivityThread.main(ActivityThread.java:6692)
[AndroidRuntime] at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
[AndroidRuntime] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
[AndroidRuntime] Caused by: java.lang.ClassNotFoundException: Didn't find class "crc64117ee1069bfbf50b.MainApplication" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/it.arcasgr.apparcafondi-2/lib/arm64, /data/app/it.arcasgr.apparcafondi-2/base.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.arm64_v8a.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.en.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.it.apk!/lib/arm64-v8a, /data/app/it.arcasgr.apparcafondi-2/split_config.xxhdpi.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
[AndroidRuntime] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
[AndroidRuntime] at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
[AndroidRuntime] at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
[AndroidRuntime] at android.app.Instrumentation.newApplication(Instrumentation.java:1000)
[AndroidRuntime] at android.app.LoadedApk.makeApplication(LoadedApk.java:835)
[AndroidRuntime] ... 9 more

Version Information

=== Visual Studio Community 2019 for Mac ===

Version 8.5 (build 3183)
Installation UUID: 2a861f20-9c1b-4c9c-957e-47b77939201c
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 6.14.1.39 (d16-5 / 30e8706b4)

Package version: 608000123

=== Mono Framework MDK ===

Runtime:
Mono 6.8.0.123 (2019-10/1d0d939dc30) (64-bit)
Package version: 608000123

=== Roslyn (Language Service) ===

3.5.0-beta4-20125-04+1baa0b3063238ed752ad1f0368b1df6b6901373e

=== NuGet ===

Version: 5.4.0.6315

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/3.1.200/Sdks
SDK Versions:
3.1.200
3.1.102
3.1.101
3.0.100
2.2.402
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.8.0/lib/mono/msbuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
3.1.2
3.1.1
3.0.0
2.2.7
2.1.16
2.1.15
2.1.13

=== Xamarin.Profiler ===

Version: 1.6.13.11
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 10.2.0.100 (Visual Studio Community)
Commit: xamarin-android/d16-5/988c811
Android SDK: /Users/albert/Library/Android/sdk
Supported Android versions:
None installed

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 29.0.6
SDK Build Tools Version: 30.0.0 rc1

Build Information:
Mono: c0c5c78
Java.Interop: xamarin/java.interop@fc18c54
ProGuard: xamarin/proguard@905836d
SQLite: xamarin/sqlite@46204c4
Xamarin.Android Tools: xamarin/xamarin-android-tools@9f4ed4b

=== Microsoft Mobile OpenJDK ===

Java SDK: /Users/albert/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android SDK Manager ===

Version: 16.5.0.39
Hash: 6fb4c79
Branch: remotes/origin/d16-5
Build date: 2020-02-20 23:25:56 UTC

=== Android Device Manager ===

Version: 16.5.0.71
Hash: 49194e8
Branch: remotes/origin/d16-5
Build date: 2020-02-20 23:26:18 UTC

=== Apple Developer Tools ===

Xcode 11.3.1 (15715)
Build 11C504

=== Xamarin.Mac ===

Xamarin.Mac not installed. Can't find /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/Version.

=== Xamarin.iOS ===

Version: 13.14.1.39 (Visual Studio Community)
Hash: 30e8706b4
Branch: d16-5
Build date: 2020-02-20 16:41:48-0500

=== Xamarin Designer ===

Version: 16.5.0.471
Hash: 35aa4889d
Branch: remotes/origin/d16-5
Build date: 2020-02-25 00:52:08 UTC

=== Xamarin Inspector ===

Version: 1.4.3
Hash: db27525
Branch: 1.4-release
Build date: Mon, 09 Jul 2018 21:20:18 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 805003183
Git revision: 60e06e010baa677204535600661dda3fb4677c2f
Build date: 2020-03-20 06:18:03-04
Build branch: release-8.5
Xamarin extensions: 60e06e010baa677204535600661dda3fb4677c2f

=== Operating System ===

Mac OS X 10.15.3
Darwin 19.3.0 Darwin Kernel Version 19.3.0
Thu Jan 9 20:58:23 PST 2020
root:xnu-6153.81.5~1/RELEASE_X86_64 x86_64

Log File

Find the ZIP with the MSBuild diagnostic attached

Archive.zip

@bolk bolk added the Area: App Runtime Issues in `libmonodroid.so`. label Mar 31, 2020
@grendello grendello added Area: App+Library Build Issues when building Library projects or Application projects. and removed Area: App Runtime Issues in `libmonodroid.so`. labels Mar 31, 2020
@grendello grendello added this to the Under Consideration milestone Mar 31, 2020
@dellis1972
Copy link
Contributor

@bolk looking at the logs you seem to be trying to debug an App Bundle via AndroidPackageFormat=aab ?

I suspect there are some settings that are incompatible with the aab, if you reset it back to apk for Debugging that should work. You only really need to use aab for when you send your app to google.

@bolk
Copy link
Author

bolk commented Mar 31, 2020

I'm not trying to debug the app, but I'm trying to run the release version on the phone

@jonathanpeppers
Copy link
Member

@bolk if you uninstall the app & Rebuild, then try again, does it fix anything?

@bolk
Copy link
Author

bolk commented Mar 31, 2020

No, unfortunately it doesn't.

@dellis1972
Copy link
Contributor

@bolk does it work ok if you use a release build with AndroidPackageFormat=apk ?
I'm wondering if there is a bug in our bundle-tool structure.

@bolk
Copy link
Author

bolk commented Apr 1, 2020

Hi @dellis1972,
I had to downgrad to Xamarin.Android to 10.0.6.2 to release a test build for some colleagues.
I now upgraded back to 10.2.0.100.
I built with apk but the deploy to the simulator fails.
It actually fails both with apk and with aab.
I attach the diagnostic logs.

vs.zip

@dellis1972
Copy link
Contributor

@bolk

Thanks for the logs. Both the apk and abb seem to suffer from the same issue. There is no classes.dex file in the produced package.

[INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/it.arcasgr.apparcafondi-b85wWk8h1FE3K8v24Ifaww==: Package /data/app/it.arcasgr.apparcafondi-b85wWk8h1FE3K8v24Ifaww==/base.apk code is missing]
 Module 'base' has no dex files but the attribute 'hasCode' is not set to false in the AndroidManifest.xml.

@jonathanpeppers there seems to be a problem with the interaction of MultiDex and dx.
@bolk have you tried using the new R8 dex tool , rather than dx ? Also the logs are great but the deployment log looks a bit weird. Some of the targets which I would have expected to run are skipped because they are up to date.

Sorry to ask again but could you run the following from an MSBuild Command prompt and a clean project please?

msbuild Arca.Android.csproj /restore /t:SignAndroidPackage /bl 

This should produce a *.binlog file which will contain all the information we need. We need to figure out why the classes.dex file is not being included in the apk/abb. The enhanced fastdev mode is not enabled (I checked that), so this must be something new that we have not seen before.

Thanks in advance for your help with this.

@jonathanpeppers
Copy link
Member

I think this is a duplicate of: #4478 (comment)

Luckily, @brendanzagaeski figured out the steps to reproduce. I'll probably drop everything to look into this.

@bolk
Copy link
Author

bolk commented Apr 2, 2020

Hello @dellis1972

please find attached the zip file with the binlog.

I've tried the R8 but the build fails:

R8 : warning : Missing class: android.media.SubtitleTrack$RenderingWidget$OnChangedListener
R8 : warning : Missing class: android.media.SubtitleController$Anchor
R8 : error : Compilation can't be completed because some library classes are missing.

msbuild.binlog.zip

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Apr 2, 2020
Fixes: xamarin#4478
Fixes: xamarin#4486

We with the release of VS for Mac 8.5 going stable have been getting
some reports of:

    error ADB0010:  Deployment failed
    Mono.AndroidTools.InstallFailedException: Failure [INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/com.contoso.androidapp1-RTZZFPyLkRI7Bk7VDgGkDg==: Package /data/app/com.contoso.androidapp1-RTZZFPyLkRI7Bk7VDgGkDg==/base.apk code is missing]
        at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess (System.String output, System.String packageName) [0x00159] in <65710797f19f43cc9d8f9e05353b9615>:0
        at Mono.AndroidTools.AndroidDevice+<>c__DisplayClass95_0.<InstallPackage>b__0 (System.Threading.Tasks.Task`1[TResult] t) [0x0001c] in <65710797f19f43cc9d8f9e05353b9615>:0
        at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () [0x00024] in <f9d1b832704f410aa8ec771f4fe80552>:0
        at System.Threading.Tasks.Task.Execute () [0x00000] in <f9d1b832704f410aa8ec771f4fe80552>:0
        --- End of stack trace from previous location where exception was thrown ---
        at AndroidDeviceExtensions.PushAndInstallPackage (Mono.AndroidTools.AndroidDevice device, System.String apkFile, System.String packageName, System.Boolean reinstall, Mono.AndroidTools.Adb.AdbProgressReporter notifyProgress, System.Threading.CancellationToken token) [0x001d3] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
        at AndroidDeviceExtensions.PushAndInstallPackage (Mono.AndroidTools.AndroidDevice device, System.String apkFile, System.String packageName, System.Boolean reinstall, Mono.AndroidTools.Adb.AdbProgressReporter notifyProgress, System.Threading.CancellationToken token) [0x00402] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
        at Xamarin.AndroidTools.AndroidDeploySession.InstallPackage () [0x003be] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
        at Xamarin.AndroidTools.AndroidDeploySession.RunAsync (System.Threading.CancellationToken token) [0x003f8] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
        at Xamarin.AndroidTools.AndroidDeploySession.RunLoggedAsync (System.Threading.CancellationToken token) [0x00077] in <81f66a65e9434daf9d06602cf6ac0bf9>:0  [/Users/macuser/Projects/AndroidApp1/AndroidApp1/AndroidApp1.csproj]

The error means the `.apk` is missing `classes.dex`.

It seems to affect apps where:

  * `AndroidDexTool=dx`, this would need to be set explicitly or it
    would default to `d8`.
  * The issue appears to occur on macOS-only.

Brendan was able to reproduce the issue:

xamarin#4478 (comment)

Using the zip file, there is an `obj` directory sitting next to the
`.sln` file:

  * `obj`
  * `AndroidApp1`
  * `AndroidApp1/AndroidApp1.csproj`
  * `AndroidApp1/obj`
  * `AndroidApp1.sln`

Note that the actual `$(BaseIntermediateOutputPath)` would be
`AndroidApp1/obj`. Brendan just moved an `obj` directory next to the
`.sln` file while trying to reproduce the issue.

However, it appears the presence of this phantom `obj` triggers the
issue! You can rename it to `obj2` to *solve* the issue. It is a
directory with sub-directories, but no files.

If you look at the build log, the main difference is:

    CompileToDalvik
        Parameters
    -       ClassesOutputDirectory = obj/Release/android/bin/classes/
    +       ClassesOutputDirectory = obj/Release/android/bin/classes

The presence of the trailing `/` breaks this code:

    cmd.AppendSwitchIfNotNull ("--output ", Path.GetDirectoryName (ClassesOutputDirectory));

`classes.dex` is output into `obj/Release/android/bin/classes` instead
of `obj/Release/android/bin` and doesn't make it into the final
`.apk`.

So tracking back to what creates the input value of
`ClassesOutputDirectory`, it is the MSBuild property
`$(_AndroidIntermediateJavaClassDirectory)`, it is merely:

    <_AndroidIntermediateJavaClassDirectory>$(IntermediateOutputPath)android\bin\classes\</_AndroidIntermediateJavaClassDirectory>

But then comparing broken vs working logs:

    Broken:
        _InitialBaseIntermediateOutputPath = obj/
        BaseIntermediateOutputPath = obj/
        IntermediateOutputPath = obj/Release/
        _AndroidIntermediateJavaClassDirectory = obj/Release/android/bin/classes/
    Working:
        _InitialBaseIntermediateOutputPath = obj\
        BaseIntermediateOutputPath = obj\
        IntermediateOutputPath = obj\Release\
        _AndroidIntermediateJavaClassDirectory = obj\Release\android\bin\classes\

It appears the existence of this random `obj` directory influences
MSBuild's path combining behavior?

The trailing `\` works, but `/` does not, due to `TrimEnd`:

    <CompileToDalvik
        ...
        ClassesOutputDirectory="$(_AndroidIntermediateJavaClassDirectory.TrimEnd('\'))"

I have no explanation of to why this started occurring. My guess is
there is some change in the behavior of MSBuild running on Mono?

However, after reviewing `<CompileToDalvik/>`, I think we should just
remove the `Path.GetDirectoryName` call. We should just pass in the
directory name we want to use from MSBuild targets.

I could add a test for this scenario by setting a global property:

    _AndroidIntermediateJavaClassDirectory=obj/Debug/android/bin/classes/

Setting a global property could test the trailing `/` character
regardless of MSBuild behavior.

I also cleaned things up a bit by create a new
`$(_AndroidIntermediateDexOutputDirectory)` property.
@brendanzagaeski
Copy link
Member

I think this is a duplicate of: #4478 (comment)

Yup, I agree with that assessment. Adjusting the test case from #4478 to use the Android App Bundle packaging format leads to the same error seen in the log files for the Android App Bundle scenario from this item (#4486):

error : Module 'base' has no dex files but the attribute 'hasCode' is not set to false in the AndroidManifest.xml.

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Apr 3, 2020
Fixes: xamarin#4478
Fixes: xamarin#4486

We with the release of VS for Mac 8.5 going stable have been getting
some reports of:

    error ADB0010:  Deployment failed
    Mono.AndroidTools.InstallFailedException: Failure [INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/com.contoso.androidapp1-RTZZFPyLkRI7Bk7VDgGkDg==: Package /data/app/com.contoso.androidapp1-RTZZFPyLkRI7Bk7VDgGkDg==/base.apk code is missing]
        at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess (System.String output, System.String packageName) [0x00159] in <65710797f19f43cc9d8f9e05353b9615>:0
        at Mono.AndroidTools.AndroidDevice+<>c__DisplayClass95_0.<InstallPackage>b__0 (System.Threading.Tasks.Task`1[TResult] t) [0x0001c] in <65710797f19f43cc9d8f9e05353b9615>:0
        at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () [0x00024] in <f9d1b832704f410aa8ec771f4fe80552>:0
        at System.Threading.Tasks.Task.Execute () [0x00000] in <f9d1b832704f410aa8ec771f4fe80552>:0
        --- End of stack trace from previous location where exception was thrown ---
        at AndroidDeviceExtensions.PushAndInstallPackage (Mono.AndroidTools.AndroidDevice device, System.String apkFile, System.String packageName, System.Boolean reinstall, Mono.AndroidTools.Adb.AdbProgressReporter notifyProgress, System.Threading.CancellationToken token) [0x001d3] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
        at AndroidDeviceExtensions.PushAndInstallPackage (Mono.AndroidTools.AndroidDevice device, System.String apkFile, System.String packageName, System.Boolean reinstall, Mono.AndroidTools.Adb.AdbProgressReporter notifyProgress, System.Threading.CancellationToken token) [0x00402] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
        at Xamarin.AndroidTools.AndroidDeploySession.InstallPackage () [0x003be] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
        at Xamarin.AndroidTools.AndroidDeploySession.RunAsync (System.Threading.CancellationToken token) [0x003f8] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
        at Xamarin.AndroidTools.AndroidDeploySession.RunLoggedAsync (System.Threading.CancellationToken token) [0x00077] in <81f66a65e9434daf9d06602cf6ac0bf9>:0  [/Users/macuser/Projects/AndroidApp1/AndroidApp1/AndroidApp1.csproj]

The error means the `.apk` is missing `classes.dex`.

It seems to affect apps where:

  * `AndroidDexTool=dx`, this would need to be set explicitly or it
    would default to `d8`.
  * The issue appears to occur on macOS-only.

Brendan was able to reproduce the issue:

xamarin#4478 (comment)

Using the zip file, there is an `obj` directory sitting next to the
`.sln` file:

  * `obj`
  * `AndroidApp1`
  * `AndroidApp1/AndroidApp1.csproj`
  * `AndroidApp1/obj`
  * `AndroidApp1.sln`

Note that the actual `$(BaseIntermediateOutputPath)` would be
`AndroidApp1/obj`. Brendan just moved an `obj` directory next to the
`.sln` file while trying to reproduce the issue.

However, it appears the presence of this phantom `obj` triggers the
issue! You can rename it to `obj2` to *solve* the issue. It is a
directory with sub-directories, but no files.

If you look at the build log, the main difference is:

    CompileToDalvik
        Parameters
    -       ClassesOutputDirectory = obj/Release/android/bin/classes/
    +       ClassesOutputDirectory = obj/Release/android/bin/classes

The presence of the trailing `/` breaks this code:

    cmd.AppendSwitchIfNotNull ("--output ", Path.GetDirectoryName (ClassesOutputDirectory));

`classes.dex` is output into `obj/Release/android/bin/classes` instead
of `obj/Release/android/bin` and doesn't make it into the final
`.apk`.

So tracking back to what creates the input value of
`ClassesOutputDirectory`, it is the MSBuild property
`$(_AndroidIntermediateJavaClassDirectory)`, it is merely:

    <_AndroidIntermediateJavaClassDirectory>$(IntermediateOutputPath)android\bin\classes\</_AndroidIntermediateJavaClassDirectory>

But then comparing broken vs working logs:

    Broken:
        _InitialBaseIntermediateOutputPath = obj/
        BaseIntermediateOutputPath = obj/
        IntermediateOutputPath = obj/Release/
        _AndroidIntermediateJavaClassDirectory = obj/Release/android/bin/classes/
    Working:
        _InitialBaseIntermediateOutputPath = obj\
        BaseIntermediateOutputPath = obj\
        IntermediateOutputPath = obj\Release\
        _AndroidIntermediateJavaClassDirectory = obj\Release\android\bin\classes\

It appears the existence of this random `obj` directory influences
MSBuild's path combining behavior?

The trailing `\` works, but `/` does not, due to `TrimEnd`:

    <CompileToDalvik
        ...
        ClassesOutputDirectory="$(_AndroidIntermediateJavaClassDirectory.TrimEnd('\'))"

The breakage was introduced in 185c529, which added a trailing `\` to
the directory passed to `<CompileToDalvik/>`. We are unsure what is
causing the strange MSBuild behavior.

However, after reviewing `<CompileToDalvik/>`, I think we should just
remove the `Path.GetDirectoryName` call. We should just pass in the
directory name we want to use from MSBuild targets.

I could add a test for this scenario by setting a global property:

    _AndroidIntermediateJavaClassDirectory=obj/Debug/android/bin/classes/

Setting a global property could test the trailing `/` character
regardless of MSBuild behavior.

I also cleaned things up a bit by create a new
`$(_AndroidIntermediateDexOutputDirectory)` property.
jonpryor pushed a commit that referenced this issue Apr 6, 2020
…4505)

Fixes: #4478
Fixes: #4486

We with the release of VS for Mac 8.5 going stable have been getting
some reports of:

	error ADB0010:  Deployment failed
	Mono.AndroidTools.InstallFailedException: Failure [INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/com.contoso.androidapp1-RTZZFPyLkRI7Bk7VDgGkDg==: Package /data/app/com.contoso.androidapp1-RTZZFPyLkRI7Bk7VDgGkDg==/base.apk code is missing]
	    at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess (System.String output, System.String packageName) [0x00159] in <65710797f19f43cc9d8f9e05353b9615>:0
	    at Mono.AndroidTools.AndroidDevice+<>c__DisplayClass95_0.<InstallPackage>b__0 (System.Threading.Tasks.Task`1[TResult] t) [0x0001c] in <65710797f19f43cc9d8f9e05353b9615>:0
	    at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () [0x00024] in <f9d1b832704f410aa8ec771f4fe80552>:0
	    at System.Threading.Tasks.Task.Execute () [0x00000] in <f9d1b832704f410aa8ec771f4fe80552>:0
	    --- End of stack trace from previous location where exception was thrown ---
	    at AndroidDeviceExtensions.PushAndInstallPackage (Mono.AndroidTools.AndroidDevice device, System.String apkFile, System.String packageName, System.Boolean reinstall, Mono.AndroidTools.Adb.AdbProgressReporter notifyProgress, System.Threading.CancellationToken token) [0x001d3] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
	    at AndroidDeviceExtensions.PushAndInstallPackage (Mono.AndroidTools.AndroidDevice device, System.String apkFile, System.String packageName, System.Boolean reinstall, Mono.AndroidTools.Adb.AdbProgressReporter notifyProgress, System.Threading.CancellationToken token) [0x00402] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
	    at Xamarin.AndroidTools.AndroidDeploySession.InstallPackage () [0x003be] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
	    at Xamarin.AndroidTools.AndroidDeploySession.RunAsync (System.Threading.CancellationToken token) [0x003f8] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
	    at Xamarin.AndroidTools.AndroidDeploySession.RunLoggedAsync (System.Threading.CancellationToken token) [0x00077] in <81f66a65e9434daf9d06602cf6ac0bf9>:0  [/Users/macuser/Projects/AndroidApp1/AndroidApp1/AndroidApp1.csproj]

The error means the `.apk` is missing `classes.dex`.

It seems to affect apps where:

  * `$(AndroidDexTool)=dx`, this would need to be set explicitly or
    it would default to `d8`.
  * The issue appears to occur only on macOS.

@brendanzagaeski [was able to reproduce the issue][0]:

Using [`4478TestCase.zip`][1], there is an `obj` directory sitting
next to the `.sln` file:

  * `obj`
  * `AndroidApp1`
  * `AndroidApp1/AndroidApp1.csproj`
  * `AndroidApp1/obj`
  * `AndroidApp1.sln`

Note that the actual `$(BaseIntermediateOutputPath)` would be
`AndroidApp1/obj`.  Brendan just moved an `obj` directory next to the
`.sln` file while trying to reproduce the issue.

However, it appears the presence of this phantom `obj` triggers the
issue!  You can rename it to `obj2` to *solve* the issue.  It is a
directory with sub-directories, but no files.

If you look at the build log, the main difference is:

	CompileToDalvik
	    Parameters
	-       ClassesOutputDirectory = obj/Release/android/bin/classes/
	+       ClassesOutputDirectory = obj/Release/android/bin/classes

The presence of the trailing `/` breaks this code:

	cmd.AppendSwitchIfNotNull ("--output ", Path.GetDirectoryName (ClassesOutputDirectory));

`classes.dex` is output into `obj/Release/android/bin/classes` instead
of `obj/Release/android/bin` and doesn't make it into the final `.apk`.

Tracking back to what creates the input value of
`ClassesOutputDirectory`, it is the MSBuild property
`$(_AndroidIntermediateJavaClassDirectory)`, which is:

	<_AndroidIntermediateJavaClassDirectory>$(IntermediateOutputPath)android\bin\classes\</_AndroidIntermediateJavaClassDirectory>

But then comparing broken vs working logs:

	Broken:
	    _InitialBaseIntermediateOutputPath = obj/
	    BaseIntermediateOutputPath = obj/
	    IntermediateOutputPath = obj/Release/
	    _AndroidIntermediateJavaClassDirectory = obj/Release/android/bin/classes/
	Working:
	    _InitialBaseIntermediateOutputPath = obj\
	    BaseIntermediateOutputPath = obj\
	    IntermediateOutputPath = obj\Release\
	    _AndroidIntermediateJavaClassDirectory = obj\Release\android\bin\classes\

It appears the existence of this random `obj` directory influences
MSBuild's path combining behavior?

The trailing `\` works, but `/` does not, due to `TrimEnd`:

	<CompileToDalvik
	    ...
	    ClassesOutputDirectory="$(_AndroidIntermediateJavaClassDirectory.TrimEnd('\'))"

The breakage was introduced in 185c529, which added a trailing `\` to
the directory passed to `<CompileToDalvik/>`.  We are unsure what is
causing the strange MSBuild behavior.

However, after reviewing `<CompileToDalvik/>`, I think we should just
remove the `Path.GetDirectoryName()` call.  We should just pass in the
directory name we want to use from MSBuild targets.

I could add a test for this scenario by setting a global property:

	/p:_AndroidIntermediateJavaClassDirectory=obj/Debug/android/bin/classes/

Setting a global property could test the trailing `/` character
regardless of MSBuild behavior.

I also cleaned things up a bit by create a new
`$(_AndroidIntermediateDexOutputDirectory)` property.

[0]: #4478 (comment)
[1]: https://github.com/xamarin/xamarin-android/files/4419061/4478TestCase.zip
@jonathanpeppers
Copy link
Member

@bolk as a workaround can you try this at the bottom of your .csproj file?

  <PropertyGroup>
    <_AndroidIntermediateJavaClassDirectory>obj\$(Configuration)\android\bin\classes</_AndroidIntermediateJavaClassDirectory>
  </PropertyGroup>
</Project>

This should allow you to keep using dx. Let us know if that works, thanks.

@bolk
Copy link
Author

bolk commented Apr 7, 2020

Hello @jonathanpeppers,
I've added the definirion in the .csproj and set dx.
It now compiles and deploys it to the emulator, but it doesn't run.
It quits immediatly with:

am start -n "it.arcasgr.apparcafondi/crc64117ee1069bfbf50b.SplashActivity"
Starting: Intent { cmp=it.arcasgr.apparcafondi/crc64117ee1069bfbf50b.SplashActivity }

jonpryor pushed a commit that referenced this issue Apr 7, 2020
…4505)

Fixes: #4478
Fixes: #4486

We with the release of VS for Mac 8.5 going stable have been getting
some reports of:

	error ADB0010:  Deployment failed
	Mono.AndroidTools.InstallFailedException: Failure [INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/com.contoso.androidapp1-RTZZFPyLkRI7Bk7VDgGkDg==: Package /data/app/com.contoso.androidapp1-RTZZFPyLkRI7Bk7VDgGkDg==/base.apk code is missing]
	    at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess (System.String output, System.String packageName) [0x00159] in <65710797f19f43cc9d8f9e05353b9615>:0
	    at Mono.AndroidTools.AndroidDevice+<>c__DisplayClass95_0.<InstallPackage>b__0 (System.Threading.Tasks.Task`1[TResult] t) [0x0001c] in <65710797f19f43cc9d8f9e05353b9615>:0
	    at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () [0x00024] in <f9d1b832704f410aa8ec771f4fe80552>:0
	    at System.Threading.Tasks.Task.Execute () [0x00000] in <f9d1b832704f410aa8ec771f4fe80552>:0
	    --- End of stack trace from previous location where exception was thrown ---
	    at AndroidDeviceExtensions.PushAndInstallPackage (Mono.AndroidTools.AndroidDevice device, System.String apkFile, System.String packageName, System.Boolean reinstall, Mono.AndroidTools.Adb.AdbProgressReporter notifyProgress, System.Threading.CancellationToken token) [0x001d3] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
	    at AndroidDeviceExtensions.PushAndInstallPackage (Mono.AndroidTools.AndroidDevice device, System.String apkFile, System.String packageName, System.Boolean reinstall, Mono.AndroidTools.Adb.AdbProgressReporter notifyProgress, System.Threading.CancellationToken token) [0x00402] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
	    at Xamarin.AndroidTools.AndroidDeploySession.InstallPackage () [0x003be] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
	    at Xamarin.AndroidTools.AndroidDeploySession.RunAsync (System.Threading.CancellationToken token) [0x003f8] in <81f66a65e9434daf9d06602cf6ac0bf9>:0
	    at Xamarin.AndroidTools.AndroidDeploySession.RunLoggedAsync (System.Threading.CancellationToken token) [0x00077] in <81f66a65e9434daf9d06602cf6ac0bf9>:0  [/Users/macuser/Projects/AndroidApp1/AndroidApp1/AndroidApp1.csproj]

The error means the `.apk` is missing `classes.dex`.

It seems to affect apps where:

  * `$(AndroidDexTool)=dx`, this would need to be set explicitly or
    it would default to `d8`.
  * The issue appears to occur only on macOS.

@brendanzagaeski [was able to reproduce the issue][0]:

Using [`4478TestCase.zip`][1], there is an `obj` directory sitting
next to the `.sln` file:

  * `obj`
  * `AndroidApp1`
  * `AndroidApp1/AndroidApp1.csproj`
  * `AndroidApp1/obj`
  * `AndroidApp1.sln`

Note that the actual `$(BaseIntermediateOutputPath)` would be
`AndroidApp1/obj`.  Brendan just moved an `obj` directory next to the
`.sln` file while trying to reproduce the issue.

However, it appears the presence of this phantom `obj` triggers the
issue!  You can rename it to `obj2` to *solve* the issue.  It is a
directory with sub-directories, but no files.

If you look at the build log, the main difference is:

	CompileToDalvik
	    Parameters
	-       ClassesOutputDirectory = obj/Release/android/bin/classes/
	+       ClassesOutputDirectory = obj/Release/android/bin/classes

The presence of the trailing `/` breaks this code:

	cmd.AppendSwitchIfNotNull ("--output ", Path.GetDirectoryName (ClassesOutputDirectory));

`classes.dex` is output into `obj/Release/android/bin/classes` instead
of `obj/Release/android/bin` and doesn't make it into the final `.apk`.

Tracking back to what creates the input value of
`ClassesOutputDirectory`, it is the MSBuild property
`$(_AndroidIntermediateJavaClassDirectory)`, which is:

	<_AndroidIntermediateJavaClassDirectory>$(IntermediateOutputPath)android\bin\classes\</_AndroidIntermediateJavaClassDirectory>

But then comparing broken vs working logs:

	Broken:
	    _InitialBaseIntermediateOutputPath = obj/
	    BaseIntermediateOutputPath = obj/
	    IntermediateOutputPath = obj/Release/
	    _AndroidIntermediateJavaClassDirectory = obj/Release/android/bin/classes/
	Working:
	    _InitialBaseIntermediateOutputPath = obj\
	    BaseIntermediateOutputPath = obj\
	    IntermediateOutputPath = obj\Release\
	    _AndroidIntermediateJavaClassDirectory = obj\Release\android\bin\classes\

It appears the existence of this random `obj` directory influences
MSBuild's path combining behavior?

The trailing `\` works, but `/` does not, due to `TrimEnd`:

	<CompileToDalvik
	    ...
	    ClassesOutputDirectory="$(_AndroidIntermediateJavaClassDirectory.TrimEnd('\'))"

The breakage was introduced in 185c529, which added a trailing `\` to
the directory passed to `<CompileToDalvik/>`.  We are unsure what is
causing the strange MSBuild behavior.

However, after reviewing `<CompileToDalvik/>`, I think we should just
remove the `Path.GetDirectoryName()` call.  We should just pass in the
directory name we want to use from MSBuild targets.

I could add a test for this scenario by setting a global property:

	/p:_AndroidIntermediateJavaClassDirectory=obj/Debug/android/bin/classes/

Setting a global property could test the trailing `/` character
regardless of MSBuild behavior.

I also cleaned things up a bit by create a new
`$(_AndroidIntermediateDexOutputDirectory)` property.

[0]: #4478 (comment)
[1]: https://github.com/xamarin/xamarin-android/files/4419061/4478TestCase.zip
@brendanzagaeski
Copy link
Member

@bolk, to double-check, does your AndroidManifest.xml by chance still have the android:hasCode="false" attribute? If so, you'll need to remove that or set it to android:hasCode="true".

@bolk
Copy link
Author

bolk commented Apr 8, 2020

Hi @brendanzagaeski, I've checked and there's no android:hasCode attribute in the manifest.

@brendanzagaeski
Copy link
Member

Interesting. Is there any more output from the application when it exits after the am start -n and Starting: Intent lines?

Depending on when the app is exiting, the best way to get the full information might be to use adb logcat:

  1. Open Tools > SDK Command Prompt.

  2. Run the following command to clear the log on the device:

    adb logcat -c
    
  3. Repeat the app crash once more.

  4. Collect the device log by running a command similar to the following:

    adb logcat -d > "$HOME/Desktop/adblogcat.txt"
    
  5. Attach back the adblogcat.txt file from your desktop, or open it in a text editor and copy and paste just the relevant lines. You can optionally compress the file in a .zip archive before attaching it.

Thanks in advance!

@dellis1972
Copy link
Contributor

You might also want to check for the hasCode in the generated AndroidManifest.xml which should be in the Intermediate directory e.g obj/Debug/android/AndroidManifest.xml

@bolk
Copy link
Author

bolk commented Apr 8, 2020

Hi all,

I attach the archive of both the application output and the adblogcat.
I checked and even in the intermediate android manifest there's no hasCode attribute

Archive.zip

@brendanzagaeski
Copy link
Member

@bolk, thanks for the log files. I have opened a new issue to continue discussion about the new error that's appearing in those logs: #4570

@brendanzagaeski
Copy link
Member

Release status update

A Preview version of Xamarin.Android has now been published that includes the fix for this item (#4486). The fix is not yet included in a Release version. I will update this item again when a Release version is available that includes the fix.

Fix included in Xamarin.Android 10.3.0.74.

Fix included on macOS in Visual Studio 2019 for Mac version 8.6 Preview 2. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.

(The fix is also included on Windows in Visual Studio 2019 version 16.6 Preview 3.)

@brendanzagaeski
Copy link
Member

Release status update

A new Release version of Xamarin.Android has now been published that includes the fix for this item (#4486).

Fix included in Xamarin.Android 10.3.1.0.

Fix included on macOS in Visual Studio 2019 for Mac version 8.6. To get the new version that includes the fix, check for the latest updates on the Stable updater channel.

(The fix is also included on Windows in Visual Studio 2019 version 16.6.)

@bolk
Copy link
Author

bolk commented May 25, 2020

Hello @brendanzagaeski, with the new release things seems to have been fixed. Thank you

@michelinaFolino
Copy link

Hi guys

i have the same problem with the following versions, can you help me out?

=== Visual Studio Community 2019 for Mac (Preview) ===

Version 8.8 Preview (8.8 build 2811)
Installation UUID: aff68713-0e62-4384-a986-02b402d17a23
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

Package version: 612000093

=== Mono Framework MDK ===

Runtime:
Mono 6.12.0.93 (2020-02/620cf538206) (64-bit)
Package version: 612000093

=== Roslyn (Language Service) ===

3.8.0-4.20503.2+75d31ee941ce4dbbe5752f4ac2d5c0583f388a27

=== NuGet ===

Version: 5.8.0.6783

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/3.1.402/Sdks
SDK Versions:
3.1.402
3.1.301
3.1.300
2.1.810
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
3.1.8
3.1.5
3.1.4
2.1.22
2.1.19
2.1.18

=== .NET Core 2.1 SDK ===

SDK: 2.1.810

=== Xamarin.Profiler ===

Version: 1.6.15.68
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Apple Developer Tools ===

Xcode 12.0.1 (17220)
Build 12A7300

=== Xamarin.Mac ===

Version: 6.22.1.26 (Visual Studio Community)
Hash: 1dc78db1a
Branch: d16-8-xm
Build date: 2020-09-18 17:30:35-0400

=== Xamarin.iOS ===

Version: 14.4.0.9 (Visual Studio Community)
Hash: d85bf7be1
Branch: d16-8
Build date: 2020-09-23 11:06:08-0400

=== Xamarin Designer ===

Version: 16.8.0.491
Hash: 335b65ec3
Branch: remotes/origin/d16-8
Build date: 2020-09-25 22:22:16 UTC

=== Xamarin.Android ===

Version: 11.1.0.15 (Visual Studio Community)
Commit: xamarin-android/d16-8/5e36e6e
Android SDK: /Users/folinomi/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
None installed

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 30.0.2

Build Information:
Mono: be2226b
Java.Interop: xamarin/java.interop@79d9533
ProGuard: Guardsquare/proguard@ebe9000
SQLite: xamarin/sqlite@1a3276b
Xamarin.Android Tools: xamarin/xamarin-android-tools@2fb1cbc

=== Microsoft OpenJDK for Mobile ===

Java SDK: /Users/folinomi/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android SDK Manager ===

Version: 16.8.0.31
Hash: 309321e
Branch: remotes/origin/d16-8
Build date: 2020-09-24 23:02:14 UTC

=== Android Device Manager ===

Version: 16.8.0.45
Hash: fc0af5f
Branch: remotes/origin/d16-8
Build date: 2020-09-24 23:02:34 UTC

=== Build Information ===

Release ID: 808002811
Git revision: c2511e043f96618435996078ddfd3e0fb34aa645
Build date: 2020-10-08 13:30:34-04
Build branch: release-8.8
Xamarin extensions: c2511e043f96618435996078ddfd3e0fb34aa645

=== Operating System ===

Mac OS X 10.15.5
Darwin 19.5.0 Darwin Kernel Version 19.5.0
Tue May 26 20:41:44 PDT 2020
root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64

=== Enabled user installed extensions ===

NuGet Package Management Extensions 0.24

@brendanzagaeski
Copy link
Member

Thanks for submitting the new report #5226, michelinaFolino! For any other users who might come across this issue (4486) when searching for symptoms similar to what michelinaFolino describes, please see that new issue #5226 for the latest updates.

@xamarin xamarin locked as resolved and limited conversation to collaborators Jun 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants