Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
X

X2C

> 编程语言
Open source

Increase layout loading speed 200%

2.6K stars0 likes0 views
WebsiteGitHub

About

Increase layout loading speed 200%

Background

中文

Generally, when writing a page, everyone writes the layout through xml, and loads the xml layout into memory through set contentView or LayoutInflater.from(context).inflate method.

advantage

  • Good maintainability
  • Support instant preview
  • Clear code structure

Disadvantage

  • Reading xml is time consuming
  • Recursive parsing xml is time consuming
  • The time it takes to generate an object is more than three times that of new.

Our team has also explored many solutions on this issue. Once it reached the other extreme, completely abandoning xml, all controls are new through java, even directly in the canvas, so although the performance is indeed improved, but the code is gone. A little bit of readability and maintainability. We later reflected on the code to see the machine, or to show it? ? Maybe X2C has given us an answer.

X2C

In order to preserve the advantages of xml and solve the performance problems it brings, we developed the X2C solution. That is, during the compilation and generation of the APK, the translation of the layout that needs to be translated generates the corresponding java file, so that the developer writes the layout or writes the original xml, but for the program, the runtime loads the corresponding java file. We use APT (Annotation Processor Tool) + JavaPoet technology to complete the operation of the whole process during the compilation [Annotation] -> [Resolve] -> [Translate xml] -> [Generate java].

Performance comparison

After the development integration, we did a simple test, the performance comparison is as follows

Loading method frequency Average load time
XML 100 30
X2C 100 11

Integrated use

1.Add dependency

Add dependencies in the build.gradle file of the module

java
annotationProcessor 'com.zhangyue.we:x2c-apt:1.1.2'
implementation 'com.zhangyue.we:x2c-lib:1.0.6'

2.Add annotation

Add annotations to any java class or method that uses the layout.

java
@Xml(layouts = "activity_main")

3.Configure custom properties (no or no)

Create an X2C_CONFIG.xml file under the module, which defines the mapping between attributes and methods. If the receiver is a view, write the view. Otherwise fill in params.

mxl
<x2c-config>
    <attr name="app:mixColor" toFunc="view.setMixColor(int)" />
    <attr name="android:layout_marginTop" toFunc="params.topMargin=int" />
</x2c-config>

4.Load layout through X2C

Replace where you originally used set content view or inflate, as follows:

java
this.setContentView(R.layout.activity_main); --> X2C.setContentView(this, R.layout.activity_main);
java
LayoutInflater.from(this).inflate(R.layout.activity_main,null); --> X2C.inflate(this,R.layout.activity_main,null);

Process file

Original xml

…

Generated java file

…

Generated map file

…

not support

  • The merge tag cannot determine the parent of the xml during compilation, so it cannot be supported.
  • System style, only the style list of the application can be found during compilation, and the system style cannot be queried, so only the in-app style is supported.

support

  • ButterKnifer
  • DataBinding
  • android widget
  • customView
  • include tag
  • viewStub tag
  • fragment label
  • Application style
  • Custom attribute
  • System attribute
Attribute name Attribute name
android:textSize app:layout_constraintRight_toLeftOf
android:textColor app:layout_constraintBottom_toTopOf
android:text app:layout_constraintTop_toTopOf
android:background app:layout_constrainedHeight
view all

There are usage problems and other technical issues, welcome to exchange discussion

QQ群:870956525,Add please specify fromX2C

Welcome everyone to use, the project will continue to maintain

Another: welcome to joinIReader family, study new Android technologies together. Please send your resume[email protected],Indicate the direction of employment。

LICENSE

…

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Java

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言