Introduction to the

Android uses XML to describe the View structure, and the way it is parsed and displayed at runtime cannot avoid reflection and IO time consuming. QXML provides a less intrusive, extensible, code-configurable way to convert layout files into Java code to improve View building efficiency.

GitHub address: Qxml

Comparison with X2C

X2C:

Use the annotation handler to generate the View class, which needs to add annotations in the class and replace setContentView method, which is more intrusive; Support for layout attributes is not perfect, and it is prone to errors in generated classes; The extension of custom attributes is not convenient enough; Support for multiple Layout Types, View Compat, LayoutInflate Factory, and so on is not comprehensive enough.

Qxml:

Transform is used to generate View class, and the rules of generating View are realized by adding annotation class. The boundary separation between rules and code can be achieved without changing the original code. Good support for layout attributes, with the goal of complete UI consistency; Custom attributes can realize complex logic, and expansion is convenient to achieve; Support for multiple Layout types, View Compat, LayoutInflate Factory and other features to achieve peels and expand views.

performance

40% less + load time compared to inflate, which increases with layout complexity, as shown in demo performance tests.

support-test-demo.apk

androidx-test-demo.apk

requirements

Gradle 3.5.0 above

Currently supported components

Base

component Support properties component Support properties component Support properties
View almost TextView almost EditText almost
Button almost ImageView almost Button almost
CheckBox almost ImageButton almost ImageView almost
SeekBar almost RatingBar almost Spinner almost
Switch almost tableLayout almost TableRow almost
ViewStub almost ScrollView almost HorizontalScrollView almost
ProgressBar almost SurfaceView almost RadioGroup almost
RadioButton almost LinearLayout almost RelativeLayout almost
FrameLayout almost TextureView almost WebView almost
ListView almost GridLayout almost ExpandableListView almost
ViewFlipper almost ViewSwitcher almost TextSwitcher almost
VideoView almost
include almost merge almost DataBinding almost
custom style almost system style none layout multi type almost

Support & Androidx

component Support properties component Support properties component Support properties
AppBarLayout almost CardView almost CollapsingToolbarLayout almost
ConstraintLayout almost Group almost Guideline almost
Placeholder almost Barrier almost MotionLayout almost
CoordinatorLayout almost DrawerLayout almost RecyclerView almost
NestedScrollView almost TabLayout almost TabItem almost
Toolbar almost ViewPager almost Fragment almost
SwipeRefreshLayout all

Third Part

component Support properties component Support properties component Support properties
Lottie almost fresco almost

Based on using

1. In the Projectbuild.gradleAdd a dependency to

Check Releases for the latest version information

buildscript {
    ...
    ext.qxml_version = "1.3.0"
    repositories {
        ...
        mavenCentral()
    }
    ...
    dependencies {
        ...
        classpath "io.github.duduhuang88:gradle-plugin:$qxml_version". }}Copy the code

2. In the modulebuild.gradleAdd a dependency to

// apply plugin: 'com.android.application' or 'com.android.library'
apply plugin: 'com.qxml.code.plugin'compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } dependencies {... kapt"io.github.duduhuang88:qxml-processor:$qxml_version"
		or
	annotationProcessor "io.github.duduhuang88:qxml-processor:$qxml_version"

	//support
	implementation "io.github.duduhuang88:qxml-support:$qxml_version"

	//androidx
	implementation "io.github.duduhuang88:qxml-androidx:$qxml_version". }Copy the code

Configuration of 3.

android {
    ......
    sourceSets {
        main {
            ......
            resources.srcDirs += "build/qxml/tempRes". } } } qxml { enabletrue		// Whether to enable
    useFactory false		// Whether to use layoutInflater's Factory, which consumes performance when enabled
    viewDebug false		// Whether to display flags on the converted View
    logEnable true		// Whether to enable log
    compatMode com.qxml.CompatMode.AUTO  / / compat mode
    acceptReferenceStyle true    // Whether to accept style references, only a few references are supported
    useCreateViewListener true  / / use createViewListener
    // This is the default setting
    buildType {
        debug { // Configure debug separately. Default Settings are used for unset values
            viewDebug true
        }
        release {
            viewDebug false}}}Copy the code

4. Gradle. properties Optional

QXML_VALID_CODE = *** : used for custom View and local check

QXML_LOG_ENABLE = true/false: Whether to enable log for annotationProcesser

QXML_USING_STABLE_ID = true/false: Specifies whether to use a fixed resource Id. The default value is true

5. Layout Optional configuration

Available in layout.xml root:

App: QXML: genIgnore (ignore) genWithUnImplementAttr (generated) genWithoutUnImplementAttr (when there is no unrealized attributes to generate)

App: qXML_use_Factory: Uses the Layoutinflater Factory

App: qXML_DEBUG: Whether to display the debug flag of the view

App :qxml_compat: never(not compat) auto(automatic selection) force(mandatory use)

Note: The configuration takes effect for all views under the root node and has a higher priority than gradle configuration

6. Simple diagramming of build results

After the build, there is a simple chart of the results in build\ QXML \report.html

7. Confused

No need to

Extended use

disadvantages

1. Android built-in style is not supported

2. Increased compile timeFirst compilations grow in size and complexity based on layouts, and then cache is used

3. Increase the size of APK. When there are 52 layout files in demo, the size of Release APK increases by about 38K

4. Currently, extending a View requires fully qualified class names, but this may change in the future, there is no need to fully qualify class names except for defining shared variables

LICENSE

The Apache 2.0 License