【问题标题】:EmptyThrowable: The APK file *.apk does not exist on diskEmptyThrowable:磁盘上不存在 APK 文件 *.apk
【发布时间】:2016-06-03 09:41:01
【问题描述】:

这是一个使用 gradle 的 Android 应用程序。点击Run后,在outputs/apk中找到了APP_V1.3.4_2016-02-22_11:30:29_google_play.apk,但是事件日志说:

11:30:31 EmptyThrowable:APK 文件 /.../WorkSpace/Android/.../app/build/outputs/apk/APP_V1.3.4_2016-02-22_11:30:14 _google_play.apk 在磁盘上不存在。

11:30:32 会话“应用程序”:安装 APK 时出错

这是我的build.gradle 文件:

apply plugin: 'com.android.application'

def releaseTime() {
    return new Date().format("yyyy-MM-dd_HH:mm:ss",
        TimeZone.getTimeZone("GMT+08:00"))
}

android {
    compileSdkVersion 'Google Inc.:Google APIs:23'
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 29
        versionName "1.3.4"
        manifestPlaceholders = [SOME_CHANNEL_VALUE: "some_channel"]
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        debug {}

        release {
            // ...
        }
    }

    buildTypes {
        debug {
            zipAlignEnabled true
            minifyEnabled false
            shrinkResources true
        }

        release {
            zipAlignEnabled true
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
            signingConfig signingConfigs.release

            applicationVariants.all { variant ->

                def time = releaseTime()

                variant.outputs.each { output ->
                    def apk = output.outputFile
                    def endFileName = "${time}_${variant.productFlavors[0].name}.apk"

                    if (apk != null &&
                        apk.name.endsWith('.apk') &&
                        !apk.name.endsWith('${endFileName}')) {
                        def fileName = "APP_V${defaultConfig.versionName}_${endFileName}"
                        output.outputFile = new File(apk.parentFile, fileName)
                    }
                }
            }
        }
    }

    productFlavors {
        google_play {
            manifestPlaceholders = [SOME_CHANNEL_VALUE: "google_play"]
        }
    }
}

dependencies {
    // ...
}

那么releaseTime()有什么问题吗?

【问题讨论】:

  • 是的,你看到确切的时间了吗?一是11:30:14。另一个是 11:30:29(这是您发现的,但与计算机预期的不同)
  • @ucsunil 感谢您的回复。我注意到两次之间的区别,但我不知道如何解决它:(我不知道gradle是如何找到apk的。
  • 建议使用 git date 代替:例如。 "git show -s --format=%ci" 以及更多内容:github.com/JakeWharton/u2020/blob/master/build.gradle 关于漂亮格式:git-scm.com/docs/pretty-formats

标签: android gradle android-gradle-plugin


【解决方案1】:

原因是 Gradle 计算了两次时间 - 一次是在构建 apk 时,另一次是在尝试安装它时。

在创建 endFileName 时不要再次计算时间。做吧:

def endFileName = time + "_${variant.productFlavors[0].name}.apk"

【讨论】:

  • 能否请您详细说明为什么它运行两次?谢谢!
  • releaseTime() 没有被调用两次,是吗?
【解决方案2】:

在我的情况下,问题是要安装的 apk 的名称被缓存了,所以每次我尝试运行应用程序时,都会生成一个带有今天日期的 apk,但是在安装时,Android Studio 会查找一个旧的 apk 文件姓名。解决方案是点击Sync Gradle,然后点击Build > Rebuild Project。您可能还想删除文件夹 app/apksapp//build/outputs/apk 之前。

【讨论】:

  • 当我尝试使用当前日期自动重命名 apk 时为我工作
【解决方案3】:

重新启动 Android Studio 对我有用。希望同样对你有用。 其他请看下面的链接。 The APK file does not exist on disk

编辑 - 如果这不起作用,请使用文件菜单中的无效缓存/重新启动。希望这会有所帮助。

【讨论】:

    【解决方案4】:

    就我而言,这是 android 插件版本的问题。我将它从 File->Project Structure->Project 更改为 Android Plugin Version 到任何以前的版本。它可以是任何版本的 Android Studio。基本上 Android 插件版本是您的 Android Studio 软件版本的版本。保持 android 插件版本与 android studio 软件版本相同不是强制性的。你可以给它你安装的任何以前版本的android studio。

    【讨论】:

    • 终于找到了对我有用的解决方案,谢谢!
    【解决方案5】:

    这发生在我身上,因为我删除了“独立 SDK 管理器”下的所有“附加”工具,并且只重新下载了必要的工具。 *我不确定这是否与问题有关。

    无论如何,我尝试了 Sync GradleRebuild Project 都没有解决问题。

    EmptyThrowable:磁盘上不存在 APK 文件 D:\...\outputs\apk\app-debug.apk。

    我通过点击解决了:

    构建 > 构建 APK

    适合我的解决方案。

    【讨论】:

    • 什么都试过了,我花了一整天的时间!!构建 > 构建 APK 做到了!难以置信!
    • 每次需要构建apk,有没有更好的解决方案?
    • @UmaAchanta 我不认为没有构建 apk 的任何替代方案。如果您担心构建 apk 所花费的时间,那么较新版本的 Android Studio 似乎大大减少了构建时间。 (根据我以前的经验,但我不记得是哪一个。)
    【解决方案6】:

    我已经尝试了所有的东西,比如

    • 之前删除文件夹 app/apks 和 app//build/outputs/apk
    • SyncGradle 和重建项目
    • lintOptions { 中止错误错误 }

    但通过点击 Build>Build APK

    解决了问题

    【讨论】:

      【解决方案7】:

      我遇到了同样的问题,上面的建议没有任何帮助:既不清理/构建也不重新启动 Android Studio/PC。问题是文件 .gradle/buildOutputCleanup/cache.properties 被锁定。只需删除 cache.properties.LOCK 文件然后清理项目即可解决问题

      【讨论】:

        猜你喜欢
        • 2016-03-06
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        • 2018-09-21
        • 2017-02-04
        • 1970-01-01
        • 2015-10-13
        相关资源
        最近更新 更多