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

error ADB0010: Mono.AndroidTools.InstallFailedException: Failure [INSTALL_FAILED_INVALID_APK: #4478

Closed
vravindranath opened this issue Mar 28, 2020 · 17 comments · Fixed by #4505
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects.

Comments

@vravindranath
Copy link

vravindranath commented Mar 28, 2020

Steps to Reproduce

  1. Updated the VS for mac Version 8.5 (build 3183)
  2. Try to install the debug build in phone.
  3. Fails with the following error.

Expected Behavior : Should successfully into the device.

Actual Behavior Unable to install the app into device. The install fails with the following errors

error ADB0010: Mono.AndroidTools.InstallFailedException: Failure [INSTALL_FAILED_INVALID_APK: com.test.teaseer.==/base.apk code is missing]

error ADB0010:   at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess (System.String output, System.String packageName) [0x00152] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Mono.AndroidTools/Internal/AdbOutputParsing.cs:341 
 error ADB0010:   at Mono.AndroidTools.AndroidDevice+<>c__DisplayClass95_0.<InstallPackage>b__0 (System.Threading.Tasks.Task`1[TResult] t) [0x00016] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Mono.AndroidTools/AndroidDevice.cs:753 
error ADB0010:   at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () [0x00024] in <f9d1b832704f410aa8ec771f4fe80552>:0 
error ADB0010:   at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-10/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319 
 error ADB0010: --- End of stack trace from previous location where exception was thrown ---
error ADB0010: 
 error ADB0010:   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) [0x00189] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Devices/AndroidDeviceExtensions.cs:187 
     error ADB0010:   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) [0x003df] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Devices/AndroidDeviceExtensions.cs:203 
    error ADB0010:   at Xamarin.AndroidTools.AndroidDeploySession.InstallPackage () [0x003be] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Sessions/AndroidDeploySession.cs:433 
     error ADB0010:   at Xamarin.AndroidTools.AndroidDeploySession.RunAsync (System.Threading.CancellationToken token) [0x003ae] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Sessions/AndroidDeploySession.cs:217 
error ADB0010:   at Xamarin.AndroidTools.AndroidDeploySession.RunLoggedAsync (System.Threading.CancellationToken token) [0x0002f] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Sessions/AndroidDeploySession.cs:119 
error ADB0010: Mono.AndroidTools.InstallFailedException: Failure [INSTALL_FAILED_INVALID_APK: Package couldn't be installed in /data/app/com.kvaerner.webuild.dev-vlUnvmZEIqEoxbCUR62wrA==: Package /data/app/com.kvaerner.webuild.dev-vlUnvmZEIqEoxbCUR62wrA==/base.apk code is missing]
    error ADB0010:   at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess (System.String output, System.String packageName) [0x00152] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Mono.AndroidTools/Internal/AdbOutputParsing.cs:341 
 error ADB0010:   at Mono.AndroidTools.AndroidDevice+<>c__DisplayClass95_0.<InstallPackage>b__0 (System.Threading.Tasks.Task`1[TResult] t) [0x00016] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Mono.AndroidTools/AndroidDevice.cs:753 
    error ADB0010:   at System.Threading.Tasks.ContinuationTaskFromResultTask`1[TAntecedentResult].InnerInvoke () [0x00024] in <f9d1b832704f410aa8ec771f4fe80552>:0 
    error ADB0010:   at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-10/external/bockbuild/builds/mono-x64/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319 
     error ADB0010: --- End of stack trace from previous location where exception was thrown ---
     error ADB0010: 
error ADB0010:   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) [0x00189] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Devices/AndroidDeviceExtensions.cs:187 
     error ADB0010:   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) [0x003df] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Devices/AndroidDeviceExtensions.cs:203 
     error ADB0010:   at Xamarin.AndroidTools.AndroidDeploySession.InstallPackage () [0x003be] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Sessions/AndroidDeploySession.cs:433 
 error ADB0010:   at Xamarin.AndroidTools.AndroidDeploySession.RunAsync (System.Threading.CancellationToken token) [0x003ae] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Sessions/AndroidDeploySession.cs:217 
    error ADB0010:   at Xamarin.AndroidTools.AndroidDeploySession.RunLoggedAsync (System.Threading.CancellationToken token) [0x0002f] in /Users/runner/runners/2.165.0/work/1/s/xamarin-android/external/monodroid/tools/msbuild/external/androidtools/Xamarin.AndroidTools/Sessions/AndroidDeploySession.cs:119 
  Done executing task "InstallPackageAssemblies" -- FAILED.

Version Information

=== Visual Studio Enterprise 2019 for Mac ===

Version 8.5 (build 3183)
Installation UUID: aa0315c4-d53f-442d-8e1e-95d85e467894
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.1.100
3.1.100-preview3-014645
3.0.100
2.1.701
2.1.504
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.1.0
3.1.0-preview3.19553.2
3.0.0
2.1.16
2.1.15
2.1.14
2.1.13
2.1.12
2.1.8

=== 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 Enterprise)
Commit: xamarin-android/d16-5/988c811
Android SDK: /Users/developer/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
8.1 (API level 27)

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

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

=== Microsoft Mobile OpenJDK ===

Java SDK: /Users/developer/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

=== 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

=== Xamarin Designer ===

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

=== Apple Developer Tools ===

Xcode 11.3.1 (15715)
Build 11C504

=== Xamarin.Mac ===

Version: 6.14.1.39 (Visual Studio Enterprise)
Hash: 30e8706b4
Branch: d16-5
Build date: 2020-02-20 16:41:47-0500

=== Xamarin.iOS ===

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

=== 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.14.6
Darwin 18.7.0 Darwin Kernel Version 18.7.0
Sat Oct 12 00:02:19 PDT 2019
root:xnu-4903.278.12~1/RELEASE_X86_64 x86_64

Can you please let me know what needs to be done to revert to previous version of xamarin android and VS for Mac. Where can we download these packages for Mac. Currently blocked due to the issue.

@ghost
Copy link

ghost commented Mar 30, 2020

I have the same exact problem after updating Xamarin on Mac. The problem does not exist in 10.1.3.7, so rolling back helps.

@jonathanpeppers
Copy link
Member

Can someone attach a diagnostic MSBuild log? It seems like it is complaining about classes.dex missing from the APK, but we need the full log to understand what happened.

Download URLs are on the README for each version.

@jonathanpeppers jonathanpeppers self-assigned this Mar 30, 2020
@jonathanpeppers jonathanpeppers added Area: App+Library Build Issues when building Library projects or Application projects. need-info Issues that need more information from the author. labels Mar 30, 2020
@jonathanpeppers jonathanpeppers added this to the Under Consideration milestone Mar 30, 2020
@brendanzagaeski
Copy link
Contributor

brendanzagaeski commented Apr 1, 2020

Can someone attach a diagnostic MSBuild log?

As a quick follow-up on this, in Visual Studio for Mac, this error will likely be part of the app deployment phase, so the full steps to collect diagnostic MSBuild output in this case are:

  1. Set Visual Studio > Preferences > Projects [section] > Build [subsection] > Log verbosity to Diagnostic.
  2. Clean and deploy the app project again.
  3. Open the View > Pads > Errors window. Click on the Build Output button to show the build output.
  4. Copy and paste all of the output from that window into a text file.
  5. Open the View > Pads > Deploying to Device window.
  6. Copy and paste all of the output from that deployment window into a text file too. (You can paste it at the end of the same text file from step 4 if you like.)
  7. Zip up and attach the text file(s).

Note that the diagnostic MSBuild output does contain environment variables, such as the macOS user name, so if you prefer to keep it private, a few options are:

  • Upload the project to a temporary private GitHub repository, and add brendanzagaeski or jonathanpeppers as a collaborator.
  • Or use Help > Report a Problem from within Visual Studio to create an item on Visual Studio Developer Community. Those allow private attachments. Then you can link to the Developer Community item here. After that I'll close the Developer Community item to point to this GitHub issue, and we can continue the discussion here.
  • Or send a one to one message to me on Gitter, for example using a /query @brendanzagaeski command from within the Xamarin.Android Gitter channel, and we can coordinate on another way to send the project non-publicly.

Thanks in advance!

@brendanzagaeski
Copy link
Contributor

brendanzagaeski commented Apr 2, 2020

I found that I was able to reproduce this problem with a new template project by enabling multidex and setting the DEX compiler to DX. I'll take a look through the output to see if I can find additional clues.

EDIT: The initial steps to reproduce and log file I attached in this comment weren't quite consistent. There's something trickier happening moving between the IDE and the command line. I'll post another comment with more accurate steps to reproduce and a new log file once I've got that sorted out.

@vravindranath
Copy link
Author

vravindranath commented Apr 2, 2020

Sorry my dev was getting broken due to the issue. I had to roll back.
I dont use the new version of Xamarin android now. But it has started happening once i upgraded.
The dex was using dx and there was no r8 used. Previously with the same setting it was working.
Happy to see that you were able to reproduce.

@brendanzagaeski
Copy link
Contributor

Steps to reproduce

  1. Download and extract the attached test case on macOS.
  2. msbuild -restore -p:Configuration=Release -t:Install

4478TestCase.zip

Expected behavior

The classes.dex file generated by the CompileToDalvik task should be output to obj/Release/android/bin/classes.dex.

To produce this expected behavior with the test case:

rm -r obj AndroidApp1/bin AndroidApp1/obj
msbuild -restore -p:Configuration=Release -t:Install

The obj directory at the top level of the test case started off as a full copy of the obj directory that I had moved there to save for later comparisons. But I was able to trim it down to just a small hierarchy, containing a few directories and no files.

It seems the presence of that directory for some reason changes the path separator behavior in MSBuild.

Actual behavior

The classes.dex file is output to obj/Release/android/bin/classes/classes.dex:

$ find AndroidApp1 -name 'classes.dex'
AndroidApp1/obj/Release/android/bin/classes/classes.dex

Log files

logs.zip

The problem seems to be caused by a difference in path separator characters.

For example, the first difference between the Expected and Actual diagnostic MSBuild output log files is:

-Property reassignment: $(MSBuildProjectExtensionsPath)="/Users/macuser/Projects/AndroidApp1/AndroidApp1/obj/" (previous value: "obj\") at /Library/Frameworks/Mono.framework/Versions/6.8.0/lib/mono/xbuild/Current/Microsoft.Common.props (56,5)
+Property reassignment: $(MSBuildProjectExtensionsPath)="/Users/macuser/Projects/AndroidApp1/AndroidApp1/obj/" (previous value: "obj/") at /Library/Frameworks/Mono.framework/Versions/6.8.0/lib/mono/xbuild/Current/Microsoft.Common.props (56,5)

And the important difference is:

 Using "CompileToDalvik" task from assembly "/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Build.Tasks.dll".
 Task "CompileToDalvik"
   Task Parameter:DxJarPath=/Users/macuser/Library/Developer/Xamarin/android-sdk-macosx/build-tools/28.0.3/lib/dx.jar
   Task Parameter:DxExtraArguments=--dex --no-strict
   Task Parameter:JavaToolPath=/Users/macuser/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25/bin
   Task Parameter:JavaMaximumHeapSize=1G
-  Task Parameter:ClassesOutputDirectory=obj/Release/android/bin/classes
+  Task Parameter:ClassesOutputDirectory=obj/Release/android/bin/classes/

A possible workaround

I was able to work around the problem by adjusting Xamarin.Android.Common.targets to account for the different path separator:

--- a/Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/xbuild/Xamarin/Android/Xamarin.Android.Common.targets
+++ b/Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/xbuild/Xamarin/Android/Xamarin.Android.Common.targets
@@ -2476,7 +2476,7 @@ because xbuild doesn't support framework reference assemblies.
     JavaToolPath="$(JavaToolPath)"
     JavaMaximumHeapSize="$(JavaMaximumHeapSize)"
     JavaOptions="$(JavaOptions)"
-    ClassesOutputDirectory="$(_AndroidIntermediateJavaClassDirectory.TrimEnd('\'))"
+    ClassesOutputDirectory="$(_AndroidIntermediateJavaClassDirectory.TrimEnd('\').TrimEnd('/'))"
     ClassesZip="$(_AndroidIntermediateClassesZip)"
     ToolPath="$(DxToolPath)"
     ToolExe="$(DxToolExe)"

(The D8 task avoids this picky dependence on the presence or absence of the path separator character, so switching to the D8 DEX compiler is also a workaround for the attached test case.)

Version info

Mono Framework MDK 6.8.0.123 (2019-10/1d0d939dc30)
MSBuild 16.5.0.16201

macOS 10.14.6
US English locale, US Pacific time zone

@brendanzagaeski brendanzagaeski removed the need-info Issues that need more information from the author. label Apr 2, 2020
@jonathanpeppers
Copy link
Member

@brendanzagaeski thanks for figuring this out. This is extremely weird.

I can reproduce the problem with your zip, but deleting the top obj directory fixes the problem. This makes no sense at all, because the actual obj directory is in AndroidApp1/obj

jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Apr 2, 2020
Fixes: dotnet#4478
Fixes: dotnet#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:

dotnet#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.
jonathanpeppers added a commit to jonathanpeppers/xamarin-android that referenced this issue Apr 3, 2020
Fixes: dotnet#4478
Fixes: dotnet#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:

dotnet#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

@vravindranath 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.

@gsgou
Copy link

gsgou commented Apr 6, 2020

Had the same issue, worked for me and allowed me to keep dx. Thanks a lot for the workaround!
#1461 (comment)

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
Contributor

Release status update

A Preview version of Xamarin.Android has now been published that includes the fix for this item. 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.)

@sihde
Copy link

sihde commented Apr 21, 2020

I can confirm the preview release on MacOS resolved this issue for me.

@brendanzagaeski
Copy link
Contributor

Release status update

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

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.

(Fix also included on Windows in Visual Studio 2019 version 16.6.)

@SaravanaPandian0905
Copy link

I have been facing this same issue over 2 months on Windows. I couldn`t able to downgrade Xamarin Android version to 10.3.1.0 as it installs latest version (11.0.2.0) when running VS setup on windows. Kindly help.

VS 2019 Version - 16.7.7

@jonathanpeppers
Copy link
Member

@SaravanaPandian0905 can you file a new issue with more details?

You can check if your .apk file is missing classes.dex, and if it is there, it is likely a different problem.

@SaravanaPandian0905
Copy link

SaravanaPandian0905 commented Nov 2, 2020

@jonathanpeppers , Have created a new issue with more details. Please have a look at this once.
#5255

You can check if your .apk file is missing classes.dex - Thanks for your suggestion. By extracting the APK file, shall I check apk file is missing classes.dex ?

@jonathanpeppers
Copy link
Member

You can use any .zip utility to look at the .apk file. All Java/Kotlin code is in a single classes.dex file that is expected to be in the root of the archive.

This issue is long fixed, so your problem is likely something completely different.

@SaravanaPandian0905
Copy link

@jonathanpeppers , Thanks. I could able to see the classes.dex file after extracting .apk. could you please look at the new issue which I created ? and have a look at the screenshot once and share me a solution/suggestion if any because I have been facing this same Mono AndroidTools exception issue for so long till now.

@ghost ghost 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