How to decode Source code from .apk of Android

. apk is the executable extension in Android OS, to decode source code(java files) and its respective user interface(xml file) from it. I will show a short demo with pictures to describe it,

I took a simpleswap application’s apk file, and I done this in linux platform with debian(distro)

Part 1 – To get the source file .java of project

1) rename the existing .apk file into .zip format,

FlashLightDemo.apk -> FlashLightDemo.apk.zip

2) Extract it to give folder,with name FlashLightDemo.apk, navigate inside to it and look for classes.dex file

3) Download dex2jar convertor, extract it at safe place, copy the classes.dex into the extracted dex2jar folder. Navigate to the extracted folder using terminal

$ cd dex2jar-x.x.x.x x/

 

$ ./dex2jar.sh classes.dex

As a result you may get classes_dex2jar.jar inside the same extracted folder.

4) Download the java decompiler, else google it you will definitely get java decompiler, extract it and look for jd-gui, double click Image

and navigate to classes_dex2jar.jar, you may get source code with package names, as below

Image

Click on respective Source File and select SAVE SOURCE under File Tag, you can get all source files together in zip format,  but sometimes it becomes hard to unzip it.

Part 2 – To get XML file

1) Copy the .apk file onto new folder

2) Download apktool1.5.2.tar.bz2 and apktool-install-linux-r05-ibot-tar-bz2 put both at same folder

3) Download framework-res.apk file and put in the same folder

4) Extract apktool-install-linux-r05-ibot-tar-bz2 and apktool1.5.2.tar.bz2 

5) Copy  apktool.jar of apktool1.5.2.tar.bz2, .apk file of application and framework-res.apk inside extracted  apktool-install-linux-r05-ibot-tar-bz2 folder.

6) Open new terminal and navigate to extracted apktool-install-linux-r05-ibot-tar-bz2 folder, and type

$ ./apktool if framework-res.apk

It will install the framework.

7) And type,

$ ./apktool d "application".apk

It will decode needed, res/, AndroidManifest.xml and smali/ inside newly generated folder

Hence source file,layout, and Manifest.xml with you and Enjoy..!

Advertisements

Java

I hope this is the right time to say something about Java, it is a higher level language, java platform lies above the software platform(OS), a special type of virtual machine needed to execute java code, it is named as JVM (Java Virtual Machine). This JVM only understands bytecode, where to get such byte code..?

Software development in Java follows steps as below,

1) Install required JDK (Java Development Kit)

2) Set CLASSPATH

3) Start programming,

4) Execution,

a) convert the source code into bytecodes (understandable form of JVM),

$ javac src.java 

b) using interpreter provide bytecode into JVM, and get the output

$ java src 

I am using linux debian, I told you all what I followed.. I welcome your comments..

Now I will share some thing about Java,

Polymorphism in java

Polymorphism – Same thing with different forms…

        a) Static Polymorphism

It is compile time polymorphism,

Happens by method overloading,

// DemoPolymorphism.java

 public class DemoPolymorphism{

public int add(int a,int b){

return a+b;

}

public int add(int a,double b){

return a+(int)b;

}

public int add(double a,int b){

return (int)a+b;

}

public int add(double a,double b){

return (int)(a+b);

}

}

// methodDemo.java

public class methodDemo{

public static void main(String[] args){

DemoPolymorphism aa = new DemoPolymorphism();

System.out.println(aa.add(4,4));

System.out.println(aa.add(4.3,3));

System.out.println(aa.add(3,4.4));

System.out.println(aa.add(3.3,4.5));

}

}

        b) Dynamic Polymorphism

It is Runtime polymorphism

Happens by method overriding

// DynamicDemo.java

class Demo{

public void move(){

System.out.println("A Vehicle can move...");

}

}

class child extends Demo{

public void move(){

System.out.println("Jack and jill went upto hill");

}

}

public class dynamicDemo{

public static void main(String[] args){

Demo aa = new child();    // reference type is Demo and object being referenced to child class

aa.move();     // prints "Jack and jill went upto hill"

Demo bb = new Demo();  // reference type is Demo and object being referenced to Demo class

bb.move();   // prints "A Vehicle can move..."

}

}

Inheritance

I hope every new comer to java might struggle under standing this important concepts of java.

I hope nothing to say more about Inheritance, just see yourself you inherit your parents, like wise a class can inherit another class, results as parent and child class relationship.

public class inheritanceDemo{

public static void main(String[] args){

Child obj1 = new Child();

obj1.display();

obj1.printIt();

Parent obj2 = new Child();

obj2.printIt();

//obj2.display();

}

}

class Parent{

public void printIt(){

System.out.println("Myself Parent Class");

}

}

class Child extends Parent{

public void display(){

System.out.println("Myself Child Class");

}

}

What if I add this line

obj2.display();

just below

obj2.printIt();

.

It will throw error, since obj2 of type Parent is not aware of method ‘display()’ specific to class Child. To overcome this, we need cast object as below,

Child obj3 = (Child)obj2;

obj3.display();

And finally I can make my code lovable,

public class inheritanceDemo{

public static void main(String[] args){

Child obj1 = new Child();

obj1.display();

obj1.printIt();

Parent obj2 = new Child();

obj2.printIt();

Child obj3 = (Child)obj2;

obj3.display();

}

}

class Parent{

public void printIt(){

System.out.println("Myself Parent Class");

}

}

class Child extends Parent{

public void display(){

System.out.println("Myself Child Class");

}

}

Compile and see the output…

Myself Child Class

Myself Parent Class

Myself Parent Class

Myself Child Class

Constractor Chaining

//    classA.java

public class classA{

public classA(){

System.out.println("Jack and jill went upto hill");

}

}

//     classB.java

public class classB extends classA{

public classB(){

System.out.println("To fetch a pail of water");

}

}

//    demo.java

public class demo{

public static void main(String[] args){

new classB();        // equivalent to classB bb = new classB;

}

}

And the output is

Jack and jill went upto hill

To fetch a pail of water

Interface

Interface holds abstract classes, which can be implemented by any class for usage,

//        interfaceClass.java

public interface interfaceClass{

public void hello();
}

//        mainDemo.java

public class mainDemo{
 public static void main(String[] args){
 interfaceClass i = new dummy();

i.hello();
// i.anotherMethod(); it is not possible since type of 'i' instance(object) is interface it cannot read other methods of class(which implements interfaceClass)
 dummy j = new dummy(); // it is possible
 j.anotherMethod();
 }
}
class dummy implements interfaceClass{
 public void hello(){
 System.out.println("jack and jill went upto hill");
 }

public void anotherMethod(){
 System.out.println("I am extra method at dummy class");
 }
}

Local and Anonymous Class demo

//        helloWorldClass.java

public class helloWorldClass{

//     interface
 interface helloInterface{
 public void greet();
 public void greetSomeOne(String someOne);
 }

//     method

public void display(){

//     local class
 class helloLocalClass implements helloInterface{
 String someOne = "world";

public void greet(){
 System.out.println("Hello LocalClass greet");
 }

public void greetSomeOne(String name){
 someOne = name;
 System.out.println("Hello "+someOne);
 }
 }

//    Creating instance of interface with local class object

helloInterface obj1 = new helloLocalClass();

//    Anonymous class declaration

helloInterface obj2 = new helloInterface(){
 String someOne = "world";

public void greet(){
 System.out.println("French Greet");
 }

public void greetSomeOne(String name){
 someOne = name;
 System.out.println("Hello "+someOne);
 }
 };

//    Another Anonymous class

helloInterface obj3 = new helloInterface(){
 String someOne = "world";

public void greet(){
 System.out.println("German Greet");
 }

public void greetSomeOne(String name){
 someOne = name;
 System.out.println("Hello "+someOne);
 }
 };

 obj1.greet();
 obj2.greetSomeOne("Audhil");
 obj3.greet();
 }

//    main Method to show demo

public static void main(String[] args){
 helloWorldClass Obj = new helloWorldClass();

Obj.display();
 }
}

I welcome your comments here…

Enjoy…!