Decompile an APK, modify it and then recompile it
Asked Answered
W

8

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.

Womb answered 11/9, 2012 at 13:1 Comment(1)
Surely, you'd build the Java files the same way any Android developer would, using the Android SDK.Arneson
W
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!

Womb answered 11/9, 2012 at 13:35 Comment(10)
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 workBrit
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"Womb
I managed to use this way on a really huge app, I think it works perfectly for some complex applications tooPatrickpatrilateral
@MuhammedRefaat Did you make it work on your huge app?Breeching
@Breeching 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 seePatrickpatrilateral
@MuhammedRefaat Thank you, in my cases, there are many unreadable variables and functions, I think it cannot be re-compiled directly. :(Breeching
@Breeching yea I think it just depends on the apk type not it's size, good look in solving your problemPatrickpatrilateral
May be the creator used pro guard. This is a tool that you can use to obfuscate the code before it's packaged in the apk: developer.android.com/tools/help/proguard.htmlWomb
Is this really the current way to decompile, edit and recompile an app? There should be a script uniting these tools.Gatlin
Could it work? I have search for a long while. And the issues in Jadx mention it doesn't work due to java files which decompiled from jadx were not 1:1 mapping to apk.Houle
M
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.

Melanochroi answered 10/2, 2014 at 6:38 Comment(2)
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.Billiebilling
@IgorLerinc while i was answering this , there was no ads in this site will remove this from listMelanochroi
C
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.

Chouinard answered 15/12, 2019 at 15:24 Comment(1)
Great solution but now jarsigner doesn't support newer Android signing schemes. github.com/patrickfav/uber-apk-signer is much better to use instead of jarsigner.Retrospection
E
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

Exaction answered 11/3, 2013 at 5:28 Comment(3)
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 fileSkyla
@DarioDias I think it canBreeching
@Breeching DarioDias asks how, not if it is possibleAngelangela
R
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:

Rental answered 18/1, 2016 at 13:48 Comment(0)
T
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>)

Trachytic answered 6/6, 2015 at 6:1 Comment(0)
M
-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.

Mier answered 1/3, 2018 at 6:18 Comment(0)
B
-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

Barrio answered 15/5, 2017 at 0:54 Comment(1)
Careful! Flag -f deletes the entire folder.Voorhis

© 2022 - 2024 — McMap. All rights reserved.