【问题标题】:Gradle script rename file problemsGradle脚本重命名文件问题
【发布时间】:2015-07-09 11:03:25
【问题描述】:

我使用下面的代码生成一个 .apk 文件,它工作正常。 但是,为了能够调试,我需要在“applicationVariants.all”周围注释代码,否则 Android Studio 会说找不到该文件。

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def apk = output.outputFile;
                    def newName = "app-release-" + getDate() + ".apk";
                    output.outputFile = new File(apk.parentFile, newName);
                }
            }

        }
    }

我该如何做才能使其生成 .apk 文件并在 Android Studio 上进行调试?

更新

我发现发生了什么,实际上当我在文件名中使用日期和时间时,生成文件中的时间与 Android Studio 尝试安装的时间不同。

我的函数 getDate() 返回:

def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyyMMddHHmm')
    return formattedDate
}

创建的文件是 app-release-201507110957.apk。 但是,在 Android Studio 控制台中,错误是:

Uploading file
    local path: /Volumes/Macintosh HD/AndroidstudioProjects/App/app/build/outputs/apk/app-release-201507110956.apk
    remote path: /data/local/tmp/com.domain.app
Local path doesn't exist.

生成文件上的文件名比 Android Studio 尝试安装的文件名提前 1 分钟。 关于如何解决这个问题的任何想法?我想在文件名中包含小时和分钟,因为我可能每天为 QA 团队生成多个版本。

【问题讨论】:

  • applicationVariants 不是buildTypes.release 的成员...它是buildTypes 的兄弟

标签: android android-studio build.gradle


【解决方案1】:

目前,重命名是您发布版本的一部分。只需将重命名作为一般操作,如下所示:

android {

  ...

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    debug {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

  applicationVariants.all { variant ->
      variant.outputs.each { output ->
      def apk = output.outputFile;
      def newName = "app-release-" + getDate() + ".apk";
      output.outputFile = new File(apk.parentFile, newName);
    }
  }
}

【讨论】:

  • 还是同样的问题:“上传文件本地路径:/Volumes/Macintosh HD/AndroidstudioProjects/App/app/build/outputs/apk/app-release-201507091037.apk远程路径:/data/local /tmp/com.domain.myapp 本地路径不存在。”
  • 您是否尝试像添加发布类型一样添加调试构建类型(请参阅我更新的答案)。
  • 我发现了正在发生的事情。请查看我更新的问题。
【解决方案2】:

我通过验证变体是否可调试解决了我的问题。

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            applicationVariants.all { variant ->
                if (!variant.buildType.isDebuggable()) {
                    variant.outputs.each { output ->
                        def apk = output.outputFile;
                        def newName = "app-release-" + getDate() + ".apk";
                        output.outputFile = new File(apk.parentFile, newName);
                    }
                }
            }
        }
    }

就像我只将我的文件名应用于发布版本。可调试的将继续使用Android Studio设置的相同名称,因此在调试应用时不会产生问题。

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 2017-10-26
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多