当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《使用gradle时如何定义apk输出目录?》经验,为你挑选了2个好方法。

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

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



1> gerbit..:

这对我有用:

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)


这对Gradle 2.2.1+版本不起作用:>没有这样的属性:outputFile for class:com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated

2> goRGon..:

我找到了适用于最新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]
            }
        }
    }
}

推荐阅读
李桂平2402851397
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有