Android Studio - How to ZipAlign apk
Asked Answered
P

6

43

I have created a signed apk using Android Studio and I'm now going to zipalign it, I have read this: http://developer.android.com/tools/help/zipalign.html but I'm still not sure where to add the lines of code, is it in the Gradle file and where in the file do I add the lines?

Permeate answered 20/1, 2014 at 19:39 Comment(2)
One question, can we zipaligned already signed apk. I'm doing it manually but getting error while installing it, any suggestion here.Merely
@CoDe, yes you can. linkPickaback
T
85

Inside you main module's build.gradle file you can have multiple build types along with your debug one.

You can specify zipAlign characteristic inside any of your buildType by using

buildTypes {

     release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        zipAlignEnabled true
    }
}

Note: Older versions use zipAlign, instead of zipAlignEnabled

Default gradle tasks always created for debug and release buildTypes whether you define or not. Debug is for debugging purpose and Release for Signed Application (Build >> Generate Signed Apk). You must define your signingConfig for release builds.

Check Build Types section in below mentioned link for other properties that you can use in your buildTypes

http://tools.android.com/tech-docs/new-build-system/user-guide

Tuinenga answered 20/1, 2014 at 19:44 Comment(6)
Note that with the release of Android Studio 1.0 runProguard should be changed to minifyEnabled.Lannie
I have add zipAlignEnabled true but the Developer Console keeps telling me the apk-file is not zip aligned.Testee
Be careful, the correctly zip aligned file is not in the buikd/output/apk directory but in the app directory directly. I've spent time on this!Clown
@pyus13 Anybody knows what is the default value of zipAlign ? If we are not writing it then it is false or true.Gog
I tried zipAlignEnabled false, but the apk was still zip aligned.Wilkie
Same here @KimiChiu. How do I disable it?Fulvous
O
6

If you have created an .apk file (release mode) from Android Studio, your APK is already aligned. The zipalign step is part of the Build Process in Android Studio.

You can check an APK with:

zipalign -c -v 4 {APP}.apk

To align an APK, run:

zipalign -v 4 {APP}.apk {ZIPALIGNED_APP}.apk
Onshore answered 30/12, 2015 at 7:35 Comment(1)
Android Studio was creating non-zip aligned release mode .apk files for me by default!Ladon
L
4

Set classpath from 'com.android.tools.build:gradle:2.2.0-alpha3' to

classpath 'com.android.tools.build:gradle:2.1.2'

Laith answered 9/7, 2016 at 9:7 Comment(1)
Any time. :) @herrmarteLaith
K
2

For complete signing process do follow steps. (Zip align manually after build- signed apk)

Step 0: Before sign apk add code in your app module.gradle file : set zippAlignEnable and minifyEnable true.

buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            //zipAlignEnabled true 
           //uncomment for automatically zip aligned by studio
        }
    }

Step 1: Sign your APK, relese mode more info

Step 2: find location : .\your sdk\build-tools\24.0.0

Step 3: copy your .jks keystore file and signed APK then paste it here.

Step 4: open command prompt (For windows). set path. of your location where you paste stuff.

Step 5: type command: zipalign -f -v 4 infile.apk outfile.apk

here infile is my signed apk and outfile is file which is uploaded on play store.

Done...!

If cmp will got BUILD failed.

See here for more about error (Zipalign build failed) do following

1) Restart and open Android Studio,

2) close cmd, then reopen it.

3) Now do above process.

Edit: resources.arsc BAD-1 error found

 set classpath 'com.android.tools.build:gradle:2.2.0-alpha3'

to

classpath 'com.android.tools.build:gradle:2.1.2'

Happy Release :)

Knacker answered 27/6, 2016 at 13:17 Comment(2)
Mac users: 5) ./zipalign -f -v 4 infile.apk outfile.apkLumpish
I tried the same but getting error while installing align_apk, could you please check this.Merely
W
1

zipalign is in ADT/sdk/build-tools/android4.4W

  1. Copy android4.4W folder

  2. Paste it in yourproject/platforms/android/build/outputs/apk

  3. Now, copy yourkey.keystore and your project.apk

  4. Paste them into android4.4W full path

    yourproject/platforms/android/build/outputs/apk/android4.4W

  5. then, $ zipalign -v 4 project.apk wowdone.apk.

Visit for more details: http://prehow.com/convert-wordpress-to-android-app-tutorial.html

Thats all, hope this answer will help you.

Waneta answered 24/6, 2015 at 6:44 Comment(1)
I did not found any android4.4W folder in specified location.Merely
P
0

This works for me:

  1. Copy the last version folder 22.0.1 to yourproject/platforms/android/build/outputs/apk (like version 22.0.1 is my last version)

  2. Copy yourkey.keystore and your project.apk into this verison folder 22.0.1.

  3. Under the path yourproject/platforms/android/build/outputs/apk/22.0.1, run this command:

    zipalign -v 4 name_of_current.apk name_of_wanted.apk
    
  4. The name_of_wanted.apk should be located under yourproject/platforms/android/build/outputs/apk/22.0.1

Psychology answered 23/7, 2015 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.