Decompile an APK, modify it and then recompile it - java

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.

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!

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
Upload apk from local machine
Wait some moments
Download source code in zip format
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.
Once you have source code , follow the step mentioned in the accepted answer
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.

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.

First download the dex2jar tool from Following link
http://code.google.com/p/dex2jar/downloads/list
Extract the file it create dex2jar folder
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
Now pick classes.dex file and put it into dex2jar folder
Now open cmd window and type the path of dex2jar folder
Now type the command dex2jar.bat classes.dex and press Enter
Now Open the dex2jar folder you found classes_dex2jar.jar file
Next you download the java decompiler tool from the following link
http://java.decompiler.free.fr/?q=jdgui
Last Step Open the file classes_dex2jar.jar in java decompiler tool now you can see apk code

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:
apk-dl.com
apkpure.com
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:

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>)

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.

This is a way:
Using apktool to decode:
$ apktool d -f {apkfile} -o {output folder}
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
https://apk.tools/tools/apk-decompiler/ (recommended)
http://www.javadecompilers.com/apk
https://www.apkdecompilers.com

Related

classes.dex file does't contain my java classes

I have developed an app in android studio and test it in my phone. I have formatted my computer and forgot to save my project to somewhere. Now i want to get my source codes back. I get the APK file from my phone (either by using adb and some other tools).
I am using dex2jar and decompile my classes.dex file but it does not show my own Java classes. Is it possible to get them back?
You can decompile it so easily a great tutorial below:
https://futurestud.io/tutorials/how-to-decompile-an-android-app-apk
4 easy steps as below:
1- unzip you apk like you unzip any zip file
2-find classes.dex files
3-convert them to jar file using dexToJar tool
4- open the jar file using JD-Gui application (availbale for windows/ubuntu/mac)
This is usually done by some obfuscator, such as Proguard. This means, among other things, that the class names of your classes will be changed into meaningless names (such as A/B/C) the names will still be different in the fully qualified path of a java class, e.g. you might have multiple ""A" classes in different packages, but other than that you will have those class names multiple times. In most cases it will not harm te functionality of your program, but it will make it harder to reverse-engineer.
If this is your app and you can rebuild it, you can disable ProGuard or a subset of its features, otherwise you will need to reverse engineer it on your own.

how to decompile a .apk without errors?

I tried the apktool, and even the online version, http://www.decompileandroid.com/
But still can't get something that works in eclipse.
I want to decompile an app called bucket drops, which was made as part of an android course. I want to look at the source code and tweak it.
When I tried decompiling the .apk file it lacked the .classpath and .project files, so I made a new project in eclipse and copied the source code into it, but for some reason eclipse shows that the app is buggy!
What do I do?
How-To: Decompile/Recompile apk's with ApkTool
Requirements:
ApkTool
Java JDK/JRE 32 bit
Basic Cmd/terminal knowledge
Download ApkTool here
ApkTool.zip
first you need to extract that zip folder to anywhere you choose, preferably on root of c/drive ie: C:/android/apktool extracting the files to 'apktool' folder
Now place the STOCK framework-res.apk & whatever other apk's you would like to modify into the 'apktool' folder
Getting to CMD directory
Open up a Cmd Prompt (terminal) and get to the 'apktool' directory ie:
Your cmd will probly look like this..
C:\users\yourname>
so type in
cd.. (yes include the '..' and press enter after you type each command)
this will get you to back directory so now you will see
C:\users>
so type again
cd..
you should now see this:
C:>
now type in:
cd 'apktoo'l/'directory'
for me it looks like:
cd android/apktool
now you should see:
C:/android/apktool>
and you are in the apktool directory :D
Click to expand...
Installing Framework
Now that you are in directory we need to install the device's framework to your system for it to compile correctly so type this:
apktool if framework-res.apk
this will install the devices framework to your system (need to do this)
Decompiling
now to decompile the apk you want to edit.. type this:
apktool d framework-res.apk framework1
or
apktool d music.apk music
music.apk is the apk your decompiling
music is the folder you're creating for the decompiled apk
now make your edits :D DO NOT Change/Edit ANY PNG's
Recompiling/Finished Product
now to recompile your new apk type this:
apktool b music music-new.apk
or
apktool b framework1 framework-res-new.apk
framework1 is the folder with your edited files
framework-res-new.apk is your new apk file :D
Now we are NOT done... we cannot use the new apk's... we have to take what we did and add it back to the original apk file to keep proper signature :D
use something like 7zip, take the 'resources.arcs' file and any other xml files you edited out of the "-new" apk and copy them into Original apk
now the apk is ready to flash with your new changes :D :D
Hope this helped some of you new themers out there!!!
Source: AndroidForums
One of my colleague has written a ruby Gem to address this problem, It can decompile an Apk and open it's code in a friendly GUI.
apkToJava is the gem name. you should try it once.
It's an easy to use tool which just take two to three commands to get an Apk decompiled.
gem install apkToJava
apkToJava setup
apkToJava /path/to/apk/file.apk
As of now its support only two operating systems Mac OS and Linux.

How to recover Gradle files from APK file?

I've succeeded in recovering the Java files and XML files from an APK file using this tutorial but I cannot find the Gradle scripts( build.gradle, settings gradle, local.properties, etc.)
I've created a new project and I had imported the Java and XML files but the project doesn't work, I guess because of the Gradle.
Does anyone know how to do this?
The gradle file is not compiled into the APK, it is only for the actual android studio project for fetching dependencies and app configuration. There is no way to get a gradle file from an APK
Try creating your own folders in the studio and try copying the code into the studio....that should work...and about gradle files? android studio will build it with gradle...as much as i know studio uses gradle build system to build app
you can’t . when you compile apk it will merge all the file into single file . An APK file contains all of that program's code (such as .dex files), resources, assets, certificates, and manifest file.
So When You decompile it . you can not file gradle file .
From what I've read in terms of Apktool vs JADX: Pros and Cons, JADX apparently decompiles APK files to Java Code and also has the option to allow you to save said Decompiled APK as a Gradle project, it has CLI version as well as a GUI version.
however I noticed that one of the Cons of JADX comes from when the APK that JADX is being used on, contains some non-ASCII characters, causing the Decompiling process to fail.
Maybe this could work for you, not sure if it will give you exactly what you want in terms of original Gradle files but it might be a step in the right direction.
You can find more information about JADX, from the creators Jadx GitHub repository, Note that it doesn't work on every APK you use it on.
Hope this helps.

How to decompile Java class files which jd-gui decompiler is unable to decompile?

We accidently got some JAVA source files deleted from the system.
This happened when We checked out a folder from repository directly as an eclipse project and then tried to copy a folder from another eclipse project into this one.Strangely all those folder files have vanished from local system (and obviously they never made it to SVN repository).
Somehow We have obtained .svn-base files for some CLASS files.Now We are trying to decompile class files to obtain the source code back. However some of the class files are giving error "invalid input file" when jd compiler is trying to process them.
Is there any other solution to this ?
From what I can tell, "jd-gui" is closed source, so you may need to contact the developer to find out what that message means. (If it was open source, we could just look at the source code to figure out what the likely problem was!)
However, one thing is clear from various questions that Google shows up, and that is that "jd-gui" doesn't understand how to decompile archives (JAR, EAR, etc). So maybe you just need to un-JAR something to make it work.
The other thing you could try (first) is to see what "javap" makes of the class files that "jd-gui" doesn't like.
Try using JAD to decompile your files.
www.varaneckas.com/jad/
You may need to unzip your jar files and then run something like the following to decompile them all
find <srcdirectory> -name *.class -exec jad -s .java -r -ff -clear -debug -d <dest directory> {} \;
I havent tested the above command but hopefully it is almost there...
I also had the error "invalid input file" with a certain class file. I was using version 0.3.6 of jd-gui. After downloading the newest version of jd-gui (which was 1.6.6 at the moment of writing this) I could decompile the class file which previously failed.
So if someone still has this problem it makes sense to check the version and upgrade if it is an older one. Here is a link to the "Latest-Release"-download page:
http://java-decompiler.github.io/

how to open an .apk file

hi i am a new developer, i created an app of my own. I created the .apk file and get it signed and aligned it. When i was running it on my device i thought of doing some changes in it, but i missed the original source code. Can i get back my source code using the aligned.apk file... I am using eclipse
The apk is just a zip file so you can unzip it. Your code will be in a classes.dex file in the dalvik format. There are some tools you can use to convert it back from dex2jar, and then decompile the class files using jad but your source code will most likely be imcomplete.

Categories

Resources