0

I want to set APK filePath in gradle that version is v4.1,but I can set its name and not set file path. What do I do?

applicationVariants.all {
    variant->
        variant.outputs.all {
            output->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith('.apk')) {
                    outputFileName = "${defaultConfig.applicationId}_${variant.productFlavors[0].name}_${defaultConfig.versionName}_${releaseTime()}.apk"
                }
        }
}
azizbekian
  • 60,783
  • 13
  • 169
  • 249
Mr.Four
  • 67
  • 10

2 Answers2

1

From this migration guide,

Using the Variant API to manipulate variant outputs is broken with the new plugin. It still works for simple tasks, such as changing the APK name during build time.

However, more complicated tasks that involve accessing outputFile objects no longer work. That's because variant-specific tasks are no longer created during the configuration stage.

What you already achieved is the ONLY thing that we can do with APK at build time.

You can workaround by moving the files to another location after build completion using Gradle copy task:

task copySupportFiles(type: Copy){
    from 'src/main/support'
    into 'build/outputs/apk'
    include '**/*.dat'
    include '**/.txt'
}

assembleDebug {}.doLast{
    tasks.copySupportFiles.execute()
}
nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
  • First,thanks you answer. I see some blogs about it .That create a array that add the folder,then foreach fileArray,copy to a new folder . Fortunately, I found a better method. – Mr.Four Dec 08 '17 at 04:18
  • @Mr.Four which one? can you share it with me? But my answer is working right? – nhoxbypass Dec 08 '17 at 04:19
  • I have posted the method ,but it may be deprecated in gradle 5.0+. – Mr.Four Dec 08 '17 at 04:21
1

I've figured out solution for this:

applicationVariants.all{
            variant->
                //Android Studio 3.0+
                variant.outputs.all{
                    outputFileName = "${defaultConfig.applicationId}_${variant.productFlavors[0].name}_${defaultConfig.versionName}_${releaseTime()}.apk"
                    variant.packageApplication.outputDirectory = new File("xx/yyy/ttt")

                }
        }

But It may be deprecated in gradle 5.0+,because variant.packageApplication.outputDirectory will be deleted in gradle 5.0. And you see a warning that: access outputDirectory exceeds its access rights

nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
Mr.Four
  • 67
  • 10