Android utils.gradle

从AndroidManifest中通过正则获取属性值:
  android.applicationVariants.all { variant ->  
            def manifestFile = file("src/main/AndroidManifest.xml")  
            def pattern = Pattern.compile("versionName=\"(.+)\"")  
            def manifestText = manifestFile.getText()  
            def matcher = pattern.matcher(manifestText)  
            matcher.find()  
            def versionName = matcher.group(1)  
            pattern = Pattern.compile("versionCode=\"(.+)\"")  
            matcher = pattern.matcher(manifestText)  
            matcher.find()  
            def versionCode = matcher.group(1)  
}
还可以通过GPath库操作
def getVersionNameAdvanced(){
   def xmlFile = project.file("AndroidManifest.xml")
   def androidManifest = new XmlSlurper().parse(xmlFile)
   //def androidManifest = new XmlSlurper().parse("AndroidManifest.xml")
   return androidManifest['@android:versionName']   
}



获取时间方法:
def buildTime() {  
    def df = new SimpleDateFormat("yyyyMMdd'-'HHmm")  
    df.setTimeZone(TimeZone.getDefault())  
    return df.format(new Date())  
} 


copy apk操作
task copyOutputApk(type: Copy) {
    from 'build/outputs/apk'
    into 'apk'
    exclude '**/*-unaligned.apk'
}

project.tasks.findByName('assembleRelease') << {
    copyOutputApk.execute()
    clean.execute()//清除build文件夹
}
或者定义一个task依赖,执行gradlew allTask就行了
task allTask(dependsOn: ['clean', 'assembleRelease', 'copyOutputApk']){
    clean.mustRunAfter copyOutputApk
    copyOutputApk.mustRunAfter assembleRelease
}

删除unaligned apk
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()
        }
    }
  }
}


//对于 android library 编译,我会 disable 所有的 debug 编译任务  
def disableDebugBuild(){
  //project.tasks 包含了所有的 tasks,下面的 findAll 是寻找那些名字中带 debug 的 Task。  
  //返回值保存到 targetTasks 容器中  
  def targetTasks = project.tasks.findAll{task ->
      task.name.contains("Debug")
  }
  //对满足条件的 task,设置它为 disable。如此这般,这个 Task 就不会被执行  
  targetTasks.each{
     println "disable debug task  : ${it.name}"
     it.setEnabled false
  }
}

 

转载于:https://my.oschina.net/bruces/blog/687363

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值