Gradle脚本:如何在XML文件中创建和写入数据

时间:2019-05-08 06:29:24

标签: android xml gradle groovy build.gradle

我正在创建xml,并通过使用gradle脚本并在xml文件中写入一些数据。我已经创建了.gradle文件,运行gradle文件后,我无法实现xml文件的确切格式。我在下面发布我的代码:请任何人指导我。 导入groovy.xml.MarkupBuilder

task generatepublicxml {

    //def resDir = project.projectDir.absolutePath + "/src/main/res-public"

    // Create new public.xml with writer
    new File("/home/signity/Desktop/public.xml").withWriter { writer ->
        // Create MarkupBuilder with 4 space indent
        def destXml = new MarkupBuilder(new IndentPrinter(writer, "    ", true));
        def destXmlMkp = destXml.getMkp();

        // GIST NOTE: our project needed the ResourceName suppression, but its not needed in general
        destXml.resources('xmlns:tools': 'http://schemas.android.com/tools', 'tools:ignore': 'ResourceName')
                {
            // Leave file comment
            destXmlMkp.yield "\r\n"
            destXmlMkp.comment("AUTO-GENERATED FILE.  DO NOT MODIFY. public.xml is generated by the generatepublicxml gradle task")

        }
    }
}

我的必需文件格式:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="remoteAccessConsumerKey">3MVG92mNMNiWvonjPDM9qqaDip0MFl9TGc</string>
    <string name="oauthRedirectURI">saleschap:///mobilesdk/detect/oauth/done</string>

    <string-array name="oauthScopes">
        <item>api</item>
        <item>web</item>
        <item>refresh_token</item>
    </string-array>

    <string name="androidPushNotificationClientId"></string>

</resources>

1 个答案:

答案 0 :(得分:1)

由于我看到您是从gradle构建文件中执行此操作的,因此请首先注意gradle执行阶段。

我假设您希望在执行任务时而不是在加载构建文件时运行代码。如果是这样,则需要将代码放在任务的doFirstdoLast块中。如果您按照问题的方式编写代码,则无论构建文件generatepublicxml是否运行,都将在加载构建文件时执行该代码(即,您可以运行完全不同的任务,并且代码仍会运行)。

从gradle开始时,这通常并不明显,我建议您阅读gradle docs on the build lifecycle,以免感到困惑。

通过以下方式,构建以下build.gradle:

import groovy.xml.*

task generatepublicxml {
  doLast { 
    file("public.xml").withWriter { writer ->
      // Create MarkupBuilder with 4 space indent
      def xml = new MarkupBuilder(new IndentPrinter(writer, "    ", true))

      xml.doubleQuotes = true
      xml.mkp.xmlDeclaration(version: '1.0', encoding: 'utf-8')

      xml.resources('xmlns:tools': 'http://schemas.android.com/tools', 'tools:ignore': 'ResourceName') {
        string(name: 'remoteAccessConsumerKey', '3MVG92mNMNiWvonjPDM9qqaDip0MFl9TGc')
        string(name: 'oauthRedirectURI',        'saleschap:///mobilesdk/detect/oauth/done')
        'string-array'(name: 'oauthScopes') {
          item('api')
          item('web')
          item('refresh_token')
        }
        string(name: 'androidPushNotificationClientId')

        // Leave file comment
        mkp.yield('\n    ')
        mkp.comment("AUTO-GENERATED FILE.  DO NOT MODIFY. public.xml is generated by the generatepublicxml gradle task")

      }
    }
  }
}

运行时:

~> gradle generatepublicxml

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

得出以下public.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ResourceName">
    <string name="remoteAccessConsumerKey">3MVG92mNMNiWvonjPDM9qqaDip0MFl9TGc</string>
    <string name="oauthRedirectURI">saleschap:///mobilesdk/detect/oauth/done</string>
    <string-array name="oauthScopes">
        <item>api</item>
        <item>web</item>
        <item>refresh_token</item>
    </string-array>
    <string name="androidPushNotificationClientId" />
    <!-- AUTO-GENERATED FILE.  DO NOT MODIFY. public.xml is generated by the generatepublicxml gradle task -->
</resources>

如果您不希望在xmlns元素上使用resources属性等,则可以执行以下操作:

xml.resources {
...
}

其结果是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   ...