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.

5
  • I've never heard of this happening before! Do you have the .class file at least? Apr 7, 2015 at 19:47
  • I can't find the .class file Apr 7, 2015 at 19:59
  • This actually happened to me TWICE. One crash from Sublime Text, one from Android Studio. WTF
    – Zyoo
    Jul 29, 2015 at 14:04
  • this happened to me very often. especially after BSOD
    – A. N
    Apr 8, 2016 at 6:59
  • See my answer here, it may help you stackoverflow.com/questions/41175409/…
    – resw67
    Jan 14, 2017 at 15:15

9 Answers 9

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.

5
  • 4
    Or probably more simple, right click file and go to local history
    – stevebot
    Apr 7, 2015 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. Apr 7, 2015 at 19:58
  • It saved my two days work... but now all imports failed
    – seema
    Sep 23, 2015 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, 2015 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 5, 2016 at 4: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 :)

7
  • Your solution saved me. Thank you so much :*
    – Alex
    Oct 6, 2015 at 18:23
  • Greate! Thank you this saved me, local history was empty! Oct 24, 2015 at 20:46
  • Thankyou!!great solution Dec 19, 2015 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'? Sep 2, 2016 at 1: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) Sep 2, 2016 at 10:42
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
1
  • Yes, this problem occur due to improper shutdown, I faced many times and it resolve by this steps. thank you. Aug 26, 2020 at 6:00
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.

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.

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

1
  • 1
    who would have thought this would actually work Apr 10, 2021 at 22:24
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

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.

1
  • It is not showing anything there too. But it says contents are identical Apr 7, 2015 at 20:02
0

Try in Android studio

Invalidate and cache/Restart ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.