Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Fix issue where app will not install (#…
Browse files Browse the repository at this point in the history
…7719)

Fixes: https://dev.azure.com/DevDiv/DevDiv/_workitems/edit/1398544

It is quite common for users to switch between Debug and Release
configurations in order to test the app.  However, if the `Release`
build is using a custom signing keystore you will generally see this
warning and error:

	warning MSB6006: "adb" exited with code 1.
	[BT : 1.8.1] error : Installation of the app failed.

This is not entirely helpful, since you often need to dig into the
diagnostic log to figure out what the actual issue is.

This warning is produced when we try to run
`adb uninstall -k @PACKAGE_NAME@`:

	adb uninstall -k com.xamarin.example
	The -k option uninstalls the application while retaining the data/cache.
	At the moment, there is no way to remove the remaining data.
	You will have to reinstall the application with the same signature, and fully uninstall it.
	If you truly wish to continue, execute 'adb shell cmd package uninstall -k'.

We are not entirely sure why the application gets into this state,
but once it does you have to completely uninstall it.  We currently
ignore this error but that then results in the following error:

	Error: INSTALL_FAILED_UPDATE_INCOMPATIBLE: Existing package com.xamarin.example signatures do not match newer version; ignoring!

This is because the apps used different signing keystores.  As a
result they are incompatible.  The only option once you get this issue
is to uninstall the app manually and try again.  However the error
messaging is not obvious so users generally have no idea what to do.

Fix a few things in this area. Introduce a new `<AndroidAdb/>` task
which is responsible for making the calls to `adb`.  We previously used
`<Exec/>`, which made it hard to make any customizations around error
messaging.  We will check the result of the `<AndroidAdb/>` task when
calling `adb uninstall -k @PACKAGE_NAME@` and if the output contains
`adb shell cmd package uninstall` then we will automatically call
`adb shell cmd package uninstall @PACKAGE_NAME@`.  This will completely
remove the app from the device and will allow the later `bundletool`
invocation to work.

We have also updated `<InstallApkSet/>` to look for error messages from
`bundletool` and report them, so users will get better information.
We now generate error messages like:

	[BT : 1.8.1] error : Installation of the app failed.
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: The APKs have been extracted in the directory: /var/folders/5p/10yqy2kx6r9dnmnxh_nt6s0r0000gn/T/1389125984700138671
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 01:54:24 E/SplitApkInstallerBase: Failed to commit install session 1426763565 with command cmd package install-commit 1426763565. Error: INSTALL_FAILED_UPDATE_INCOMPATIBLE: Existing package com.xamarin.toggledebugreleasewithsigning signatures do not match newer version; ignoring!
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: [BT:1.8.1] Error: Installation of the app failed.
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: com.android.tools.build.bundletool.model.exceptions.CommandExecutionException
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000:  Installation of the app failed.
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.model.exceptions.InternalExceptionBuilder.build(InternalExceptionBuilder.java:57)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.device.DdmlibDevice.installApks(DdmlibDevice.java:192)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.commands.InstallApksCommand.lambda$execute$2(InstallApksCommand.java:214)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.device.AdbRunner.run(AdbRunner.java:81)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.device.AdbRunner.run(AdbRunner.java:43)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.commands.InstallApksCommand.execute(InstallApksCommand.java:214)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.BundleToolMain.main(BundleToolMain.java:91)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.BundleToolMain.main(BundleToolMain.java:49)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: Caused by: com.android.ddmlib.InstallException: Failed to commit install session 1426763565 with command cmd package install-commit 1426763565. Error: INSTALL_FAILED_UPDATE_INCOMPATIBLE: Existing package com.xamarin.toggledebugreleasewithsigning signatures do not match newer version; ignoring!
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.ddmlib.SplitApkInstallerBase.installCommit(SplitApkInstallerBase.java:99)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.ddmlib.SplitApkInstaller.install(SplitApkInstaller.java:85)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.ddmlib.internal.DeviceImpl.installPackages(DeviceImpl.java:1166)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	at com.android.tools.build.bundletool.device.DdmlibDevice.installApks(DdmlibDevice.java:176)
	obj/Release/android/bin/com.xamarin.toggledebugreleasewithsigning.apks : java error BT0000: 	... 6 more

Unit Test are added.
  • Loading branch information
dellis1972 authored Jan 28, 2023
1 parent 35db527 commit 1d7092d
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 23 deletions.
57 changes: 57 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/AndroidAdb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.IO;
using System.Text;
using Xamarin.Android.Tools;

namespace Xamarin.Android.Tasks {

public class AndroidAdb : AndroidToolTask
{
public override string TaskPrefix => "AADB";

public string AdbTarget { get; set; }
public string Command { get; set; }
public string Arguments { get; set; }

public bool IgnoreErrors { get; set; } = false;

[Output]
public bool Result { get; set; } = true;

protected override string ToolName => OS.IsWindows ? "adb.exe" : "adb";

protected override string GenerateFullPathToTool ()
{
return Path.Combine (ToolPath, ToolExe);
}

//adb $(AdbTarget) uninstall -k &quot;$(_AndroidPackage)&quot;
//adb $(AdbTarget) uninstall $(_AndroidPackage)
//adb $(AdbTarget) install -r &quot;$(ApkFileSigned)&quot;
//adb $(AdbTarget) shell cmd package uninstall -k $(_AndroidPackage)
protected override string GenerateCommandLineCommands ()
{
var sb = new StringBuilder ();
if (!string.IsNullOrEmpty (AdbTarget))
sb.Append ($" {AdbTarget} ");
sb.AppendFormat ("{0} {1}", Command, Arguments);
return sb.ToString ();
}

protected override bool HandleTaskExecutionErrors ()
{
if (!Result)
return true;
return base.HandleTaskExecutionErrors ();
}

protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance)
{
if (singleLine.Contains ("adb shell cmd package uninstall"))
Result = false;
base.LogEventsFromTextOutput (singleLine, messageImportance);
}
}
}
28 changes: 28 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/InstallApkSet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Xamarin.Android.Tools;

namespace Xamarin.Android.Tasks
Expand All @@ -14,6 +17,8 @@ public class InstallApkSet : BundleToolAdbTask
{
public override string TaskPrefix => "IAS";

public override string DefaultErrorCode => "BT0000";

[Required]
public string ApkSet { get; set; }

Expand All @@ -37,5 +42,28 @@ internal override CommandLineBuilder GetCommandLineBuilder ()

return cmd;
}

const string InstallErrorRegExString = @"(?<exception>com.android.tools.build.bundletool.model.exceptions.CommandExecutionException):(?<error>.+)";
static readonly Regex installErrorRegEx = new Regex (InstallErrorRegExString, RegexOptions.Compiled);

protected override IEnumerable<Regex> GetCustomExpressions ()
{
yield return installErrorRegEx;
}

internal override bool ProcessOutput (string singleLine, AssemblyIdentityMap assemblyMap)
{
var match = installErrorRegEx.Match (singleLine);
if (match.Success) {
// error message
var error = match.Groups ["error"].Value;
var exception = match.Groups ["exception"].Value;
SetFileLineAndColumn (ApkSet, line: 1, column: 0);
AppendTextToErrorText (exception);
AppendTextToErrorText (error);
return LogFromException (exception, error);;
}
return base.ProcessOutput (singleLine, assemblyMap);
}
}
}
27 changes: 24 additions & 3 deletions src/Xamarin.Android.Build.Tasks/Tasks/JavaToolTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ protected override string GenerateFullPathToTool ()
return Path.Combine (ToolPath, ToolExe);
}

bool LogFromException (string exception, string error) {
protected bool LogFromException (string exception, string error) {
switch (exception) {
case "java.lang.OutOfMemoryError":
Log.LogCodedError ("XA5213", Properties.Resources.XA5213, ToolName, GenerateCommandLineCommands ());
Expand All @@ -138,7 +138,7 @@ bool LogFromException (string exception, string error) {
}
}

bool ProcessOutput (string singleLine, AssemblyIdentityMap assemblyMap)
internal virtual bool ProcessOutput (string singleLine, AssemblyIdentityMap assemblyMap)
{
var match = CodeErrorRegEx.Match (singleLine);
var exceptionMatch = ExceptionRegEx.Match (singleLine);
Expand Down Expand Up @@ -197,6 +197,23 @@ protected virtual void GetLineNumber (string match, out int line, out int column
column = 0;
}

protected virtual IEnumerable<Regex> GetCustomExpressions ()
{
return Enumerable.Empty<Regex> ();
}

protected void SetFileLineAndColumn (string file, int line = 0, int column = 0)
{
this.file = file;
this.line = line;
this.column = column;
}

protected void AppendTextToErrorText (string text)
{
errorText.AppendLine (text);
}

protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
{
errorLines.Add (singleLine);
Expand All @@ -207,7 +224,11 @@ protected override void LogEventsFromTextOutput (string singleLine, MessageImpor
}
var match = CodeErrorRegEx.Match (singleLine);
var exceptionMatch = ExceptionRegEx.Match (singleLine);
foundError = foundError || match.Success || exceptionMatch.Success;
var customMatch = false;
foreach (var customRegex in GetCustomExpressions ()) {
customMatch |= customRegex.Match (singleLine).Success;
}
foundError = foundError || match.Success || exceptionMatch.Success || customMatch;
}
}
}
Expand Down
52 changes: 32 additions & 20 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" InitialTargets="_WriteLockFile">
<UsingTask TaskName="Xamarin.Android.Tasks.RemoveUnknownFiles" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.AndroidAdb" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.AndroidComputeResPaths" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.AndroidSignPackage" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
<UsingTask TaskName="Xamarin.Android.Tasks.AndroidCreateDebugKey" AssemblyFile="Xamarin.Android.Build.Tasks.dll" />
Expand Down Expand Up @@ -2658,21 +2659,12 @@ because xbuild doesn't support framework reference assemblies.
<PropertyGroup>
<_DeployCommand>&quot;$(AdbToolPath)adb&quot; $(AdbTarget) install -r &quot;$(ApkFileSigned)&quot;</_DeployCommand>
</PropertyGroup>
<Exec
ContinueOnError="True"
Command="$(_DeployCommand)"
ConsoleToMSBuild="True">
<Output TaskParameter="ExitCode" PropertyName="_DeployExitCode" />
<Output TaskParameter="ConsoleOutput" ItemName="_DeployConsoleOutput" />
</Exec>
<ItemGroup>
<_AdbError Include="The command `$(_DeployCommand)` exited with code $(_DeployExitCode):" />
<_AdbError Include="@(_DeployConsoleOutput->' %(Identity)')" />
</ItemGroup>
<Error
Condition=" '$(_DeployExitCode)' != '0' "
Code="ADB0000"
Text="@(_AdbError, '%0a')"
<AndroidAdb
ToolExe="$(AdbToolExe)"
ToolPath="$(AdbToolPath)"
AdbTarget="$(AdbTarget)"
Command="install"
Arguments="-r &quot;$(ApkFileSigned)&quot;"
/>
</Target>

Expand Down Expand Up @@ -2704,11 +2696,25 @@ because xbuild doesn't support framework reference assemblies.
<PropertyGroup>
<_UninstallCommand>&quot;$(AdbToolPath)adb&quot; $(AdbTarget) uninstall -k &quot;$(_AndroidPackage)&quot;</_UninstallCommand>
</PropertyGroup>
<Exec
ContinueOnError="True"
Command="$(_UninstallCommand)"
ConsoleToMSBuild="True"
<AndroidAdb
Condition=" '$(EmbedAssembliesIntoApk)' == 'true' "
ContinueOnError="True"
ToolExe="$(AdbToolExe)"
ToolPath="$(AdbToolPath)"
AdbTarget="$(AdbTarget)"
Command="uninstall"
Arguments="-k $(_AndroidPackage)"
>
<Output TaskParameter="Result" PropertyName="_UninstallResult" />
</AndroidAdb>
<AndroidAdb
Condition=" '$(EmbedAssembliesIntoApk)' == 'true' And '$(_UninstallResult)' == 'false' "
ContinueOnError="True"
ToolExe="$(AdbToolExe)"
ToolPath="$(AdbToolPath)"
AdbTarget="$(AdbTarget)"
Command="shell cmd package uninstall"
Arguments="$(_AndroidPackage)"
/>
<InstallApkSet
ToolPath="$(JavaToolPath)"
Expand All @@ -2728,7 +2734,13 @@ because xbuild doesn't support framework reference assemblies.
</Target>

<Target Name="_Uninstall">
<Exec Command="&quot;$(AdbToolPath)adb&quot; $(AdbTarget) uninstall $(_AndroidPackage)" />
<AndroidAdb
ToolExe="$(AdbToolExe)"
ToolPath="$(AdbToolPath)"
AdbTarget="$(AdbTarget)"
Command="uninstall"
Arguments="$(_AndroidPackage)"
/>
</Target>

<Target Name="Uninstall"
Expand Down
45 changes: 45 additions & 0 deletions tests/MSBuildDeviceIntegration/Tests/InstallTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,51 @@ public void ToggleFastDev ()
}
}

[Test]
public void ToggleDebugReleaseWithSigning ([Values ("aab", "apk")] string packageFormat)
{
AssertCommercialBuild ();
AssertHasDevices ();

string path = Path.Combine ("temp", TestName.Replace ("\"", string.Empty));
byte [] data = ResourceData.GetKeystore ();
string storepassfile = Path.Combine (Root, path, "storepass.txt");
string keypassfile = Path.Combine (Root, path, "keypass.txt");
var password = "file:android";

var proj = new XamarinAndroidApplicationProject {
};
proj.SetProperty (proj.ReleaseProperties, "AndroidSigningStorePass", $"file:{storepassfile}");
proj.SetProperty (proj.ReleaseProperties, "AndroidSigningKeyPass", $"file:{keypassfile}");
proj.SetProperty (proj.ReleaseProperties, "AndroidKeyStore", "True");
proj.SetProperty (proj.ReleaseProperties, "AndroidSigningKeyStore", "test.keystore");
proj.SetProperty (proj.ReleaseProperties, "AndroidSigningKeyAlias", "mykey");
proj.SetAndroidSupportedAbis ("armeabi-v7a", "x86", "x86_64");
proj.SetProperty (proj.ReleaseProperties, "AndroidPackageFormat", packageFormat);
proj.SetProperty ("AndroidUseApkSigner", "true");
proj.OtherBuildItems.Add (new BuildItem (BuildActions.None, "test.keystore") {
BinaryContent = () => data
});
proj.OtherBuildItems.Add (new BuildItem (BuildActions.None, "storepass.txt") {
TextContent = () => password.Replace ("file:", string.Empty),
Encoding = Encoding.ASCII,
});
proj.OtherBuildItems.Add (new BuildItem (BuildActions.None, "keypass.txt") {
TextContent = () => password.Replace ("file:", string.Empty),
Encoding = Encoding.ASCII,
});

using (var builder = CreateApkBuilder (path)) {
Assert.IsTrue (builder.Install (proj), "Install should have succeeded.");
//Now toggle to Release
proj.IsRelease = true;
Assert.IsTrue (builder.Install (proj), "Second install should have succeeded.");
proj.IsRelease = false;
Assert.IsTrue (builder.Install (proj), "Third install should have succeeded.");
Assert.IsTrue (builder.Uninstall (proj), "unnstall should have succeeded.");
}
}

[Test]
public void LoggingPropsShouldCreateOverrideDirForRelease ()
{
Expand Down

0 comments on commit 1d7092d

Please sign in to comment.