Android Studio 3.1升级Gradle插件3.1.0后输出文件名绝对路径报错
Hey there! I’ve run into this exact snag when upgrading from Gradle Plugin 3.0.1 to 3.1.0, so let’s break down what’s going on and how to fix it.
What’s Causing the Error?
Starting with Gradle Plugin 3.1.0, the Android team added a restriction: you can no longer use absolute paths when setting outputFileName directly. Your old configuration worked in 3.0.1 because this check wasn’t enforced yet.
Solution 1: Use Relative Paths (Quickest Fix)
Instead of pointing to your root project with an absolute path, switch to a relative path. Since your app module lives inside the root project, you can reference the root directory with ../:
android { defaultConfig { // ... your existing version and app config } buildTypes { release { def releaseFileName = "${rootProject.name}_${defaultConfig.versionName}.apk" // Use relative path to drop the APK directly into the root project outputFileName = new File("../" + releaseFileName) } } }
This will place your APK right in the root project folder, just like your original setup intended.
Solution 2: Explicitly Set Output Directory (More Flexible)
If you want to organize your APKs into a dedicated folder (like a releases directory in the root project), use applicationVariants to adjust both the file name and output location:
android { defaultConfig { // ... your existing version and app config } buildTypes { release { def releaseFileName = "${rootProject.name}_${defaultConfig.versionName}.apk" applicationVariants.all { variant -> variant.outputs.all { outputFileName = releaseFileName // Create a "releases" folder in the root project (if it doesn't exist) def releaseDir = new File(rootProject.rootDir, "releases") releaseDir.mkdirs() // Set the output file to the new directory output.setOutputFile(new File(releaseDir, releaseFileName)) } } } } }
This approach keeps your build outputs organized while avoiding the absolute path restriction.
Why This Change Happened?
The Gradle team added this rule to promote more portable build scripts. Absolute paths can cause issues with shared projects, CI/CD pipelines, or when moving the project to a different location on disk. Using relative paths or explicit directory setups makes your build more reliable across environments.
内容的提问来源于stack exchange,提问作者Alexei

