指示Android摇篮脚本删除未对齐的APK和清洁的人造文件 [英] Instruct Android Gradle script to delete unaligned apks and clean artifact files

查看:211
本文介绍了指示Android摇篮脚本删除未对齐的APK和清洁的人造文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我前几天开始使用摇篮构建系统,并拿到剧本,因为我想要的工作,那就是:

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')
    }
}

正如你可以看到有没有什么花哨这里只是构建应用程序的3味道和使用相同的密钥签名它们。我只是运行gradle这个assembleRelease 20秒后,我有我的生成文件夹的apk。但问题是,有其它文件生成我不想例如appname中释放-unaligned.apk做。

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.

我知道,这个文件是必需的apk才能签署,但想添加一个任务,我的剧本的gradle要删除这些文件?

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?

此外,如果有可能,我想删除编译过程中生成的所有其他(壳我说的假象文件)。基本上我想运行干净类似,但的gradle离开构建APK文件。我该怎么做呢?

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?

奖金:如果任何人有我如何能优化这个脚本,并启用压缩校准和ProGuard的指针(没有自定义规则只是默认模糊处理是确定),这也将帮助我,因为我很新的摇篮,没有教程的我其次说明了这些步骤。

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.

推荐答案

pretty老话题,但这里是删除不必要的未对齐文件现代化的解决方案。这是很方便尤其是CI服务器,以节省一些空间。

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.

这是一个耻辱,插件不为'的zipalign任务挂钩,所以我们需要挂钩上的zipalign后变为组装的任务。

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'.

作品与去年gradle这个插件1.2.0(gradle这个-2.4),但应为1是有效的。+

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()
        }
    }
  }
}

和另外一个,如果你的preFER检查zipAlignEnable国旗,但在这种情况下,因为释放与zipAlignEnabled = TRUE且没有跳过signingConfig的zipalign的任务,只生产建造你会被追平文件名以未对齐不变一个文件:应用程序释放-unsigned.apk

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.

这篇关于指示Android摇篮脚本删除未对齐的APK和清洁的人造文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆