/**

* @author Tibib

*

*/

public class AndroidManifestParser {

public String NS = "http://schemas.android.com/apk/res/android" ;

public AppInfo parse(InputStream in) throws Exception {

try {

//使用pull解析库

XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();

NS = parser.getNamespace();

//设置使用 namespaces特性

parser.setFeature(XmlPullParser. FEATURE_PROCESS_NAMESPACES , true );

parser.setInput(in, "UTF-8" );

parser.nextTag();

return readAppInfo(parser);

} catch (Exception e){

e.printStackTrace();

throw e;

} finally {

in.close();

}

}

private AppInfo readAppInfo(XmlPullParser parser) throws Exception{

AppInfo appInfo = new AppInfo();

while (parser.next() != XmlPullParser. END_TAG) {

if (parser.getEventType() != XmlPullParser. START_TAG) {

continue ;

}

String name = parser.getName();

// Starts by looking for the General tag

if ("application" .equals(name)){

String attrLabelValue = parser.getAttributeValue( NS, "label" );

String attrIconValue = parser.getAttributeValue( NS, "icon" );

appInfo.setAppName(attrLabelValue.split( "/" )[1]);

appInfo.setIconName(attrIconValue.split( "/" )[1]);

}

else {

skip(parser);

}

}

return appInfo;

}

// Skips tags the parser isn't interested in. Uses depth to handle nested tags. i.e.,

// if the next tag after a START_TAG isn't a matching END_TAG, it keeps going until it

// finds the matching END_TAG (as indicated by the value of "depth" being 0).

private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {

if (parser.getEventType() != XmlPullParser. START_TAG) {

throw new IllegalStateException();

}

int depth = 1;

while (depth != 0) {

switch (parser.next()) {

case XmlPullParser. END_TAG:

depth--;

break ;

case XmlPullParser. START_TAG:

depth++;

break ;

}

}

}

}

修改strings.xml中name属性为app_name(具体名称看配置)的值

/**

* @author Tibib

*

*/

public class XmlModifyUtil {

/**

* 使用的是 jdom库

*/

public static void modifyXML(File modifyXmlFile, String appNameAttrValue,

String appNameText) {

OutputStreamWriter bos = null ;

try {

SAXBuilder builder = new SAXBuilder();

if (modifyXmlFile.exists()) {

Document document = (Document) builder.build(modifyXmlFile);

Element root = document.getRootElement();

List stringChildList = root.getChildren( "string");

for (Element element : stringChildList) {

String nameAttrValue = element.getAttribute("name" )

.getValue();

if (nameAttrValue.equals(appNameAttrValue)) {

element.setText(appNameText);

}

}

String xmlFileData = new XMLOutputter().outputString(document);

// strings.xml默认是UTF-8格式

bos = new OutputStreamWriter(

new FileOutputStream(modifyXmlFile), "UTF-8" );

bos.write(xmlFileData);

bos.flush();

} else {

System. out .println("File does not exist" );

}

} catch (Exception ex) {

ex.printStackTrace();

} finally {

if (bos != null ) {

try {

bos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

执行编译和签名命令

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐