Skip to content

Instantly share code, notes, and snippets.

@zhengzhou
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zhengzhou/e9b05263f2fa47868fd6 to your computer and use it in GitHub Desktop.
Save zhengzhou/e9b05263f2fa47868fd6 to your computer and use it in GitHub Desktop.
androidStudio 运行时重命名apk
def outPut = "$buildDir/outputs/apk/"
def channel_id = -1000
def buildVariant = '-debug.'
task getChannelId << {
def manifest = new XmlParser().parse("2.1.1/AndroidManifest.xml")
def androidSchemas = new groovy.xml.Namespace("http://schemas.android.com/apk/res/android", 'android')
manifest.application.('meta-data').each{
def nodeName = it.attribute(androidSchemas.name)
if(nodeName.equals('xinyi_id'))
channel_id = it.attribute(androidSchemas.value)
}
version = manifest.attribute(androidSchemas.versionName)
}
task renameOutput <<{
def dateStr = new Date().format('yyyy.MM.dd_HH.mm')
def innerName = "_${version}_${dateStr}_${channel_id}."
file(outPut).listFiles().each {File apkFile ->
if(apkFile.name.contains(buildVariant)) {
def newName = apkFile.name.replace(buildVariant,innerName);
def fileDir = file("$buildDir/"+buildVariant.subSequence(1,buildVariant.length()-1))
copy {
from apkFile
into fileDir
}
String originName = "$fileDir/$project.name${buildVariant}apk"
file(originName).renameTo(file("$fileDir/$newName"))
println("打包出文件:$fileDir/$newName ")
}
}
}
assembleDebug <<{
buildVariant = "-debug."
getChannelId.execute()
renameOutput.execute()
}
assembleRelease <<{
buildVariant = '-release.'
getChannelId.execute()
renameOutput.execute()
}
task test <<{
println("task last")
getChannelId.execute()
renameOutput.execute()
}
uploadArchives{
repositories{
}
}
@zhengzhou
Copy link
Author

apk文件生成到 build/debug/项目名+版本名+日期+渠道号.apk中
apk文件生成到 build/release/项目名+版本名+日期+渠道号.apk中

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment