android studio - studio - ¿Cómo definir el directorio de salida de apk al usar gradle?
gradle version android studio (4)
¿Cómo definir el directorio de salida de apk al usar gradle?
Me gustaría tener la posibilidad de cargar apk en la carpeta compartida después de cada compilación.
Encontré la solución que funciona con el último complemento de 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]
}
}
}
}
Esta solución es trabajo para classpath ''com.android.tools.build:gradle:3.1.2''
y distributionUrl=https/://services.gradle.org/distributions/gradle-4.4-all.zip
. Coloque el siguiente código dentro de su alcance de build.gradle
archivo build.gradle
nivel de aplicación. Cuando utilice el comando ./gradlew assembleDebug
o ./gradlew assembleRelease
, la carpeta de salida se copiará en 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]
}
}
}
}
Para Gradle versión 2.2.1 +, puedes hacer esto:
def outputPathName = "app/app-release.apk"
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(outputPathName)
}
}
eso me funciona a mi
android.applicationVariants.all { variant ->
def outputName = // filename
variant.outputFile = file(path_to_filename)
}
o para Gradle 2.2.1+
android {
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(path_to_filename, output.outputFile.name)
}
}
}
pero la tarea "limpia" no eliminará esa apk, por lo que debe extender la tarea limpia de la siguiente manera:
task cleanExtra(type: Delete) {
delete outputPathName
}
clean.dependsOn(cleanExtra)
muestra completa
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)