When I use Android Studio 3.0 and I use the next version of Android Gradle Plugin in project/build.gradle
:
cl
I think using "./../../../" is bad solution... I use common gradle script for several projects and I want to make code to be independency from depth of output dir.
After some researching I found this solution for gradle plugin 3.1.2:
applicationVariants.all { variant ->
variant.outputs.all { output ->
def relativeRootDir = output.packageApplication.outputDirectory.toPath()
.relativize(rootDir.toPath()).toFile()
output.outputFileName = new File( "$relativeRootDir/release", newOutputName)
}
}
Use variant.packageApplicationProvider.get().outputDirectory
to avoid warning "variantOutput.getPackageApplication() is obsolete"
applicationVariants.all { variant ->
variant.outputs.each { output ->
variant.packageApplicationProvider.get().outputDirectory = new File(
project.rootDir.absolutePath + "/release")
def releaseFileName = "${rootProject.name}_${defaultConfig.versionName}.apk"
output.outputFileName = releaseFileName.toLowerCase()
}
}
I experienced the same issue. I haven't put the effort in to figure out exactly what's happened but there's a simple fix.
Just remove the root from your new file and trust the framework, i.e. change your code to
outputFileName = new File("release", releaseFileName.toLowerCase())
That was sufficient for us. We don't care about where the apk goes, only the name of the apk for a particular flavour.
Assemble/concatenate your new file name:
def newFileName = "assemble-your-new-file-name"
Then, simply assign it back.
outputFileName = newFileName
More specifically is as following
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
// Redirect your apks to new defined location to outputDirPath
def outputDirPath = new File("${project.rootDir.absolutePath}/apks/${variant.flavorName}/${variant.buildType.name}")
variant.packageApplicationProvider.get().outputDirectory = outputDirPath
def apkFileName = "${rootProject.name}_${android.defaultConfig.versionName}.apk"
output.outputFileName = apkFileName // directly assign the new name back to outputFileName
}
}
I found solution:
def releaseFileName = "${rootProject.name}_${defaultConfig.versionName}.apk"
outputFileName = "/../../../../../release/" + releaseFileName.toLowerCase()
And now output file app-release.apk
success created in MyProject/release/app-relese.apk
If you want to append the version name and version code do it like:
applicationVariants.all { variant ->
variant.outputs.all {
def versionName = variant.versionName
def versionCode = variant.versionCode
def variantName = variant.name
outputFileName = "${rootProject.name}" + '_' + variantName + '_' + versionName + '_' + versionCode + '.apk'
}
}