本文主要是介绍gradle打包编译进程process获取信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
通过获取编译进程信息,获取添加log日志及记录编译时各个进程的信息。
调整打包apk名称
applicationVariants.all { variant ->mavenFlavorName = variant.flavorNamevariant.outputs.all { output ->outputFileName = "${project.getName()}_${variant.flavorName}_${variant.buildType.name}_${variant.versionName}_${variant.versionCode}_${releaseTime()}.apk"}}
android.applicationVariants.all { variant ->variant.outputs.each { output ->def processManifest = output.getProcessManifestProvider().get()processManifest.doLast { task ->def outputDir = task.multiApkManifestOutputDirectoryFile outputDirectoryif (outputDir instanceof File) {outputDirectory = outputDir} else {outputDirectory = outputDir.get().asFile}File manifestOutFile = file("$outputDirectory/AndroidManifest.xml")println("----------- ${manifestOutFile} ----------- ")if (manifestOutFile.exists() && manifestOutFile.canRead() && manifestOutFile.canWrite()) {def manifestFile = manifestOutFile///这里第二个参数是 false ,所以 namespace 是展开的,所以下面不能用 androidSpace,而是用 nameTagdef xml = new XmlParser(false, false).parse(manifestFile)def exportedTag = "android:exported"def nameTag = "android:name"///指定 space//def androidSpace = new groovy.xml.Namespace('http://schemas.android.com/apk/res/android', 'android')def nodes = xml.application[0].'*'.findAll {//挑选要修改的节点,没有指定的 exported 的才需要增加//如果 exportedTag 拿不到可以尝试 it.attribute(androidSpace.exported)(it.name() == 'activity' || it.name() == 'receiver' || it.name() == 'service') && it.attribute(exportedTag) == null}///添加 exported,默认 falsenodes.each {def isMain = falseit.each {if (it.name() == "intent-filter") {it.each {if (it.name() == "action") {//如果 nameTag 拿不到可以尝试 it.attribute(androidSpace.name)if (it.attributes().get(nameTag) == "android.intent.action.MAIN") {isMain = trueprintln("......................MAIN FOUND......................")}}}}}it.attributes().put(exportedTag, "${isMain}")}PrintWriter pw = new PrintWriter(manifestFile)pw.write(groovy.xml.XmlUtil.serialize(xml))pw.close()}}}}
android.applicationVariants.all { variant ->variant.outputs.each { output ->//println("=============== ${variant.getBuildType().name.toUpperCase()} ===============")//println("=============== ${variant.getFlavorName()} ===============")def vnif (variant.getFlavorName() != null && variant.getFlavorName() != "") {vn = variant.name;} else {if (variant.getBuildType().name == "release") {vn = "Release"} else {vn = "Debug"}}def taskName = "process${vn}MainManifest";try {println("=============== taskName ${taskName} ===============")project.getTasks().getByName(taskName)} catch (Exception e) {return}///你的自定义名字project.getTasks().getByName(taskName).doFirst {//def method = it.getClass().getMethods()it.getManifests().getFiles().each {if (it.exists() && it.canRead()) {def manifestFile = itdef exportedTag = "android:exported"def nameTag = "android:name"///这里第二个参数是 false ,所以 namespace 是展开的,所以下面不能用 androidSpace,而是用 nameTagdef xml = new XmlParser(false, false).parse(manifestFile)if (xml.application != null && xml.application.size() > 0) {def nodes = xml.application[0].'*'.findAll {//挑选要修改的节点,没有指定的 exported 的才需要增加//如果 exportedTag 拿不到可以尝试 it.attribute(androidSpace.exported)(it.name() == 'activity' || it.name() == 'receiver' || it.name() == 'service') && it.attribute(exportedTag) == null}if (nodes.application != null && nodes.application.size() > 0) {nodes.each {def t = itit.each {if (it.name() == "intent-filter") {println("$manifestFile \n .....................${t.attributes().get(nameTag)}......................")}}}}}}}}}
}
这篇关于gradle打包编译进程process获取信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!