hikage-runtime-attribute

Maven CentralMaven metadata URLAndroid Min SDK

This is the XML attribute sets runtime module of Hikage, which provides an in-memory AAPT2 resource parsing simulator.

Play Compliance Risk Warning

Since this module involves reflection operations on XmlBlock, there may be risks related to Google Play compliance. If your app needs to be listed on Google Play, please carefully evaluate the potential risks of using this module and ensure that your app complies with Google Play's policy requirements.

Hikage is not responsible for any compliance issues that may arise from using this module.

Configure Dependency

You can add this module to your project using the following method.

We recommend that you first refer to hikage-bom to use BOM for unified version management.

Add dependency in your project's gradle/libs.versions.toml.

[versions]
hikage-runtime-attribute = "<version>"

[libraries]
hikage-runtime-attribute = { module = "com.highcapable.hikage:hikage-runtime-attribute", version.ref = "hikage-runtime-attribute" }

Configure dependency in your project's build.gradle.kts.

implementation(libs.hikage.runtime.attribute)

Please change <version> to the version displayed at the top of this document.

Traditional Method

Configure dependency in your project's build.gradle.kts.

implementation("com.highcapable.hikage:hikage-runtime-attribute:<version>")

Please change <version> to the version displayed at the top of this document.

Function Introduction

You can view the KDoc click hereopen in new window.

Compatibility Report

The following is a list of system versions tested by Hikage and their corresponding devices information for reference.

Android Version / API LevelAvailabilityTest Devices
5.0.2 (21)Redmi Note 2
6.0 (23)OPPO A53
7.0 (24)Xiaomi Mi-4c
7.1.2 (25)Samsung Galaxy S4
8.0.0 (26)Xiaomi Mi-5s
Huawei P9
8.1.0 (27)SC801A
9 (28)Huawei P10 Plus
10 (29)Hisense A9
11 (30)OPPO A9
12 (31)Huawei nova 8
13 (33)Xiaomi MIX 2S
14 (34)ARM64 Emulator
15 (35)Redmi Note 12 Turbo
16 (36)Xiaomi 15 Ultra
17 (37)ARM64 Emulator

Use in Hikage

In most cases, the capabilities of this module will be used as the base of hikage-core. You do not need to use the APIs of this module directly to parse XML attribute sets.

For specific usage, please refer to hikage-core → XML Attribute Sets.

Use in Any Project

Hikage provides decoupled API capabilities for this module. You can use the XML attribute set parsing capabilities provided by this module out of the box in any project.

AttributeSetResolver

We create an XML resource of android:text attribute for TextView and parse it into an AttributeSet object.

The following example

// Assume this is your Context instance.
val context: Context
// Create an AttributeSet resolver instance.
val resolver = AttributeSetResolver.from(context)
// Returns an XmlResourceParser object, which is AttributeSet.
val attrs = resolver.newParser(
    listOf(
        AttributeItem.from(
            name = "android:text", 
            value = AttributeItem.Value.Str("Hello dynamic attribute!")
        )
    )
)
// Create a TextView and pass in the parsed AttributeSet.
val tv = TextView(context, attrs)

At this time, when tv is loaded into a parent layout or rendered in a layout, the android:text attribute will be correctly set and display "Hello dynamic attribute!".

AttributeSetResolver supports passing in the AttributeResolverParams parameter to configure its parsing behavior.

The following example

// Assume this is your Context instance.
val context: Context
// Assume this is the AttributeItem list you need to construct.
val items: List<AttributeItem>
// Create parameter object
val params = AttributeResolverParams(
    // Specify the forged source XML layout resource ID,
    // only used to provide context information, and will not actually inflate.
    sourceResId = R.layout.my_layout,
    // Specify the resource package name for parsing, instead of obtaining it through context.
    resourcePackageName = "com.example.myapp"
)
// Create an AttributeSet resolver instance
val resolver = AttributeSetResolver.from(context)
// Pass in the parameter object to parse the AttributeSet
val attrs = resolver.newParser(items, params)

You can also change the global default parameters of AttributeSetResolver, which will affect the default parsing behavior of all AttributeSetResolver instances in the entire project.

The following example

AttributeSetResolver.defaultResolverParams = AttributeResolverParams(
    // ...
)

AttributeSetResolver implements the Closeable interface. You can call the close() method to release the resources it occupies when you don't need to use it, or use use to automatically manage resource release.

The following example

// Assume this is your Context instance.
val context: Context
// Assume this is the AttributeItem list you need to construct.
val items: List<AttributeItem>
// Create an AttributeSet resolver instance and use it in the use block.
val attrs = AttributeSetResolver.from(context).use { resolver ->
    // Returns an XmlResourceParser object, which is AttributeSet.
    resolver.newParser(items)
}
// Create a TextView and pass in the parsed AttributeSet.
val tv = TextView(context, attrs)

AttributeSetResolver also provides a release(parser) method, which is mainly targeted at the layoutlib of the Android Studio preview function. There is no need to call this method in actual device operation.

AttributeItem

AttributeItem is a data class used to create an AttributeSet. You can construct any XML attribute set through it.

AttributeItem.Value provides four types involved in the construction process: Str, Raw, Bool, and Real.

The following example

val items = listOf(
    AttributeItem.from(
        name = "android:text", 
        // Most commonly used, just pass in a string directly.
        value = AttributeItem.Value.Str("This is a text")
    ),
    AttributeItem.from(
        name = "android:textSize", 
        // Strings can be parsed to the final raw value.
        value = AttributeItem.Value.Str("16sp")
    ),
    AttributeItem.from(
        name = "android:padding", 
        // Strings can also be parsed into attribute references.
        value = AttributeItem.Value.Str("?attr/actionBarSize")
    ),
    AttributeItem.from(
        name = "android:enabled", 
        // Only supports true or false.
        value = AttributeItem.Value.Bool(true)
    ),
    AttributeItem.from(
        name = "android:alpha",
        // Corresponds to XML's float format, such as 1.0 or 0.5.
        value = AttributeItem.Value.Real(0.5f)
    ),
    AttributeItem.from(
        name = "android:layout_weight", 
        // Raw numerical value or property value of types like ID, Enum, etc.
        value = AttributeItem.Value.Raw(1)
    )
)

If you are not sure what to use in what scenario, just choose AttributeItem.Value.Str, whatever is in XML is supported.

Of course, you can also give namespace to specify the attribute namespace instead of needing to use : to specify it every time.

The following example

val item = AttributeItem.from(
    namespace = "android",
    name = "text", 
    value = AttributeItem.Value.Str("This is a text")
)

AttributeItem implements the Serializable interface. You can serialize it into any object such as JSON for transmission and deserialize it back into an object when needed.