Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

android - XML-driven GUIs and performance

Reading the Online Developer Guide page on XML layouts, I've found the following statement:

Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile.

I know about the many advantages of XML layouts and resources, but since the XML files are placed inside the APK, I think there's no real way to modify the GUI without repackaging. I mean, most of us are using eclipse ADT plugin and ANT to package the apps, so there's no real gain in not compiling the class files (since after modifying the resource files, a developer will have to repackage the app and produce a new APK file). Because of this, there is no way to redeploy just the GUI on devices without redeploying the entire APK.

If this supposition were true, then XML files are going to be the same during the lifetime of an APK file. I guess these files (specially layout ones) have to be parsed and processed at runtime (before an activity's onCreate) which would be less efficient than building the GUI programmatically (like in Swing). Layout is not usually a bottleneck, but if I'm correct, I see here an small waste of time that could be better used (for instance, with animations).

Reading the same page, it states:

When you compile your application, each XML layout file is compiled into a View resource.

Inspecting one of my APKs, I've been looking for a precompiled file inside classes.dex and there's nothing but my java classes and the R.class file. The layout XML files are inside the /res/layout folder, and there's a file named resources.arsc which seems to contain info about other resources (strings, icons names), but nothing related to Views, I think (correct me if I'm wrong).

My questions:

  • Are XML layout files precompiled, and to which file?
  • If not, is there a compilation option to fully precompile layout info into a file for faster load time? (ideally, this would be a class file)
  • If not, is there a way to create that file in the first execution and cache it on the application installation folder, so that subsequent runtime executions can read that file instead of parsing the XML and have a faster load time?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have a gander at the class documentation for LayoutInflater, you'll notice they say:

For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)

So yes, layout files are indeed precompiled to some degree, and, judging by the above excerpt, it would be in the R$layout.class output file (but I'm not 100% sure on that). The precompiled layout file is located in your compiled APK package, as /res/layout/<layout_id>.xml. You'll notice if you extract it and open it in a text editor that most plain-text XML elements have been mapped to some binary form.

It would probably be the same kind of compression you can see in AndroidManifest.xml files that get packaged into the APKs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...