0

I'm trying to read xml from the android manifest using Groovy (particularly XmlSlurper from import groovy.xml.XmlUtil), and I'm getting below from Gradle.

Error:The prefix "android" for attribute "android:name" associated with an element type "activity" is not bound.

The code that results in that error is as follows:

def innerNodeTemplate = '''
                    <activity android:name=".activity.MyActivity"></activity>
                    '''
def activityNode = new XmlSlurper().parseText(innerNodeTemplate)

I have tried declaring the namespace as follows (from this existing answer)

activityNode = new XmlSlurper(false,false).parseText(innerNodeTemplate).declareNamespace(android:'android')

but then I get a more explicit exception for the same namespace issue

Error:Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 53; The prefix "android" for attribute "android:name" associated with an element type "activity" is not bound.

Is there anything else I can try?

Community
  • 1
  • 1
kira_codes
  • 1,457
  • 13
  • 38
  • There does not exist such namespace bounded in your xml. Why are you adding while parsing? – Rao Feb 17 '17 at 03:52
  • Hi Rao, "android:name" is in my xml. Do I need to add a declaration like xmlnx:android="" at the start of the xml snippet? – kira_codes Feb 17 '17 at 15:29

1 Answers1

0

As Rao pointed out, I was failing to bound the xml namespace.

The solution was adding xmlns:android="http://schemas.android.com/apk/res/android" to the root tag like so

def innerNodeTemplate = '''<activity android:name=".activity.MyActivity" xmlns:android="http://schemas.android.com/apk/res/android"></activity>'''
activityNode = new XmlSlurper(false, true).parseText(innerNodeTemplate)

Afterwards there is no need to call .declareNamespace( )

kira_codes
  • 1,457
  • 13
  • 38