ChatGPT解决这个技术问题 Extra ChatGPT

The APK file does not exist on disk

When I am trying debug application on Android Studio gives this log output :

The APK file /Users/MyApplicationName/app/build/outputs/apk/app-debug.apk does not exist on disk.

I restarted Android Studio, but I can't solve this problem . How can I solve it ? Thank you

How can I solve it ? compile an apk
@TimCastelijns I actually wonder if Android Studio is not intelligent enough to include the create debug apk task into the deb application task. Gradle should be made for this.
@Trilarion By default AS executes gradle's assembleDebug task which compiles a debug apk. Something seems wrong here, which requires manual compilation
Adding Gradle Make-Aware fixed this issue for me. stackoverflow.com/a/25871525/1712417
Try to add "Gradle Make-Aware" stackoverflow.com/questions/18256177/…

A
Askr

Click this option to solve the error:

https://i.stack.imgur.com/2KSyU.png


Special characters in your project directory name will also throw this error.
This didn't work for me at all. I would advise answers that explain why they are supposed to work rather than just telling the reader what to do, so that they can understand / avoid / solve those errors later.
Would you explain what does this exactly do and why this problem happened?
btw "Special characters" also include numbers. It's ridiculous, wasted tons of time on this. facepalm :)
That button doesn't exist on the latest version of Android Studio, but I believe it's the "Sync Project With Gradle Files" option, which did work for me.
P
Prateek

If you are facing this issue after the update to Android Studio V 3.1 then try the below.

I was facing this same problem when I updated to Android Studio 3.1. All other solutions I found here were quite manual because you have to clean and rebuild every time, that is not good at all. But thanks to Iman Marashi's answer here I was able to solve it.

Go to Run -> Edit Configurations...

Make sure you have a "Gradle-aware Make" on the Before launch section:

https://i.stack.imgur.com/9YoVI.png

To add it click the + sign and select Gradle-aware Make and a popup will appear, just leave the text field empty and click OK and OK.


This is the fix for the recent 3.1 update. For me, the first symptom of this issue was the APK not "responding" to changes in the code. It's now clear to me that the new APK wasn't building properly at all.
This answer can solve for me. I updated 3.1 and get this error.
You sire are a life saver.
WOW! You deserve 1000 up votes for this! Android Studio deserves 1000 down votes!
Thanks, this is definitely the right answer and should be the accepted one
B
Bartek

In my case, executing "Sync Project with Gradle Files" helped. Neither restarting Andoid Studio nor clearing the cache did anything.


Thanks. Its help me, too. Neither restarting Andoid Studio nor clearing the cache didn't help.
Did the trick for me after changing the naming script which is in a separate file from my normal build.gradle, so Android Studio didn't automatically pick up on the change and prompt me to sync.
This is the real answer.
File -> Sync Project with Gradle Files stackoverflow.com/a/19932896/2527204
Had this issue trying to run a Unit test. It was asking for a device to run a Unit test.
J
Jing Li

If you just want to know the conclusion, please go to the last section. Thanks.

Usually when building project fails, some common tricks you could try:

Build -> Clean Project

Check Build Variants

Restart Android Studio (as you mentioned)

But to be more specific to your problem - when Android Studio could not find the APK file on disk. It means that Android Studio has actually successfully built the project, and also generated the APK, however, for some reason, Android Studio is not able to find the file.

In this case, please check the printed directory according to the log. It's helpful.

For example:

With Android Studio 2.0 Preview (build 143.2443734).

Checkout to a specific commit (so that it's detached from head): git checkout [commit_hash] Run project Android Studio tells: The APK file /Users/MyApplicationName/app/build/outputs/apk/app-debug-HEAD.apk does not exist on disk Go to the directory, there is a file actually named: app-debug-(HEAD.apk (with an extra parenthesis) Run git branch *(HEAD detached at 1a2bfff)

So here you could see, due to my gradle build script's mistake, file naming is somehow wrong.

Above example is just one scenario which could lead to the same issue, but not necessary to be the same root cause as yours.

As a result, I strongly recommend you to check the directory (to find the difference), and check your build.gradle script (you may change the apk name there, something like below):

applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def newFileName = "whatever you want to name it";
            def apk = output.outputFile;
            output.outputFile = new File(apk.parentFile, newFileName);
        }
    }

My issue was that I was adding the app version in the APK name but the app version has x.y.z format and the "." (dot) in the naming was causing the errors. So remember no other dots but the one before apk :)
R
Rim Gazzah

Make sure that you don't have apostrophe or & in your path


The '&' was what was keeping me from compiling. Thanks @Rim Gazzeh
I
Iman Marashi

Make sure that there is Grading-aware Make in Run/Debugging configurations >> before launch section :

https://i.stack.imgur.com/N1qMW.png


u
user213493

I witnessed a similar issue usually when I'm switching git branches. For me shutting down Android Studio rm -rfv ~/Library/Caches/AndroidStudio* and restart Android Studio is the ticket.


My similar issue was solved by this method. I rename apk files in the gradle build to append the version number to the file, trying to debug my main activity was trying to run the wrong apk file name (previous version number). Not sure when this started but it was working fine until it didn't.
Android studio cache it is very big problem!
It is. I have just (again) incremented my build number, this changes the apk file name, again I have this problem.
l
live-love

In Android Studio 3.1.1 select File | Sync Project with Gradle Files.

In Android Studio 3, select File | Synchronize.

Then run your project again.


In Studio v3.1.1 , File -> Sync Project with Gradle Files. and you are good to go
Then in a field on top toolbar(with icones), in next from hammer icon you have to return 'app ' back, in configuration dropdown
A
Androiderson

The year is 2018. Android Studio version is 3.2.0 alpha3 and we still get this problem. As none of the above worked for me, here is my 2 cents:

Every time this happens I just generate the apk from command line:

gradlew assembleDebug

UPDATE

Deleting the folder .AndroidStudio[version] and losing all your preferences and caches... seems to solve the problem


but this only works for one build? if you make further changes and press the 'play' button, it just pushes the old apk and doesn't rebuild for the changes. So annoying, wtf were they thinking
I
Immortal Code

In my case, I was using a special character in my app file path. I closed the Android Studio and removed the ' character from my app file path. Everything worked fine when I reopned the project.


Yeah ideally Android Studio should be smart enough to prevent users from using special character in their file path. Or better I should have followed the good programming practices myself :)
M
Mun0n

I've solved building an apk using the Build option from the top window and Build APK. No need to do something weird.


J
Jean-Marc Delafont

For Unix like users (Linux/MacOs X), instead of removing ~/.gradle/caches/, in command line do :

$ cd path_to_you_project
$ touch build.gradle

Then ask Android Studio to build APK, it will reset gradle cache itself.


L
Lou Morda

My problem is that I was appending the version number to the APK. Changing the version number and re-syncing Gradle fixed the problem for me.

def appendVersionNameVersionCode(variant, defaultConfig) {
    variant.outputs.each { output ->
        if (output.zipAlign) {
            def file = output.outputFile
            def removeApp = file.name.replace("app-", "")
            def removeType = removeApp.replace("-release", "")
            def fileName = removeType.replace(".apk", "." + defaultConfig.versionName + ".apk")
            output.outputFile = new File(file.parent, fileName)
        }

        def file = output.packageApplication.outputFile
        def removeApp = file.name.replace("app-", "")
        def removeType = removeApp.replace("-release", "")
        def fileName = removeType.replace(".apk", "." + defaultConfig.versionName + ".apk")
        output.packageApplication.outputFile = new File(file.parent, fileName)
    }
}

Have you called appendVersionNameVersionCode in gradle itself?
K
Kadir altınok

Build ---> Build APK(s) and be sure it works


d
dvrm

File -> Invalidate Caches / Restart


A
AnZ

If you tried all the above answers and it didn't work try to disable "Instant Run" feature. This one helped me after all attempts.

File -> Settings -> Build, Execution, Deployment -> Instant Run -> Uncheck checkbox there


u
u_pendra

I also got that issue, after cleaning the build. For me solution was I just sync the gradle and it worked for me.


M
MiguelSlv

In my case the problem was that debugging configuration become invalid somehow, even that the test method name, class or package has not changed.

I deleted the configurations from Run->Edit Configurations, at here:

https://i.stack.imgur.com/blhP0.png

Android studio will create a new one automatically.


Then you need to click on File / Re-Import Gradle project. This will recreate the deleted run configuration.
G
GiridharaSPK

Sync, Clean, Rebuild and Invalidate Cache and Restart

None of them worked for me.

I have deleted the APK and output.json files in the release folder and debug folder and then tried sync, clean, and build, then it worked fine.

OR simply Delete folder which has the apk and output.json file (in case of debug/ release)


S
Simon PA

Faced the same issue with gradle 1.5

I had to clean the build files:

Build -> Clean project

And build an APK to force the full compilation and sync of the gradle files:

Build -> Build APK

I still don't know why or how it happened tho.


C
Catalin Clabescu

if you are using Linux, try setting write permission to the app/build folder.


O
Orientos

i had the same problem. it was due to false name in the path. there was a special Character in the path like this: C:\User\My App\Projekte-Tablet&Handy i deleted the "&" character and it worked well.


I
Ian Spencer

My problem was including minutes in the file name - it looked for appname_debug_0.9.0.1_170214_2216.apk when the generated file was appname_debug_0.9.0.1_170214_2217.apk so the output filename code (nabbed from elsewhere) was clearly being called from two different points in the build.

applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def project = "appname"
            def SEP = "_"
    //        def flavor = variant.productFlavors[0].name
            def buildType = variant.variantData.variantConfiguration.buildType.name
            def version = variant.versionName
            def date = new Date();
            def formattedDate = date.format('yyMMdd_HHmm')

            def newApkName = project + SEP + /*flavor + */ SEP + buildType + SEP + version + SEP + formattedDate + ".apk"

            output.outputFile = new File(output.outputFile.parent, newApkName)
        }
    }

where is this block from?
It was from some other stack exchange thread from when I was first setting up the project (and had very little idea what I was doing - nothing changed there). As this part of Gradle has been changed a few times it is likely obsolete code now.
B
Boldijar Paul

The problem for me was that somehow it could really not find the apk, while the apk exists.

The APK file /Users/Paul/AndroidStudioProjects/android-tv-launcher/ui/build/outputs/apk/nameofapk.apk does not exist on disk.
Error while Installing APK

All I had to do, was right click on the apk-copy relative path, and just

adb install -r <paste of relative path>

And the apk was installed and good to go. This issue happened on OSX for me.


G
Gerardo Suarez

Solved in may of 2018 with the new Android Studio 3.1. Delete the .gradle, .idea and build directories inside your app folder. Then try to run it again and you won't see the error.


凤清云

I change build.gradle file a litle info,and click "Sync Now",just ok.


C
Community

Remove the directory ~/.gradle/caches/. It's OK to do this while Android Studio is opened. (Refer pm installing wrong package name) Press "Sync project with Gradle files" icon on Android Studio Run the project and the remote path will be correct.

Refer answer @ Android Studio gets package name wrong when running .apk


A
Amgad

Make sure that in the path generated in your logs -in your case:

/Users/MyApplicationName/app/build/outputs/apk/app-debug.apk

folder "outputs" indeed has an "apk" folder.

That was my problem, if it's not there, you will need to run the "assembleRelease" task in gradle by following the below screenshot;

https://i.stack.imgur.com/3nPDu.png


D
Dmytro Karataiev

Nothing above helped me, but I figured this out by switching to another flavor in the Build Tools panel and then switching back to the needed one.


兰坡阳

modify the file:build.gradle(app directory). in fact:you should not change this file , only by this to get sync tips,then sync project.