使用gradle时如何定义apk输出目录?
我想有可能在每次构建后将apk上传到共享文件夹.
这对我有用:
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插件的解决方案:
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] } } } }