@@ -5,12 +5,22 @@ import org.gradle.api.Project
5
5
6
6
class ManifestTransformer {
7
7
static void transform (Project project , File input , File output ) {
8
- Node xml = new XmlParser (). parse(input)
9
- Node applicationNode = xml. get(' application' )[0 ]
10
- applicationNode. appendNode(' meta-data' , [' android:name' : project. name, ' android:value' : ' com.chenenyu.router.moduleName' ])
11
- FileWriter fileWriter = new FileWriter (output)
12
- XmlNodePrinter nodePrinter = new XmlNodePrinter (new PrintWriter (fileWriter))
13
- nodePrinter. setPreserveWhitespace(true )
14
- nodePrinter. print (xml)
8
+ Node manifest = new XmlParser (). parse(input)
9
+ Node applicationNode = null
10
+ Object application = manifest. get(' application' )
11
+ if (application instanceof NodeList ) {
12
+ if (application. isEmpty()) { // There is no `application` node in AndroidManifest.xml
13
+ applicationNode = manifest. appendNode(" application" , [' xmlns:android' : ' http://schemas.android.com/apk/res/android' ])
14
+ } else {
15
+ applicationNode = application. first()
16
+ }
17
+ applicationNode. appendNode(' meta-data' , [' android:name' : project. name, ' android:value' : ' com.chenenyu.router.moduleName' ])
18
+ }
19
+ if (applicationNode != null ) {
20
+ FileWriter fileWriter = new FileWriter (output)
21
+ XmlNodePrinter nodePrinter = new XmlNodePrinter (new PrintWriter (fileWriter))
22
+ nodePrinter. setPreserveWhitespace(true )
23
+ nodePrinter. print (manifest)
24
+ }
15
25
}
16
26
}
0 commit comments