1

How can I configure a Gradle Android project so that a release APK built by the IDE is saved to a path of my choosing (eg the project root) rather than buried deep in the build folder?

I've added this to the defaultConfig section of the app build file to sensibly name the APK and it works well, but how can I specify where it goes, or move it post build completion?

archivesBaseName = "AppName-v$versionName"  // AppName-v1.2.3-release.apk

UPDATE:

I created a task in the app-level Gradle build file that successfully copies the release APK, if I run the Gradle task manually:

task copyReleaseApk(type: Copy) {
    from 'build/outputs/apk'
    into '..' // project root, one-level above "app"
    include '**/*release.apk'
}

But I have not yet found a way to make the task run automatically after the last build task. I tried this:

assembleRelease.finalizedBy(copySupportFiles)

But that results in "Could not get unknown property 'assembleRelease' for object of type com.android.build.gradle.AppExtension."

I also tried this:

assembleRelease.finalizedBy(copySupportFiles)

It appears not to do anything.

Ollie C
  • 28,313
  • 34
  • 134
  • 217
  • [Maybe doing the copy is an option...](http://stackoverflow.com/questions/21434554/copying-apk-file-in-android-gradle-project#answer-21533902) – Selvin Apr 21 '17 at 08:53
  • Maybe, but I have no idea how to do that – Ollie C Apr 21 '17 at 13:44
  • Xavier Ducrohet's answer change `task.into(buildDir)` to `task.into(path/you/wana/apk/appear)` apply fix from comment then call `gradlew publishVariantNameApk` from command line ... where `VariantName` is `Flavor1Flavor2FlavorNBuildType` so in your example is prolly `Release` so you should call `gradlew publishReleaseApk` – Selvin Apr 21 '17 at 13:46
  • @selvin Sorry, I don't understand what you're suggesting. Can you write an answer, rather than putting code in a comment? – Ollie C Apr 21 '17 at 14:11
  • the code is in the link – Selvin Apr 21 '17 at 14:17

2 Answers2

1

This worked (in the android tag of the app build.gradle file). The afterEvaluate seems to be required in order to refer to tasks like packageRelease that don't initially exist.

task copyReleaseApk(type: Copy) {
    from 'build/outputs/apk'
    into '..' // folder above the app folder
    include '**/*release.apk'
}

afterEvaluate {
    packageRelease.finalizedBy(copyReleaseApk)
}
Ollie C
  • 28,313
  • 34
  • 134
  • 217
0

It can be defined in the project's root build.gradle:

allprojects {
    buildDir = "/path/to/build/${rootProject.name}/${project.name}"
}
Bogdan Ustyak
  • 5,639
  • 2
  • 21
  • 16
  • apk still will be *deep in build folder* `build\outputs\apk` ... question is not about changing build dir but changing path to apk file only – Selvin Apr 21 '17 at 09:14
  • Ideally, I want to specify the location of the release APK only, and leave the debug APKs where they are. Is there a way to set the buildDir on a per build type basis? – Ollie C Apr 21 '17 at 13:25