Friday, April 24, 2015

Fields in an Interface

All fields in an Interface are automatically set to be 'public static final', means they are constants by default.


==============================================
===== FunBoy.java===================
public interface FunBoy {
int NUM = 3;
}
--------------------------------------------


=========================================
=======FunBoy.class=========

public interface FunBoy
  SourceFile: "FunBoy.java"
  minor version: 0
  major version: 50
  flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT
Constant pool:
   #1 = Class              #2             //  FunBoy
   #2 = Utf8               FunBoy
   #3 = Class              #4             //  java/lang/Object
   #4 = Utf8               java/lang/Object
   #5 = Utf8               NUM
   #6 = Utf8               I
   #7 = Utf8               ConstantValue
   #8 = Integer            3
   #9 = Utf8               SourceFile
  #10 = Utf8               FunBoy.java
{
  public static final int NUM = 3;
    Signature: I
    flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL
    ConstantValue: int 3


}

Tuesday, April 21, 2015

Ant Script that compiles an APK from Smali code, signs it, and installs it onto the phone

<?xml version="1.0"?>
<project name="build_original_apk" default="signing_apk" >
    
    
    <property name="out.packaged.file" value="/Users/liu/Work/PlayHelloWorldSmali/original-debug/dist/original-debug.apk" />
    
    <property name="name.signed.file" value="/Users/liu/Work/PlayHelloWorldSmali/original-debug/dist/original-debug-signed.apk"/>
    <property name="key.store" value="/Users/liu/Work/android.jks"/>
    <property name="key.store.password" value="123456"/>
    <property name="key.alias" value="MyAndroidKey"/>
    <property name="key.alias.password" value="123456"/>
    <property name="verbose" value="true"/>
    
    
    
    <target name="build_apk">
        <java jar="/Users/liu/Work/apktool_2.0.0rc4.jar" fork="true">
            <arg value="b"/>
            <arg path="/Users/liu/Work/PlayHelloWorldSmali/original-debug"/>
        </java>
        <echo>building original apk complete!</echo>
    </target>
    
    
    <target name="signing_apk" depends="build_apk">
        <signjar
        sigalg="MD5withRSA"
        digestalg="SHA1"
        jar="${out.packaged.file}"
        signedjar="${name.signed.file}"
        keystore="${key.store}"
        storepass="${key.store.password}"
        alias="${key.alias}"
        keypass="${key.alias.password}"
        verbose="${verbose}" />
    </target>
    
    <target name="install" depends="signing_apk">
        <exec executable="adb">
            <arg value="install"/>
            <arg path="${name.signed.file}"/>
        </exec>
    </target>
</project>



To run the install target, in the command line:


ant -buildfile build_original.xml install

Ant Script files that de-compiles an APK

<?xml version="1.0"?>
<project name="decompile_modified_apk" default="decompile_apk" >
    <target name="decompile_apk">
        <java jar="/Users/liu/Work/apktool_2.0.0rc4.jar" fork="true">
            <arg value="d"/>
            <arg path="/Users/liu/Work/PlayHelloWorldSmali/modified-debug.apk"/>
        </java>
        <echo>de-compilation complete!</echo>
    </target>
</project>

Monday, April 20, 2015

static methods is stored in Heap, perGen.

The simple answer is the heap. Classes and all of the data applying to classes (not instance data) is stored in the Permanent Generation section of the heap.

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>

Friday, April 17, 2015

Unix Command History Shortcut

ctrl + r    - search history
then either press enter to execute the command or left right arrow to display the command

history - display command history
cat .bash_history


!3  line 3
!-2 go back 2 lines
!cat search commands start with 'cat'
!! refer to the last line
!$ refer to the last line (arguments only)

Wednesday, April 15, 2015

Smali onCreate()



.local1 - this method uses one register

.param p1 - the Bundle savedInstanceState passed in as a method parameter (p0 is 'this')

line 25 invoke-super{p0, p1} - pass in 'this' and the parameter to super method

line 28 const v0, 0x7f040016 - the hex number is an int variable referring to a resource file in Android, in this case, it is a Layout.xml file.

line 30 invoke-virtual{p0, v0} - pass in 'this' and layout.xml to the method setContentView(I)V  , V means return void.

=======================================

Hex value of 0x1 is 1.

by putting '0x' in front of the number allows you to enter Hexadecimal numbers into the source code e.g. 0xFF = 255

======================================



.locals 2  - this method is using 2 registers
.param p1, "menu"  - the Menu menu that is passed in as a parameter

line 42 invoke-virtual {p0} - this.getMenuInflater()

move-result-object v0 - Move the result object reference of the previous method invocation into v0.

const/high16 v1, 0x7f0d0000 - Puts the 16 bit number into the the register v1. Used to save the R.menu.menu_main resource Id.

invoke-virtual {v0, v1, p1}, ...->inflate(ILandroid/view/Menu;)V  - this.getMenuInflater().inflate(R.menu.menu_main, menu);


const/4 v0, 0x1  - put 1 into register v0

return true

Java constructor

The parent class must have a constructor with the same signature of the subclass constructor

Tuesday, April 14, 2015

JVM Inner Class

Invokevirtual ...getClass() 之后马上 pop 掉 原来是JVM 在 check null;inner class has access to outer class 的所有 private fields原来是JVM在内部类创建了一个field this$0 通过constructor 把外部类对象传进来了, 

创建内部类的时候自动创建了一个参数是 外部类 的 constructor,
外部类实例被保存在  final Subclass this$0, 这就是非静态类会导致外部类无法被回收的原因


以下是 JVM check null paradigm