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

后端 未结 7 921
囚心锁ツ
囚心锁ツ 2020-12-05 19:32

When I use Android Studio 3.0 and I use the next version of Android Gradle Plugin in project/build.gradle:

cl         


        
相关标签:
7条回答
  • 2020-12-05 19:34

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-05 19:37

    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()
        }
    }
    
    0 讨论(0)
  • 2020-12-05 19:38

    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.

    0 讨论(0)
  • 2020-12-05 19:42

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-05 19:42

    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

    0 讨论(0)
  • 2020-12-05 19:45

    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'
            }
        }
    
    0 讨论(0)
提交回复
热议问题