classes.dex file does't contain my java classes - java

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.

Related

Why does VSCode keep opening *.class files as opposed to .java files?

(I couldn't figure out how to upload my screen capture to stackoverflow. So this is a streamable link: https://streamable.com/0im8tx)
In this video, VSCode opens QueriesController.class as opposed to QueriesController.java when I cmd click into QueriesController.
I have compiled provided the definitions of the jar file in my workspace:
"settings": {
"java.project.referencedLibraries": {
"include": [
"<path-to-jar-that-contains-QueriesController.jar>",
....
"sources": {
"<path-to-jar-that-contains-QueriesController.jar>": "/my/local/java/definition/src/folder",
Does anyone know why VSCode is choosing to open the definition as a .class file rather than a .java file?
I use commands to generate a simple jar package and use it in another project. It's true that when we click the class name, .class file is opened instead of .java file:
About how to generate a executable jar package, you can have a look at this reply:
Compile .java file and generate .class;
Generate manifest and pack them into jar
In general, a JAR (Java ARchive) is a package file format typically used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file for distribution.
.java file isn't included in jar packages, and that's why you get .class file opened instead of .java file.
I am not familiar with VSCode but your problem is common across most IDEs.
Usually when a jar is made, it consists of compiled class files rather than original source codes. The reason for this is to run code as efficient and fast as possible and usually people don't want source code in jar because when running they also have to be recompiled again which is a waste of time.
Take a look at this picture. I have just downloaded a jar file from mavenrepository and it downloads the compiled version of jar. The extension is .class
What the IDE does is it tries to decompile the code with a decompiler (In this case as you can see FernFlower decompiler).
However it lacks formatting and in-code documentation the source code (.java) has. Which is why most IDEs offer to download sources. Intellij shows this right on top. Other IDEs may have this setting buried in deep. (You may have to check for yourself)
When you download sources, IDE try to contact the server and download original source code. Probably that would look something like this:
If you look closely you can see name has changed to .java which represents the source code.
VS Code has option under Java Settings, Java Download sources and Maven download sources.
It is not enabled by default. Upon enabling it, VS Code shows the proper source file, although the name appears to be .Class files.(Upon Ctrl + Clicking the symbol, with method implementations, comments, etc.,JavaDoc Comments)
If proper sources are not found in m2 repository, it shows the decompiled class file with stubbed methods. A comment similar to this is shown at the beginning of the file.
// Failed to get sources. Instead, stub sources have been generated by the disassembler.
// Implementation of methods is unavailable.
In Either of the cases, VS Code shows the maven library files as .Class files in read-only mode. Also, source files are not displayed on the Java Project Explorer.(Although even if it exists in the local .m2 repos).
Hope that helps! Happy Coding!

Using .class files in netbeans

I'm trying to use a .class file I downloaded for a uni project. I don't have the .java file to go with it. I am using netbeans.
I tried just adding it to the project src folder
I tried using "add JAR/folder" on libraries and adding the directory containing it
I also tried creating a JAR file of the directory containing it and adding that
Greatly appreciate any suggestions
You should do two things:
Create a jar of the class file you received
Create an overview which methods the classfile offers
For the latter you have at least two options. One is to use a decompiler (some authors of APIs deny you to use this) like JD-GUI. The second options is to use javap, which comes with your JDK (I link to Java 8, but it exists in prior versions too). Simply call javap yourfile.class and you will see which method signatures the class offers.
But the easiest way to see the classes / methods inside the .class file is JD-GUI, so if you are not running into any legal issues use that approach.

Working with decompiled java code

In my recent project, we are in need to modify some business rules, but we don't have source code.
I took latest production deployed jar file and opened it through JD-gui. By using option save All resources i am able to get de compiled java files. Now my questions are:
I see that decompiled java code is having unnecessary line comments on each and every lines. Is there any way to avoid them?
If i make the changes to some file and then compile the code again and send the jar to production, will there be an any hidden challenges?
1.That depend which tool you used, most of the case you must clean up by yourself.
2.Some jar file contain MD5 checksum, you must remove or update checksum in order to make your new jar file work.

Decompile an APK, modify it and then recompile it

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

how to Open source code from android apk created through adobe Air( coded in flash)?

I am trying to generate the source code from apk (which is created by adobe air, which is actually coded in flash)but It generates with no java classes and no images in res folder. The app actually a game app which has a 50 images in it. Can any body suggest me that, how to get the converted flash code into java files and the resource through the apk. I would be obliged if any suggests. Thanks in advance.
umm... you need to disassemble the APK. Did you check inside for .dex or .class files?
If yes, you might be able to use something like DJ Java Decompiler on those .class files. But android uses .DEX files instead of .class and .APK instead of .JARs.

Categories

Resources