android动态修改refresh rate,通过gradle 动态修改AndroidManifest.xml文件

前言

在Android使用Gradle进行编译打包时,有时候需要动态更改AndroidManifest.xml中application、activity、meta-data等节点属性,大多数情况下一般通过productFlavors占位符替换即可完成相应的功能,但存在一些比较复杂的情况,比如说引用了一些第三方aar或者sdk,里面的一些节点属性我们不需要、或者是需要采用占位符替换,显得就不再方便或者不能达到所想要的目的。下面介绍另一种通过Groovy动态更改AndroidManifest.xml节点属性的方法。

通过Gradle动态修改Manifest文件

1、在项目配置结束的task中,找到生成Release的manifest的task

2、通过该task找到输出的manifest文件并读取文本内容

3、使用Groovy的Xml Api对manifest进行修改

4、修改后重新写,输出至原始目录

例如:在release包manifest中动态添加标签为meta-data的渠道号channel

引用第三方sdk里面有一些多申请的权限,我们也可以在这边直接剔除掉,这里选择的切入点在processResources的任务执行之前,代码如下:

android.applicationVariants.all { variant ->

variant.outputs.all { output ->

output.processResources.doFirst { pm ->

String manifestPath = output.processResources.manifestFile

println "=====test manifestPath=====$manifestPath"

def manifestContent = file(manifestPath).getText()

manifestContent = manifestContent.replace('', '')

def xml = new XmlParser().parseText(manifestContent)

println "=====test xml=====$xml"

xml.application[0].appendNode("meta-data", ['android:name': 'channel', 'android:value': 'yingyongbao'])

def serialize = groovy.xml.XmlUtil.serialize(xml)

file(manifestPath).write(serialize)

def outputFile = output.outputFile

if (outputFile != null && outputFile.name.endsWith(".apk")) {

def fileName = "Android_test.apk"

output.outputFileName = fileName

}

}

}

}

另外一种写法,在这里我们直接找到build里面manifest的具体路径,又不不同gradle的版本不一样,manifest的位置会有所区别,具体写法如下:

android.applicationVariants.all { variant ->

String variantName = variant.name.capitalize()

def processManifestTask = project.tasks.getByName("process${variantName}Manifest")

println "=====test processManifestTask=====$processManifestTask"

processManifestTask.doLast { pmt ->

def manifestPath = new File("${buildDir}/intermediates/bundle_manifest/${variant.dirName}/process${variant.dirName}Manifest/bundle-manifest/AndroidManifest.xml")

println "=====test manifestPath=====$manifestPath"

def manifest = file(manifestPath).getText()

.replaceAll('', ' ')

def xml = new XmlParser().parseText(manifest)

println "=====test xml=====$xml"

xml.application[0].appendNode("meta-data", ['android:name': 'channel', 'android:value': 'yingyongbao'])

def serialize = groovy.xml.XmlUtil.serialize(xml)

file(manifestPath).write(serialize)

def outputFile = output.outputFile

if (outputFile != null && outputFile.name.endsWith(".apk")) {

def fileName = "Android_test.apk"

output.outputFileName = fileName

}

}

}

// 有些版本的gradle的manifest路径在这下面

//def manifestPath = "${buildDir}/intermediates/manifests/full/${variant.dirName}/AndroidManifest.xml"

这里我们就处理掉项目中的BLUETOOTH权限,前提是项目中没有使用到蓝牙权限,并且我们还在manifest里面添加了一个meta-data,包含了渠道。

补充

由于在gradle 3.3 版本 processResources 提示已过期,这里我们根据系统提示使用了一种新的方式,代码如下:

android.applicationVariants.all { variant ->

variant.outputs.all { output ->

ManifestProcessorTask processorTask = output.processManifestProvider.getOrNull()

processorTask.doLast { ManifestProcessorTask task ->

def directory = task.getBundleManifestOutputDirectory()

println "=====test directory=====$directory"

def manifestPath = "$directory/AndroidManifest.xml"

println "=====test manifestPath=====$manifestPath"

def manifestContent = file(manifestPath).getText()

println "=====test manifestContent=====$manifestContent"

manifestContent = manifestContent.replace('', '')

def xml = new XmlParser().parseText(manifestContent)

println "=====test xml=====$xml"

xml.application[0].appendNode("meta-data", ['android:name': 'channel', 'android:value': 'yingyongbao'])

def serialize = groovy.xml.XmlUtil.serialize(xml)

file(manifestPath).write(serialize)

def outputFile = output.outputFile

if (outputFile != null && outputFile.name.endsWith(".apk")) {

def fileName = "Android_test.apk"

output.outputFileName = fileName

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值