3

I was creating a app for Android, it was nearly complete but now the hard drive crashed and I lost all my data along with the source code for my app. But, Luckily I had installed that app on my phone so I got the .apk file for my app and also decompiled the file using the steps mentioned in this article, Is there a way to get the source code from an APK file?

But I am having one problem, the source has changed a bit, at some places where I had mentioned some variable names are replaced by some digits. I am attaching the Screenshot of the code so that you can have a proper look at the problem.

Can anyone tell me how can I solve the problem and recover the original code which I wrote. Because I can't waste the time in writing the whole code again.

I was not able to upload the screenshot here, so I am pasting the link to the photo.

enter image description here

Community
  • 1
  • 1
Tapan Desai
  • 848
  • 2
  • 14
  • 36
  • possible duplicate of [Android: Getting source code from an APK file](http://stackoverflow.com/questions/3593420/android-getting-source-code-from-an-apk-file) – t0mm13b Aug 16 '12 at 01:04

2 Answers2

3

You cannot recover the exact source code from an APK; it's not there. The code you have recovered is the best you can do -- it's an approximation based on the compiled code.

Based on the screenshot of what you've recovered, you're very fortunate to have something at that high a level.

mah
  • 39,056
  • 9
  • 76
  • 93
  • but why the variable names were replaced by some numbers? – Tapan Desai Aug 15 '12 at 11:15
  • because the variables are static numbers that the android SDK automatically builds during compile. Those numbers are the IDs you find on R.id.something_something or R.layout.myLayout. You can either re-start the project from scratch and using this recovered code as a basis for faster development, or try to re-create the XMLs and manually replace the IDs. – Budius Aug 15 '12 at 11:16
  • so is there any way to get the value/name of the variable which relates to that ID? – Tapan Desai Aug 15 '12 at 11:19
  • @Budius actually he will not have to recreate the xml files; the apktool he used to get this far recovered them from the apk. The only thing he might need that apktool didn't do for xml's / assets is restore comments. – mah Aug 15 '12 at 11:19
  • @user1164575 see if you have a source file named .R.java; if you do, it will hold many (but perhaps not all) of the constants. – mah Aug 15 '12 at 11:22
  • @mah I dont have .R.java, so now what can i do? – Tapan Desai Aug 15 '12 at 11:23
  • @Budius you said the following line "or try to re-create the XMLs and manually replace the IDs." but how can i get the variable name? its fair that i'll not remember all of those. – Tapan Desai Aug 15 '12 at 11:27
  • @user1164575 about the only thing left to do then is learn from a horrible experience. You're not going to be able to recover the pieces that are no longer there. – mah Aug 15 '12 at 11:29
  • @Mah so does that mean i need to start from scratch again? – Tapan Desai Aug 15 '12 at 11:30
  • see @mah comment to me. Apparently those XML are already somewhere on your recovered code. And the ID names don't have to be the same. you can create whatever you want. As long as in the final project XML and Code names matches, the R. will be re-created anyway. – Budius Aug 15 '12 at 11:30
  • @Budius can you help me on this? – Tapan Desai Aug 15 '12 at 11:32
  • I reckon that for a cleaner final result restart from scratch and copying pieces of the restored code into the project is the way to go. But that's purely my opinion. And yeah, learn from mistakes, Subversion and Areca exist exactly for that. And sorry, there's not much else I can do to help. – Budius Aug 15 '12 at 11:34
  • @Tapan Desai -- starting from scratch is much more difficult than starting from most of the code. With the Java that you've got, even if it isn't quite able to compile (which is sometimes the case with reconstructed code), you have a lot to go on to rebuild your source. It's not going to be automatic and it's not going to be trivial -- it's going to take real effort on your part. If you wrote it in the first place, even if it was a long time ago, you'll be able to complete this slow process, and you may even improve your program while you do it. – mah Aug 15 '12 at 11:34
  • @Budius you advised me to reconstruct the XML's and manually replace the ID's but in which file i will find those.? – Tapan Desai Aug 15 '12 at 11:39
  • when you're building a layout (for example) you put a and set its ID to myTextView (for example) then you go to the code `(TextView)findViewById(2343454)` and replace this number by R.id.myTextView. Got it? – Budius Aug 15 '12 at 11:41
  • I +1 everything @mad said, it's not going to be trivial, not going to be automatic, it will take some time and it will take a real effort from you. But remember to have separate backups from now on. – Budius Aug 15 '12 at 11:44
2

Use this to convert it to JAR and then this to get the code.

The problem with the variables could be caused by obfuscator - ProGuard. There is a way to de-obfuscate it, but as mentioned above, you have gotten very very far and you are lucky to have what you have.

EDIT:

From official developer site:

Caution: Every time you run a build in release mode, these files are overwritten with the latest files generated by ProGuard. Save a copy of them each time you release your application in order to de-obfuscate bug reports from your release builds.

I am not sure you can de-obfuscate it withou the original ProGuard settings...

Michal
  • 15,429
  • 10
  • 73
  • 104