android studio 3.1: build:gradle:3.1.0 - Absolute path are not supported when setting an output file name

ぃ、小莉子 提交于 2019-11-28 23:25:06

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()
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!