When I use Android Studio 3.0 and I use the next version of Android Gradle Plugin in project/build.gradle
:
classpath 'com.android.tools.build:gradle:3.0.1'
And it's work fine. After I update to Android Studio 3.1 , as result I update Android Gradle Plugin :
classpath 'com.android.tools.build:gradle:3.1.0'
And now I get error in my app/build.gradle
:
def releaseFileName = "${rootProject.name}_${defaultConfig.versionName}.apk"
outputFileName = new File(rootProject.projectDir.absolutePath + "/release", releaseFileName.toLowerCase())
Error:
Absolute path are not supported when setting an output file name
I need to put output apk (app-release.apk
) in specific path in project. In folder MyProject/release/app-relese.apk
. How I can do this?
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)
}
}
Just in case this helps, this error means that now it's not allowed to have absolute paths on anything related to the apk's file name. I can attach you my BEFORE and AFTER to achieve what I needed (to have the APK in the project/app/build/ folder:
BEFORE gradle 3.1.0
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = new File(
output.outputFile.parent,
output.outputFile.name)
}
}
IN or AFTER gradle 3.1.0
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = new File(
"./../../../../../build/",
output.outputFile.name)
}
}
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.
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
The simplest solution for Android Gradle plugin 3.4.0+ is as below:
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
}
}
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()
}
}
来源:https://stackoverflow.com/questions/49530142/android-studio-3-1-buildgradle3-1-0-absolute-path-are-not-supported-when-se