Unable to open 'android-release-unsigned.apk' as zip archive (zipalign error)
Asked Answered
S

6

10

I am using Ionic to build a hybrid app. I have recently tried to build the Ionic app into an Android app but I get the following error when running ionic run android with the device connected:

Running command: /Users/USERNAME/Sites/PROJECT/egenius-client-application/hooks/after_prepare/010_add_platform_class.js /Users/USERNAME/Sites/PROJECT/egenius-client-    application
add to body class: platform-android
ANDROID_HOME=/Users/USERNAME/Library/Developer/android-sdk-macosx
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home
No target specified, deploying to device 'F7NPFP131245'.
:preBuild UP-TO-DATE
:preDebugBuild UP-TO-DATE
:checkDebugManifest
:CordovaLib:preBuild UP-TO-DATE
:CordovaLib:preDebugBuild UP-TO-DATE
:CordovaLib:compileDebugNdk UP-TO-DATE
:CordovaLib:compileLint
:CordovaLib:copyDebugLint UP-TO-DATE
:CordovaLib:mergeDebugProguardFiles
:CordovaLib:packageDebugRenderscript UP-TO-DATE
:CordovaLib:checkDebugManifest
:CordovaLib:prepareDebugDependencies
:CordovaLib:compileDebugRenderscript
:CordovaLib:generateDebugResValues
:CordovaLib:generateDebugResources
:CordovaLib:packageDebugResources
:CordovaLib:compileDebugAidl
:CordovaLib:generateDebugBuildConfig
:CordovaLib:generateDebugAssets UP-TO-DATE
:CordovaLib:mergeDebugAssets
:CordovaLib:processDebugManifest
:CordovaLib:processDebugResources
:CordovaLib:generateDebugSources
:CordovaLib:compileDebugJavaWithJavacNote: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

:CordovaLib:processDebugJavaRes UP-TO-DATE
:CordovaLib:transformResourcesWithMergeJavaResForDebug
:CordovaLib:transformClassesAndResourcesWithSyncLibJarsForDebug
:CordovaLib:mergeDebugJniLibFolders
:CordovaLib:transformNative_libsWithMergeJniLibsForDebug
:CordovaLib:transformNative_libsWithSyncJniLibsForDebug
:CordovaLib:bundleDebug
:prepareAndroidCordovaLibUnspecifiedDebugLibrary
:prepareDebugDependencies
:compileDebugAidl
:compileDebugRenderscript
:generateDebugBuildConfig
:generateDebugAssets UP-TO-DATE
:mergeDebugAssets
:generateDebugResValues
:generateDebugResources
:mergeDebugResources
:processDebugManifest
:processDebugResourceszs
:generateDebugSources
:compileDebugJavaWithJavacNote: /Users/alexandermcintosh/Sites/eGenius/egenius-client-application/platforms/android/src/org/apache/cordova/splashscreen/SplashScreen.    java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

:compileDebugNdk UP-TO-DATE
:compileDebugSources
:transformClassesWithDexForDebug
:mergeDebugJniLibFolders
:transformNative_libsWithMergeJniLibsForDebug
:processDebugJavaRes UP-TO-DATE
:transformResourcesWithMergeJavaResForDebug
:validateDebugSigning
:packageDebug
:zipalignDebugUnable to open '/Users/USERNAME/Sites/PROJECT/egenius-client-application/platforms/android/build/outputs/apk/android-debug-unaligned.apk' as zip archive
 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':zipalignDebug'.
> Process 'command '/Users/USERNAME/Library/Developer/android-sdk-macosx/build-tools/23.0.2/zipalign'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2 mins 35.63 secs
ERROR running one or more of the platforms: Error code 1 for command: /Users/USERNAME/Sites/PROJECT/egenius-client-application/platforms/android/gradlew with args:     cdvBuildDebug,-b,/Users/USERNAME/Sites/PROJECT/egenius-client-application/platforms/android/build.gradle,-PcdvBuildArch=x86,-Dorg.gradle.daemon=true,-Pandroid.    useDeprecatedNdk=true
You may not have the required environment or OS to run this project

It appears that I can build fine, running ionic build android --release manages to work without a problem, it's when I have to use the ZipAlign tool.

Running the tool manually like this: ~/Library/Developer/android-sdk-macosx/build-tools/23.0.2/zipalign -v 4 android-release-unsigned.apk egenius-app.apk tells me: Unable to open 'android-release-unsigned.apk' as zip archive.

I have tried downgrading my SDK to different versions, uninstalling and re-installing plugins and I'm not stuck, looking online doesn't provide a lot of help.

Does anyone know what I am missing?

Thanks, Alex.

UPDATE The moment I removed the dependancy Angular Material Design Icons the app built and signed successfully.

Subservience answered 10/2, 2016 at 10:56 Comment(0)
S
9

I had the error when the unsigned apk is not on the same directory with the zipalign file.

I fixed that one by putting the file path of the unsigned apk

./zipalign -v 4 /Users/mj/Projects/Ionic/myApp/platforms/android/app/build/outputs/apk/release/app-release-unsigned.apk my-signed-release.apk
Sonority answered 30/4, 2019 at 10:3 Comment(0)
N
2

try

ionic cordova plugin rm cordova-plugin-console 

then rerun the build, keytool, and jarsigner

Niemeyer answered 7/8, 2017 at 13:50 Comment(0)
C
2

I had the same problem and I tried using the following command

zipalign -v 4 /User/ionictutes/myioncApp/app-release-unsigned.apk appname.apk

Chaplain answered 23/8, 2019 at 5:42 Comment(0)
I
1

You have to sign with jarsigner first. Then you will be able to zipalign it.

Quick explanation:

keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

You’ll first be prompted to create a password for the keystore. Then, answer the rest of the nice tools’s questions and when it’s all done, you should have a file called my-release-key.keystore created in the current directory.

jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore HelloWorld-release-unsigned.apk alias_name

After this process you should be able to:

zipalign -v 4 HelloWorld-release-unsigned.apk HelloWorld.apk
Incombustible answered 10/2, 2016 at 13:9 Comment(5)
Hi Xelom, thank you for the response! I generated everything fine like you said to do, I got the keytool to generate the key and the signing worked fine, but when I tried zipping it I got: Users/alexandermcintosh/Library/Developer/android-sdk-macosx/build-tools/23.0.2/zipalign -v 4 android-release-unsigned.apk green-running.apk Unable to open 'android-release-unsigned.apk' as zip archive So it still gives the same error.Subservience
I haven't tested this solution 100% as yet but I am able to go through the process as normal after I remove the bower dependancy for Angular Material. It even runs on the device, just is a shame since most of the project depends on Angular Material.Subservience
Looks like it was specifically this dependancy: Angular Material IconsSubservience
Are you adding icons to your project or trying to download them? It is not a good practice to download them in a mobile application.Incombustible
I was adding icons to the project, I was making the app have as little need for an active internet connection as possible. Literally removing from my bower.json the dependancy material-design-icons stopped all the errors thus far.Subservience
H
0

Today I got the same issue in my Mac OS and I resolved it by taking below steps.

  1. First I copied android-release-unsigned.apk from path:

    /Applications/XAMPP/xamppfiles/htdocs/Old Projects/kcformulas-2020/mesh-formula/platforms/android/build/outputs/apk/release/android-release-unsigned.apk
    

    to my current Ionic Project folder i.e.

    /Applications/XAMPP/xamppfiles/htdocs/Old Projects/kcformulas-2020/mesh-formula 
    
  2. After that I run below command :-

    jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore android-release-unsigned.apk mesh_formula
    
  3. After that I run below command by giving complete path to zipalign like below :-

    /Users/amitgupta/Library/Android/sdk/build-tools/29.0.2/zipalign -v 4 android-release-unsigned.apk KCMeshFormula.apk
    

    My APK successfully generated after taking above steps.

Hope it will help someone in future.

Hark answered 6/1, 2020 at 1:17 Comment(0)
Z
0

I had the same problem and it worked using the complete path.

For example:

zipalign -v 4 /Users/rafaelsantiago/app/platforms/android/app/build/outputs/apk/release/app-release-unsigned.apk app.apk

Zed answered 30/4, 2020 at 0:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.