Instruct Android Gradle Script To Delete Unaligned Apks And Clean Artifact Files
Solution 1:
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.
Solution 2:
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.
Solution 3:
I can at least answer your bonus-question:
buildTypes {
release {
runProguard true
signingConfig signingConfigs.release
}
}
If you have specific proguard-rules, just enter this line to your defaultConfig
or to your product flavors:
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
The first proguard rule is the generla one from your Android SDK, the second one is from your module-directory.
ZipAlign is enabled by default if you build you project with the build variant release
.
Solution 4:
Just a little bit modified answer from @PSIXO, сonsidering Android Studio 1.5, running test cases and renaming apk file for better CI integration:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.mydemoci"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
archivesBaseName = "$applicationId-v$versionName"
}
}
task copyOutputApk(type: Copy) {
from'build/outputs/apk'
into 'apk'
exclude '**/*-unaligned.apk'
}
task buildTestDebug(dependsOn: ['clean', 'assembleDebug', 'check', 'copyOutputApk']) {
check.mustRunAfter assembleDebug
copyOutputApk.mustRunAfter check
clean.mustRunAfter copyOutputApk
}
To start build run gradlew buildTestDebug
Post a Comment for "Instruct Android Gradle Script To Delete Unaligned Apks And Clean Artifact Files"