androidapkreverse-engineeringaapt

How to force adding a custom attribute in the android namespace to a resource xml file of an android app


I tried to add a custom attribute in the android namespace to a view in a resource xml file in android app as follows:

<androidx.appcompat.widget.LinearLayoutCompat
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    android:orientation="vertical"
    android:custom="" />

but got an error when building:

AAPT: error: attribute android:custom not found.

I just want to test how android system will behave when there is an unexpected attribute of android namespace in an app. Note that the custom attribute should be in android namespace. Is there a way to force adding such an attribute without triggering an error when building an apk. Or is there a way to achieve this by reverse engineering, e.g., unpacking, modifying and repacking an apk. Actually, I have also tried the reverse engineering way with apktool, but got the similar error as building an app, probably because apktool also leverages aapt to repack an apk. Any help will be appreciated.


Solution

  • You can't add a custom attribute to the android namespace. That namespace is pre-defined and consists of only the built in Android attributes. Adding a custom attribute adds it to the application namespace, by convention that's the app:XXX attributes (although that's a convention, you can call it anything you want). Please note that custom attributes must be defined in attrs.xml, you can't just make one up in the layout xml.