Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 724
Igor Čordaš Avatar asked Nov 08 '13 10:11

Igor Čordaš


2 Answers

UPDATE February 2018. This block will cause a build error using Android Gradle plugin 3.0 or above. See 'deepSymmetry's comment below.

The "fix" is to delete the block altogether and the plugin's default behavior will automatically clean up the intermediate temporary apks (ex: app-debug-unaligned.apk).


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.

like image 168
vigilancer Avatar answered Nov 15 '22 19:11

vigilancer


I noticed there is some activity on this question from time to time so here is the way I solved the problem if it helps someone. Just define new task to copy file and then set execution order.

task copyTask(type: Copy) {
   from 'build/apk'
   into 'apks'
   exclude '**/*-unaligned.apk'
}

task allTask(dependsOn: ['clean', 'assembleRelease', 'copyTask']){
    clean.mustRunAfter copyTask
    copyTask.mustRunAfter assembleRelease
}

then just call this allTask when you want to do a build.

like image 40
Igor Čordaš Avatar answered Nov 15 '22 17:11

Igor Čordaš