PSIXO PSIXO - 3 months ago 92
Android Question

Instruct Android Gradle script to delete unaligned apks and clean artifact files

I started using Gradle build system a few days ago and got the script to work as I wanted, here it is:

buildscript {
repositories {
mavenCentral()
}
}

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

apply plugin: 'com.android.application'

android {
compileSdkVersion 17
buildToolsVersion '18.0.1'


productFlavors {
flavor1 {
packageName "flavor1"
}
flavor2 {
packageName "flavor2"
}
flavor3 {
packageName "flavor3"
}
}

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

signingConfigs {
release {
storeFile file("test.keystore")
storePassword "*****"
keyAlias "****"
keyPassword "*****"
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}

}

dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
}


As you can see there is nothing fancy here just building 3 flavours of the app and signing them with the same key. I just run gradle assembleRelease and after 20 seconds I have my apks in build folder. But the problem is that there are other files generated that I don't want for example appname-release-unaligned.apk.

I know that this file is needed before apk can be signed but would like to add a task to delete these files in my gradle script?

Also if it's possible I would like to remove all other (shell I say artefact files) generated during build. Essentially I would like to run something like gradle clean but leave the build apk files. How do I do this?

BONUS:If anyone has pointers on how can I optimise this script and enable zip align and proguard (without custom rules just default obfuscation is ok) that would also help me since I am very new to gradle and none of the tutorials I followed explains these steps.

Answer

Pretty old topic but here is modern solution for deleting unnecessary 'unaligned' file. This is quite handy especially on CI servers to save some space.

That's a shame that plugin does not provide hook for 'zipAlign' task so we'll need to hook on 'assemble' task which goes after 'zipAlign'.

Works with last gradle plugin 1.2.0 (gradle-2.4) but should be valid for 1.+

// delete unaligned files
android.applicationVariants.all { variant ->
  variant.assemble.doLast {
    variant.outputs.each { output ->
        println "aligned " + output.outputFile
        println "unaligned " + output.packageApplication.outputFile

        File unaligned = output.packageApplication.outputFile;
        File aligned = output.outputFile
        if (!unaligned.getName().equalsIgnoreCase(aligned.getName())) {
            println "deleting " + unaligned.getName()
            unaligned.delete()
        }
    }
  }
}

And another one if your prefer to check zipAlignEnable flag but in this case you'll be tied to "unaligned" constant in filename because release builds with zipAlignEnabled=true AND without signingConfig skip 'zipAlign' task and produce only one file: 'app-release-unsigned.apk'.

// delete unaligned files
android.applicationVariants.all { variant ->
variant.assemble.doLast {
    variant.outputs.each { output ->
        println "aligned " + output.outputFile
        println "unaligned " + output.packageApplication.outputFile

        File file = output.packageApplication.outputFile;
        if (variant.buildType.zipAlignEnabled && file.getName().contains("unaligned")) {
            println "deleting " + file.getName()
            file.delete()
        }
    }
  }
}

I am using the first one in case anyone cares.