Forum Xamarin.Forms

Unable to deploy and install: Failed to parse APK file

luigitniluigitni ITMember ✭✭

Hi,

I have successfully updated an app from Xamarin.Forms v2 to version 4.5.
Unfortunately I am unable to install the app on the Android emulator, receiving the following error:

ADB0010: Mono.AndroidTools.InstallFailedException: Unexpected install output: Error: Failed to parse APK file: /data/local/tmp/com.myapp.name-Signed.apk

Exception occurred while executing:
java.lang.IllegalArgumentException: Error: Failed to parse APK file: /data/local/tmp/com.myapp.name-Signed.apk
    at com.android.server.pm.PackageManagerShellCommand.setParamsSize(PackageManagerShellCommand.java:338)
    at com.android.server.pm.PackageManagerShellCommand.runInstall(PackageManagerShellCommand.java:906)
    at com.android.server.pm.PackageManagerShellCommand.onCommand(PackageManagerShellCommand.java:158)
    at android.os.ShellCommand.exec(ShellCommand.java:103)
    at com.android.server.pm.PackageManagerService.onShellCommand(PackageManagerService.java:21328)
    at android.os.Binder.shellCommand(Binder.java:634)
    at android.os.Binder.onTransact(Binder.java:532)
    at android.content.pm.IPackageManager$Stub.onTransact(IPackageManager.java:2821)
    at com.android.server.pm.PackageManagerService.onTransact(PackageManagerService.java:3856)
    at android.os.Binder.execTransact(Binder.java:731)
Caused by: android.content.pm.PackageParser$PackageParserException: Failed to parse /data/local/tmp/com.myapp.name-Signed.apk
    at android.content.pm.PackageParser.parseApkLiteInner(PackageParser.java:1590)
    at android.content.pm.PackageParser.parseApkLite(PackageParser.java:1575)
    at com.android.server.pm.PackageManagerShellCommand.setParamsSize(PackageManagerShellCommand.java:331)
    ... 9 more

   at Mono.AndroidTools.Internal.AdbOutputParsing.CheckInstallSuccess(String output, String packageName) in E:\A\_work\548\s\External\androidtools\Mono.AndroidTools\Internal\AdbOutputParsing.cs:line 345
   at Mono.AndroidTools.AndroidDevice.<>c__DisplayClass95_0.<InstallPackage>b__0(Task`1 t) in E:\A\_work\548\s\External\androidtools\Mono.AndroidTools\AndroidDevice.cs:line 753
   at System.Threading.Tasks.ContinuationTaskFromResultTask`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at AndroidDeviceExtensions.<PushAndInstallPackage>d__11.MoveNext() in E:\A\_work\548\s\External\androidtools\Xamarin.AndroidTools\Devices\AndroidDeviceExtensions.cs:line 187
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at AndroidDeviceExtensions.<PushAndInstallPackage>d__11.MoveNext() in E:\A\_work\548\s\External\androidtools\Xamarin.AndroidTools\Devices\AndroidDeviceExtensions.cs:line 203
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Xamarin.AndroidTools.AndroidDeploySession.<InstallPackage>d__112.MoveNext() in E:\A\_work\548\s\External\androidtools\Xamarin.AndroidTools\Sessions\AndroidDeploySession.cs:line 433
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Xamarin.AndroidTools.AndroidDeploySession.<RunAsync>d__106.MoveNext() in E:\A\_work\548\s\External\androidtools\Xamarin.AndroidTools\Sessions\AndroidDeploySession.cs:line 217
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Xamarin.AndroidTools.AndroidDeploySession.<RunLoggedAsync>d__104.MoveNext() in E:\A\_work\548\s\External\androidtools\Xamarin.AndroidTools\Sessions\AndroidDeploySession.cs:line 119  

I tried installing the apk using adb, but it fails again with the following message:

adb install .\com.myapp.name-Signed.apk
Performing Streamed Install
adb: failed to install .\com.myapp.name-Signed.apk: Failure [INSTALL_PARSE_FAILED_NOT_APK: Failed to parse /d
ata/app/vmdl581776687.tmp/base.apk]

I can run the iOS version, buit Android required much more package updates.

The app manifest is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myapp.name" android:versionCode="51" android:versionName="2.6" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="22" android:targetSdkVersion="29" />
    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application android:label="APPNAME" android:icon="@drawable/icon" android:theme="@style/MyTheme">
        <meta-data android:name="android.max_aspect" android:value="2.1" />
        <provider android:name="androidx.core.content.FileProvider" android:authorities="com.myapp.name" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
        </provider>
    </application>
</manifest>

I am building the app using Visual Studio 2019, compiled using Android 10 (api 29).
I tried millions time to clean the project and rebuild but the output stays the same.

The apk contains:

  • assemblies
  • assets
  • lib
  • META-INF
  • res
  • AndroidManifest.xml
  • classes.dex
  • NOTICE
  • resources.asrc

I can browse the apk file using 7zip but it fails to unzip the package. So does win default zip, which leads me to think the Xamarin generates a corrupted file for some reasons.

My signature fields in the app properties are empty, but I am building in debug mode, which as far as I remember from native development will use a default key.

I have been struggling with this since days now. Did anyone had the same issue or can point me in the right direction?

Thanks in advance!

Posts

  • luigitniluigitni ITMember ✭✭
    edited April 2020

    So it seems I have been able to track down the issue.

    When VS2019/Xamarin has done building the apk file it performs the following operations:

    Runs zipalign on the file that will be signed: D:\Development\Android\build-tools\29.0.0-rc1\zipalign.exe -p 4 "D:\repositories\myapp\Droid\obj\Debug\100\android\bin\com.myapp.name.apk" "bin\Debug\com.myapp.name-Signed.apk"
    Runs apksigner to sign the archive with the debug key: C:\Program Files\Android\Jdk\microsoft_dist_openjdk_1.8.0.25\bin\java.exe -jar D:\Development\Android\build-tools\29.0.0-rc1\lib\apksigner.jar sign --ks C:\Users\Luigi.android\debug.keystore --ks-pass pass:android --ks-key-alias androiddebugkey --key-pass pass:android --min-sdk-version 22 --max-sdk-version 29 --verbose D:\repositories\myapp\Droid\bin\Debug\com.myapp.name-Signed.apk
    Happily declares the com.myapp.name.apk as Signed
    If I do the same process by hand, I get the following exception:

    'C:\Program Files\Android\jdk\microsoft_dist_openjdk_1.8.0.25\bin\java.exe' -jar D:\Development\Android\build-tools\29.0.0-rc1\lib\apksigner.jar sign --ks C:\Users\Luigi.android\debug.keystore --ks-pass pass:android --ks-key-alias androiddebugkey --key-pass pass:android --min-sdk-version 22 --max-sdk-version 29 --verbose D:\repositories\myapp\Droid\bin\Debug\com.myapp.name-Signed.apk
    offset 32573283
    at com.android.apksig.ApkSigner.sign(ApkSigner.java:241)
    at com.android.apksig.ApkSigner.sign(ApkSigner.java:193)
    at com.android.apksigner.ApkSignerTool.main(ApkSignerTool.java:109)
    Caused by: com.android.apksig.zip.ZipFormatException: Not a Central Directory record. Signature: 0x7515cc6e
    at com.android.apksig.internal.zip.CentralDirectoryRecord.getRecord(CentralDirectoryRecord.java:143)
    at com.android.apksig.ApkSigner.parseZipCentralDirectory(ApkSigner.java:742)
    This seems to imply that:

    The signed file is malformed/corrupted
    The build system simply ignores the exception and keeps going
    If I run the apksigner on the com.myapp.name.apk (unsigned) I am able to install the executable on the emulator.

    Why is it failing when using the default toolchan? How would I fix it? I need to attach the debugger to the running app over the emulator, and I would prefer to having a working build system without requiring manual intervention.

Sign In or Register to comment.