How to define apk output directory when using gradle?
Asked Answered
N

4

23

How to define apk output directory when using gradle?

I would like to have possibility to upload apk to shared folder after each build.

Newmann answered 3/4, 2014 at 9:26 Comment(0)
E
25

thats work for me:

android.applicationVariants.all { variant ->
    def outputName = // filename
    variant.outputFile = file(path_to_filename)
}

or for Gradle 2.2.1+

android {
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(path_to_filename, output.outputFile.name)
        }
    }
}

but "clean" task will not drop that apk, so you should extend clean task as below:

task cleanExtra(type: Delete) {
    delete outputPathName
}

clean.dependsOn(cleanExtra)

full sample:

apply plugin: 'android'

def outputPathName = "D:\\some.apk"

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    applicationVariants.all { variant ->
        variant.outputFile = file(outputPathName)
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:19.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

task cleanExtra(type: Delete) {
    delete outputPathName
}

clean.dependsOn(cleanExtra)
Ela answered 24/4, 2014 at 22:22 Comment(6)
This doesn't work for Gradle 2.2.1+ versions: > No such property: outputFile for class: com.android.build.gradle.internal.api.ApplicationVariantImpl_DecoratedOtto
This doesn't work for Gradle 4.1: Cannot set the value of read-only property 'outputFile'Bauman
With Gradle 4.1 :- 1. instead of "variant.outputs.each" use "variant.outputs.all" and 2. instead of "variant.outputFile" use "variant.outputFileName"Rodriguez
Can't set directory with outputFileName, can you?Bauman
you cannot set a specific directory/folder with outputFileName However you can use relative paths in it (.e.g. "..\\..\\${buildType}\\${variant.flavorName}.apk"Raynold
none of the above is working. it keeps putting the apk's in top-level directories named after the build variant.Analcite
O
8

I found the solution that works with the latest Gradle plugin:

def archiveBuildTypes = ["release", "debug"];

applicationVariants.all { variant ->
    variant.outputs.each { output ->
        if (variant.buildType.name in archiveBuildTypes) {
            // Update output filename
            if (variant.versionName != null) {
                String name = "MY_APP-${variant.versionName}-${output.baseName}.apk"
                output.outputFile = new File(output.outputFile.parent, name)
            }
            // Move output into DIST_DIRECTORY
            def taskSuffix = variant.name.capitalize()
            def assembleTaskName = "assemble${taskSuffix}"
            if (tasks.findByName(assembleTaskName)) {
                def copyAPKTask = tasks.create(name: "archive${taskSuffix}", type: org.gradle.api.tasks.Copy) {
                    description "Archive/copy APK and mappings.txt to a versioned folder."
                    print "Copying APK&mappings.txt from: ${buildDir}\n"
                    from("${buildDir}") {
                        include "**/mapping/${variant.buildType.name}/mapping.txt"
                        include "**/apk/${output.outputFile.name}"
                    }
                    into DIST_DIRECTORY
                    eachFile { file ->
                        file.path = file.name // so we have a "flat" copy
                    }
                    includeEmptyDirs = false
                }
                tasks[assembleTaskName].finalizedBy = [copyAPKTask]
            }
        }
    }
}
Otto answered 15/10, 2015 at 1:46 Comment(3)
On which version is this supported? Latest gradle 4.1 reports unknown property 'archivedBuildTypes'. And next is read-only property 'outputFile'Bauman
My gradle build failed with error: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkData=Main{type=MAIN, fullName=oneOfMyVariantDebug, filters=[]}} of type com.android.build.gradle.internal.api.ApkVariantOutputImplSurvey
@Bauman I think you have typo there. It is not archivedBuildTypes but archiveBuildTypesSurvey
R
2

For Gradle version 2.2.1 +, you can do this:

def outputPathName = "app/app-release.apk"
applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(outputPathName)
        }
    }
Rafe answered 21/11, 2016 at 10:11 Comment(1)
This doesn't work for Gradle 4.1: Cannot set the value of read-only property 'outputFile'. Gradle 4.1 now saves APK in sub-folders named after the variant.Bauman
S
1

This solution is work for classpath 'com.android.tools.build:gradle:3.1.2' and distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip. Put the following code inside your android scope of app-level build.gradle file. When you use command ./gradlew assembleDebug or ./gradlew assembleRelease, the output folder will be copied to the distFolder.

// Change all of these based on your requirements
def archiveBuildTypes = ["release", "debug"];
def distFolder = "/Users/me/Shared Folder(Personal)/MyApplication/apk/"
def appName = "MyApplication"

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        if (variant.buildType.name in archiveBuildTypes) {
            // Update output filename
            if (variant.versionName != null) {
                String name = "$appName-${variant.versionName}-${output.baseName}.apk"
                outputFileName = new File(name)
            }
            def taskSuffix = variant.name.capitalize()
            def assembleTaskName = "assemble${taskSuffix}"
            if (tasks.findByName(assembleTaskName)) {
                def copyAPKFolderTask = tasks.create(name: "archive${taskSuffix}", type: org.gradle.api.tasks.Copy) {
                    description "Archive/copy APK folder to a shared folder."
                    def sourceFolder = "$buildDir/outputs/apk/${output.baseName.replace("-", "/")}"
                    def destinationFolder = "$distFolder${output.baseName.replace("-", "/")}"
                    print "Copying APK folder from: $sourceFolder into $destinationFolder\n"
                    from(sourceFolder)
                    into destinationFolder
                    eachFile { file ->
                        file.path = file.name // so we have a "flat" copy
                    }
                    includeEmptyDirs = false
                }
                tasks[assembleTaskName].finalizedBy = [copyAPKFolderTask]
            }
        }
    }
}
Survey answered 11/6, 2018 at 6:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.