简体   繁体   中英

Delete old .apk from outputs folder when new .apk is generated via Android studio

I have a functionality which generates .apk with a custom name. (app name + date + time)

in gradle file

def getTimestamp() {
    def date = new Date()
    return date.format('_dd-yyyy-MM_HH-mm')
}

and applying of name:

defaultConfig {
    archivesBaseName = "CUSTOM_NAME_$versionName" + getTimestamp()
}

It works fine, but all generated files have a unique name and do not deleted from outputs/apk/debug/ folder. Can you please propose a way for automating of clean up for this folder. I wish only latest file always stays in the folder. Thanks

Yes, there is a way to automate this.

First step is create a Gradle task to clean the output directory:

task cleanOutputDir(type: Delete) {
   delete fileTree(dir: "build/outputs/apk/debug/")
}

Don't forget to sync Gradle before continuing.

Afterwards, you need to configure Android Studio to execute this task before running your app. To do so, click on your default build, on top of Android Studio, and click "Edit Configurations..."

在此处输入图片说明

Then, add a new before executing Gradle Task :

在此处输入图片说明

You will se a dialog requesting the task configuration as below:

在此处输入图片说明

Choose your project and module (I have 3, usually there is only one), and start typing your Gradle Task :

在此处输入图片说明

Select the task and press Ok. Then you'll have a new task at the bottom container. The last thing you need to do is move it to the top of the three tasks, as the image shows:

在此处输入图片说明

Press "Apply", "Ok", and then run your app. First will delete all content in outputs/apk/debug and immediately after will start compiling and running your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM