Gradle zipAlign task not working?

2019-01-17 22:08发布

问题:

It appears the Gradle zipAlign task isn't working for me, not sure what I'm doing wrong. I've tried including the zipAlign task, and not including it, but it doesn't seem to make a difference. My gradle scripts spit out a "release" build, but it's never zipAligned according to the developer console when I try to upload my .apk.

Here's my build script:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}

apply plugin: 'android'

dependencies {
    compile project(':facebook-android-sdk-3.0.1:facebook')
    compile project(':google-play-services_lib')
    compile project(':nineoldandroids')
    compile project(':SlidingMenu-master:library')
    compile project(':ViewPagerIndicator')
    compile project(':volley')
    compile project(':windowed-seek-bar')
    compile files('compile-libs/androidannotations-2.7.1.jar', 'libs/Flurry_3.2.1.jar', 'libs/google-play-services.jar', 'libs/gson-2.2.4.jar', 'libs/picasso-1.1.1.jar', 'libs/crittercism_v3_0_11_sdkonly.jar', 'libs/gcm.jar', 'libs/apphance-library.jar')
}

android {
    buildToolsVersion "17.0"
    compileSdkVersion 17

    signingConfigs {
        debug {
            storeFile file('keystores/debug.keystore')
        }
    }

    buildTypes {
        debug {
            sourceSets {
                main {
                    manifest.srcFile 'AndroidManifest.xml'
                    java.srcDirs = ['src']
                    resources.srcDirs = ['src']
                    aidl.srcDirs = ['src']
                    renderscript.srcDirs = ['src']
                    res.srcDirs = ['res']
                    assets.srcDirs = ['assets']
                }
            }
        }

        release {
            zipAlign true
            sourceSets {
                main {
                    manifest.srcFile 'AndroidManifest.xml'
                    java.srcDirs = ['src']
                    resources.srcDirs = ['src']
                    aidl.srcDirs = ['src']
                    renderscript.srcDirs = ['src']
                    res.srcDirs = ['res']
                    assets.srcDirs = ['assets']
                }
            }
        }
    }
}

Any help appreciated!

回答1:

Your release build type is not configured for signing.

If you are signing your apk manually, you need to run ZipAlign manually as well. ZipAlign must happen after signing.

Gradle will zipalign an apk only if it can sign as well.

To setup signing for the release config, you'll need to first create a new signing config, then assign it to the build type.

android {
  signingConfigs {
    release {
      storeFile file("/path/to/keystore")
      storePassword "??"
      keyAlias "??"
      keyPassword "??"
    }
  }

  buildTypes {
    release {
      signingConfig signingConfigs.release
    }
  }
}

Note that all 4 parameters in the signing config are required, otherwise it'll consider some values are missing and it won't even attempt to sign.



回答2:

It is possible to take any existing Android Studio gradle project and build/sign it from the command line without editing any files. This makes it very nice for storing your project in version control while keeping your keys and passwords separate and not in your build.gradle file:

./gradlew assembleRelease -Pandroid.injected.signing.store.file=$KEYFILE -Pandroid.injected.signing.store.password=$STORE_PASSWORD -Pandroid.injected.signing.key.alias=$KEY_ALIAS -Pandroid.injected.signing.key.password=$KEY_PASSWORD