如何在将res复制到apk之后执行任务

时间:2014-05-06 21:33:07

标签: android gradle

我在res / xml中有xml文件,我想更改一个节点只是为了构建,之后再转到默认值,例如:

之前:

<resources>
  <integer name="ga_sessionTimeout">
    300
  </integer>
  <bool name="ga_autoActivityTracking">
    true
  </bool>
  <screenName name="pl.grzeslowski.transport.activities.MainActivity_">
    Main Activity
  </screenName>
  <screenName name="pl.grzeslowski.transport.activities.ResultActivity_">
    Result Activity
  </screenName>
  <string name="ga_trackingId">
      UA-XXXXXXX-Y
  </string>
</resources>

后:

<resources>
  <integer name="ga_sessionTimeout">
    300
  </integer>
  <bool name="ga_autoActivityTracking">
    true
  </bool>
  <screenName name="pl.grzeslowski.transport.activities.MainActivity_">
    Main Activity
  </screenName>
  <screenName name="pl.grzeslowski.transport.activities.ResultActivity_">
    Result Activity
  </screenName>
  <string name="ga_trackingId">
    UA-1111111-11111
  </string>
</resources>

在我的gradle .build中,我有这个任务(在文件底部),但它没有工作:

task loadAdSense() {

    println "load"
    // saving adsense tag to xml file
    def xmlFile = file("src/main/res/xml/global_tracker.xml")
    def xml = new XmlParser().parse(xmlFile)
    xml.string.each {
        if (it.@name == "ga_trackingId") {
            it.value = adsenseTag
        }
    }
    new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
}

build.dependsOn loadAdSense

task cleanXml(dependsOn: build) << {

    println "clean"
    // going back with xml
    def xmlFile = file("src/main/res/xml/global_tracker.xml")
    def xml = new XmlParser().parse(xmlFile)
    xml.string.each {
        if (it.@name == "ga_trackingId") {
            it.value = "UA-XXXXXXX-Y"
        }
    }
    new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
}
cleanXml.dependsOn build
cleanXml.mustRunAfter build

0 个答案:

没有答案