更具体地说,我编写了一个xml解析器来查看Android应用程序的清单文件,作为lint检查的一部分;此代码以前正在运行,并在更新到Android Studio 3.0后开始失败,但我看不出为什么会导致此问题。
以下是我的清单的application
节点:
<application
android:name=".App"
android:allowBackup="false"
android:icon="@drawable/app"
android:label="@string/app_name"
android:supportsRtl="false"
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">
尝试迭代清单中application
节点的属性时,我使用了以下代码片段:
// the variable `f` is my manifest file; I've confirmed it is the correct file
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document manifest = builder.parse(f);
// in practice this list only ever has 1 element
NodeList applicationList = manifest.getElementsByTagName("application");
if (applicationList.getLength() > 0) {
for (int i = 0; i < applicationList.getLength(); i++) {
org.w3c.dom.Node application = applicationList.item(i);
NamedNodeMap attributes = application.getAttributes();
// checking the size of `attributes` here gives 3
// but my application node has 7 attributes
for (int j = 0; j < attributes.getLength(); j++) {
// I examine the attributes here
}
}
}
可以预期,如果attributes
项名称被列出,它会产生android:name
,android:allowBackup
,android:icon
,android:label
,{{1} },android:supportsRtl
和android:theme
;但是,仅报告了3个属性:tools:replace
,android:allowBackup
和android:label
解析器不会抛出任何异常,但现在我的lint检查失败了,因为它检测到检查通过所需的缺少属性。在短期内,我显然可以删除检查,但我完全不知道为什么它会检测到某些属性而忽略其他属性。任何帮助表示赞赏!