使用gradle时如何定义apk输出目录?

时间:2014-04-03 09:26:19

标签: android-studio android-gradle

使用gradle时如何定义apk输出目录?

我希望有可能在每次构建后将apk上传到共享文件夹。

4 个答案:

答案 0 :(得分:22)

这对我有用:

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

或Gradle 2.2.1+

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

但"清洁"任务不会删除该apk,因此您应该扩展干净任务,如下所示:

task cleanExtra(type: Delete) {
    delete outputPathName
}

clean.dependsOn(cleanExtra)

完整样本:

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)

答案 1 :(得分:8)

我找到了适用于最新Gradle插件的解决方案:

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]
            }
        }
    }
}

答案 2 :(得分:2)

对于Gradle版本2.2.1 +,您可以这样做:

def outputPathName = "app/app-release.apk"
applicationVariants.all { variant ->
        variant.outputs.each { output ->
            output.outputFile = new File(outputPathName)
        }
    }

答案 3 :(得分:1)

此解决方案适用于classpath 'com.android.tools.build:gradle:3.1.2'distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip。将以下代码放在app-level build.gradle文件的android范围内。使用命令./gradlew assembleDebug./gradlew assembleRelease时,输出文件夹将被复制到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]
            }
        }
    }
}