90

I need to modify an existing APK, modify the sources and then recompile it.

  • I can decompile it using dex2jar or apktool, it's working great
  • From the jar file I can obtain the java sources (using jd-gui)
  • Then I can modify the java files

But now I would like to know how to recompile the java files and put them back into a jar file! (the jar part should be easy, the main problem seems to be how to recompile the java files for android)

I know that an other solution is to use apktool and then modify the smali files, but it seems to be really complicated when we want to add a lot of code!

My application is a basic a HelloWorld whitout obfuscation.

1
  • 7
    Surely, you'd build the Java files the same way any Android developer would, using the Android SDK. Sep 11, 2012 at 13:06

8 Answers 8

58

Thanks to Chris Jester-Young I managed to make it work!

I think the way I managed to do it will work only on really simple projects:

  • With Dex2jar I obtained the Jar.
  • With jd-gui I convert my Jar back to Java files.
  • With apktool i got the android manifest and the resources files.

  • In Eclipse I create a new project with the same settings as the old one (checking all the information in the manifest file)

  • When the project is created I'm replacing all the resources and the manifest with the ones I obtained with apktool
  • I paste the java files I extracted from the Jar in the src folder (respecting the packages)
  • I modify those files with what I need
  • Everything is compiling!

/!\ be sure you removed the old apk from the device an error will be thrown stating that the apk signature is not the same as the old one!

10
  • 11
    this won't work on complex Applications(Dex2jar is not perfect - also some developers manage to lock their Applications down by using proguard or siilar techniques). But for some it might work
    – Ahmad
    Sep 11, 2012 at 18:37
  • Yeah you are right, this is why I said "I think the way I managed to do it will work only on really simple projects"
    – darkheir
    Sep 12, 2012 at 8:41
  • 5
    I managed to use this way on a really huge app, I think it works perfectly for some complex applications too Dec 21, 2014 at 9:30
  • @MuhammedRefaat Did you make it work on your huge app?
    – loveisbug
    Feb 5, 2015 at 11:01
  • @loveisbug yea I did, the app was not mine, and this way managed to decompile the apk but it's code was missy as the app is coded using a hybrid technique not native coding so converting it to native won't give me the perfect result but at least I could view classes and methods which I wanted to see Feb 5, 2015 at 11:22
40

I know this question is answered still, I would like to pass an information how to get source code from apk with out dexjar.

There is an online decompiler for android apks

  1. Upload apk from local machine
  2. Wait some moments
  3. Download source code in zip format
  4. Extract zip
  5. Check is the code compiles
  6. Feel free to make your changes

I don't know how reliable is this.

@darkheir Answer is the manual way to do decompile apk. It helps us to understand different phases in Apk creation.

Report so many ads on this links Another online Apk De-compiler @Andrew Rukin : http://www.javadecompilers.com/apk

Still worth. Hats Off to creators.

2
  • 40
    But now I would like to know how to recompile the java files and put them back into a jar file! was the core-question right? This isn't answering it.
    – C4d
    Feb 16, 2018 at 15:36
  • @IgorLerinc while i was answering this , there was no ads in this site will remove this from list
    – edwin
    Jan 4, 2021 at 9:52
21

The answers are already kind of outdated or not complete. This maybe works for non-protected apks (no Proguard), but nowadays nobody deploys an unprotected apk. The way I was able to modify a (my) well-protected apk (Proguard, security check which checks for "hacking tools", security check, which checks if the app is repackaged with debug mode,...) is via apktool as already mentioned by other ones here. But nobody explained, that you have to sign the app again.

apktool d app.apk
//generates a folder with smali bytecode files. 

//Do something with it.

apktool b [folder name] -o modified.apk
//generates the modified apk.

//and then
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ~/.android/debug.keystore modified.apk androiddebugkey
//signs the app the the debug key (the password is android)

//this apk can be installed on a device.

In my test, the original release apk had no logging. After I decompiled with apktool I exchanged a full byte code file without logging by a full byte code file with logging, re-compiled and signed it and I was able to install it on my device. Afterwards I was able to see the logs in Android Studio as I connected the app to it.

In my opinion, decompiling with dex2jar and JD-GUI is only helpful to get a better understanding what the classes are doing, just for reading purposes. But since everything is proguarded, I'm not sure that you can ever re-compile this half-baked Java code to a working apk. If so, please let me know. I think, the only way is to manipulate the byte code itself as mentioned in this example.

1
11
  1. First download the dex2jar tool from Following link http://code.google.com/p/dex2jar/downloads/list

  2. Extract the file it create dex2jar folder

  3. Now you pick your apk file and change its extension .apk to .zip after changing extension it seems to be zip file then extract this zip file you found classes.dex file

  4. Now pick classes.dex file and put it into dex2jar folder

  5. Now open cmd window and type the path of dex2jar folder

  6. Now type the command dex2jar.bat classes.dex and press Enter

  7. Now Open the dex2jar folder you found classes_dex2jar.jar file

  8. Next you download the java decompiler tool from the following link http://java.decompiler.free.fr/?q=jdgui

  9. Last Step Open the file classes_dex2jar.jar in java decompiler tool now you can see apk code

3
  • 7
    9.Last Step Open the file classes_dex2jar.jar in java decompiler tool now you can see apk code.How do I go reverse from here to create the apk file after modifying the class file
    – Dario Dias
    Aug 7, 2014 at 13:02
  • 2
    @DarioDias I think it can
    – loveisbug
    Feb 5, 2015 at 11:03
  • 6
    @loveisbug DarioDias asks how, not if it is possible
    – Denny
    Jun 2, 2018 at 11:43
6

I know this question has been answered and I am not trying to give better answer here. I'll just share my experience in this topic.

Once I lost my code and I had the apk file only. I decompiled it using the tool below and it made my day.

These tools MUST be used in such situation, otherwise, it is unethical and even sometimes it is illegal, (stealing somebody else's effort). So please use it wisely.

Those are my favorite tools for doing that:

javadecompilers.com

and to get the apk from google play you can google it or check out those sites:

On the date of posting this answer I tested all the links and it worked perfect for me.

NOTE: Apk Decompiling is not effective in case of proguarded code. Because Proguard shrink and obfuscates the code and rename classes to nonsense names which make it fairly hard to understand the code.

Bonus:

Basic tutorial of Using Proguard:

2

dex2jar with jd-gui will give all the java source files but they are not exactly the same. They are almost equivalent .class files (not 100%). So if you want to change the code for an apk file:

  • decompile using apktool

  • apktool will generate smali(Assembly version of dex) file for every java file with same name.

  • smali is human understandable, make changes in the relevant file, recompile using same apktool(apktool b Nw.apk <Folder Containing Modified Files>)

0
-2

I know this question is answered still and I am not trying to be smart here. I'll just want to share another method on this topic.

Download applications with apk grail

APK Grail providing the free zip file of the application.

-3

This is a way:

  1. Using apktool to decode:

     $ apktool d -f {apkfile} -o {output folder}
    
  2. Next, using JADX (at github.com/skylot/jadx)

     $ jadx -d {output folder} {apkfile}
    

2 tools extract and decompiler to same output folder.

Easy way: Using Online APK Decompiler

1
  • Careful! Flag -f deletes the entire folder.
    – mrek
    Oct 27, 2017 at 7:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.