14

I hibernated my laptop when Android Studio was running and found later that the laptop had been shut down. Now one of my source files is appearing empty in Android Studio and in Wordpad as well. When I open it in Notepad++, it shows NUL character. The file has the same size as it had before which means that the file content is not lost (hopefully).

How do I recover the actual content of this file? I need this as I didn't have a backup of this file.

cout_display_name
  • 313
  • 1
  • 2
  • 14

9 Answers9

17

See if Android Studio kept a version in local history.

Go to your java file in Android Studio, and navigate to

VCS -> Local History -> See History

Also, start using version control, like git.

Eric S.
  • 1,502
  • 13
  • 31
  • 4
    Or probably more simple, right click file and go to local history – stevebot Apr 07 '15 at 19:52
  • it says contents are identical and is showing nothing, just like in Notepad. One thing I have noticed is at the bottom of Android Studio in line separator where it normally says "CRLF", it is showing n/a for this file. – cout_display_name Apr 07 '15 at 19:58
  • It saved my two days work... but now all imports failed – seema Sep 23 '15 at 11:18
  • 3
    I made a blunder using Invalidate Caches/Restart option before this and it washed away all my local history :( Be careful – Rehan Nov 12 '15 at 10:32
  • 2
    life saver. my java file suddenly become xml file after emulator + Mac crash and auto rebooted ( I wonder why ). – neobie Nov 05 '16 at 04:06
11

This is what always works

Step 1. Search your pc for *.apk it will show location of many apks but find the app-debug.apk this is your latest build of your app/project which ram on emulator or physical device (Confirm it by checking its location. it should be in your project folder)

Step 2. After successfully find this apk rename it to zip and open in any zip or rar opener software

Step 3. After extracting you will find classes.dex in root of Extracted folder

Step 4. Download dex2jar latest version

Step 5. Drop the classes.dex file onto de2jar.bat file in dex2jar folder. This will extract a jar from it.

Step 6. Now open this jar file in jd-gui (Download here) and voila you see all the classes you ever had in your latest working project.

This is just a small return gift for all the help i have received from answers from stackoverflow community.

Happy to help :)

  • Your solution saved me. Thank you so much :* – Alex Oct 06 '15 at 18:23
  • Greate! Thank you this saved me, local history was empty! – Firas Abd Alrahman Oct 24 '15 at 20:46
  • Thankyou!!great solution – payal tuteja Dec 19 '15 at 14:13
  • The file it extracted (step 5) was a 1K file called 'classes-dex2jar.jar' which appeared in the same folder as the original 9231K 'classes-dex' file. Opening the 1K classes-dex2jar.jar with jd-gui yielded nothing (as expected for a 1K file). The de2jar.bat doesn't appear to work for me, any other options? Should the jar file be called 'classes-dex2jar.jar'? – Androidcoder Sep 02 '16 at 01:55
  • yes the generated file is named 'classes-dex2jar.jar'.try repeating steps again. or try dropping classes.dex to dex2jar.bat instead of d2j-dex2jar.bat in same folder (Although it is deprecated but may still work) – Lakhwinder Singh Dhillon Sep 02 '16 at 10:42
  • This morning I did manage to get a 9527K classes-dex2jar.jar file. My laptop is calling it type '9 Zip' file. When I drop this into the Java decoder I see a new tab labeled 'classes-dex2jar.jar', otherwise it's blank. I don't see any thing else in the window, like class file names. Is there something more to do? Opening classes-dex2jar.jar in '9 zip', which i downloaded from the windows app store, did yield several .class files, but they are in bytecode like 'b·!(±"#¶&¼*´»'. Any suggestions on reaching the lost readable source code now that I've got this far, I thought that was the point. – Androidcoder Sep 02 '16 at 14:34
  • Are you using jd-gui decompiler? or just try installing latest apk you have, to some phone/emulator and install decompiler from playstore. That doesnt need any expertise. I tried 'Show Java'. it worked well. – Lakhwinder Singh Dhillon Sep 02 '16 at 15:25
2

Simple steps that always helped me to solve this problem

Delete caches folder of the path

 C:\Users\user\.AndroidStudio3.6\system\caches.

Deleting above folder only sometimes will not work so, need to delete specific project's caches folder too

F:\AndroidStudioProjects\sampleApplication\.idea\caches
Nabin
  • 1,451
  • 3
  • 19
  • 26
1

Android Studio has a build in file history I would check that first. You will probably find your code there. You can find it if you right click your file.

If there is just a special character (that nul) that remove it with notepad++ and check if that fixes your issue.

rekire
  • 47,260
  • 30
  • 167
  • 264
1

Simplest Soultion !

  1. Open this website - http://www.javadecompilers.com
  2. Upload the APK file of your project(app-debug.apk)
  3. And here we go, you can see and download the whole project.

Note: If you want your java files, than you can find them in the folder 'com' of the parent folder of the decompiled(downloaded) folder.

mayur saini
  • 209
  • 7
  • 17
1

Close the android studio move the project to another folder from current folder and reopen app from that folder did the trick for me

viveksuggu
  • 737
  • 2
  • 7
  • 12
1

I'm not sure what makes it to happen and android studio is far the worst IDE i've worked with (how can a tool dare to edit and save the user files without even ask? the usability team of google deserve a nobel)

But this is what I do to manage solve it...

As soon as you identify an event like this happened close android studio, stop any build in progress and dont try to run the project...
it seams that sometimes the modifications aren't done in the real source file, but android studio is opening some intermediate files instead of the original sources and showing it to you when you open the source... some kind of symbolic link mess

So with android studio closed open the file explorer go to your workspace and create a copy of your project in another directory (as far of android studio as possible)

open your souce files with any text editor and check if they are still okay...

re-open android studio and copy/paste file by file, the contents of the copy project you made to the official project

It is super boring and may take a while depeding of your project size, but i managed to recover over 90% of my files like this

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
0

First: always use source control. Git and similar DVCS's don't require a server and can create a repository for you locally.

As for recovering your file, whether it's practical will be a matter of whether the disk blocks pointed to by the filesystem are correct; the fact that you're seeing \0 suggests that you're using an SSD and that the filesystem records have the right size but wrong block(s), meaning that recovery is unlikely. You should open the file with a hex editor and see whether any content past the initial few bytes is readable. If not, the file itself is almost certainly unrecoverable.

However, IntelliJ includes a local-history feature that may have an older copy of the file.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
0

Try in Android studio

Invalidate and cache/Restart ....

Community
  • 1
  • 1
madhu527
  • 4,644
  • 1
  • 28
  • 29