Monday, April 20, 2015

Assigning constant resource IDs when re-compiling APK files





1. Add id constants to res/values/public.xml

Using this to assign a fixed ID to a resource. Add the new Ids to the last line of its kind.

<public type="id" name="action_settings" id="0x7f09003f" />
    <public type="id" name="textView" id="0x7f090040" />
    <public type="id" name="button" id="0x7f090041" />

Notice!  the id hex number must be incrementing.


2. Add id constants in R$id.smali like this:



.field public static final textView:I = 0x7f090040


.field public static final button:I = 0x7f090041


3. create an res/values/ids.xml  and add lines there (you can skip this step if it is an Layout.xml file and go to step 4)

<item type="id" name="textView"/>

    <item type="id" name="button" />

4. if what you are adding is an layout.xml file, go to R$layout.smali and add line like this:


.field public static final activity_annoying:I = 0x7f040016

Remember to actually copy&paste the layout.xml file into the correspondent folder of the project. 


Good, use the ids in your layout.xml file and run. Notice you don't have to write the id as "@+id"
, but "@id" will do.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
  <Button android:id="@id/button" android:layout_below="@id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="107.0dip" android:text="New Button"  android:layout_marginStart="41.0dip" android:layout_alignParentStart="true" />
</RelativeLayout>

No comments:

Post a Comment