How to define apk output directory when using gradle?

后端 未结 4 745
孤城傲影
孤城傲影 2020-12-08 08:07

How to define apk output directory when using gradle?

I would like to have possibility to upload apk to shared folder after each build.

相关标签:
4条回答
  • 2020-12-08 08:33

    I found the solution that works with the latest Gradle plugin:

    def archiveBuildTypes = ["release", "debug"];
    
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            if (variant.buildType.name in archiveBuildTypes) {
                // Update output filename
                if (variant.versionName != null) {
                    String name = "MY_APP-${variant.versionName}-${output.baseName}.apk"
                    output.outputFile = new File(output.outputFile.parent, name)
                }
                // Move output into DIST_DIRECTORY
                def taskSuffix = variant.name.capitalize()
                def assembleTaskName = "assemble${taskSuffix}"
                if (tasks.findByName(assembleTaskName)) {
                    def copyAPKTask = tasks.create(name: "archive${taskSuffix}", type: org.gradle.api.tasks.Copy) {
                        description "Archive/copy APK and mappings.txt to a versioned folder."
                        print "Copying APK&mappings.txt from: ${buildDir}\n"
                        from("${buildDir}") {
                            include "**/mapping/${variant.buildType.name}/mapping.txt"
                            include "**/apk/${output.outputFile.name}"
                        }
                        into DIST_DIRECTORY
                        eachFile { file ->
                            file.path = file.name // so we have a "flat" copy
                        }
                        includeEmptyDirs = false
                    }
                    tasks[assembleTaskName].finalizedBy = [copyAPKTask]
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 08:33

    For Gradle version 2.2.1 +, you can do this:

    def outputPathName = "app/app-release.apk"
    applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(outputPathName)
            }
        }
    
    0 讨论(0)
  • 2020-12-08 08:35

    thats work for me:

    android.applicationVariants.all { variant ->
        def outputName = // filename
        variant.outputFile = file(path_to_filename)
    }
    

    or for Gradle 2.2.1+

    android {
        applicationVariants.all { variant ->
            variant.outputs.each { output ->
                output.outputFile = new File(path_to_filename, output.outputFile.name)
            }
        }
    }
    

    but "clean" task will not drop that apk, so you should extend clean task as below:

    task cleanExtra(type: Delete) {
        delete outputPathName
    }
    
    clean.dependsOn(cleanExtra)
    

    full sample:

    apply plugin: 'android'
    
    def outputPathName = "D:\\some.apk"
    
    android {
        compileSdkVersion 19
        buildToolsVersion "19.0.3"
    
        defaultConfig {
            minSdkVersion 8
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    
        applicationVariants.all { variant ->
            variant.outputFile = file(outputPathName)
        }
    }
    
    dependencies {
        compile 'com.android.support:appcompat-v7:19.+'
        compile fileTree(dir: 'libs', include: ['*.jar'])
    }
    
    task cleanExtra(type: Delete) {
        delete outputPathName
    }
    
    clean.dependsOn(cleanExtra)
    
    0 讨论(0)
  • 2020-12-08 08:38

    This solution is work for classpath 'com.android.tools.build:gradle:3.1.2' and distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip. Put the following code inside your android scope of app-level build.gradle file. When you use command ./gradlew assembleDebug or ./gradlew assembleRelease, the output folder will be copied to the distFolder.

    // Change all of these based on your requirements
    def archiveBuildTypes = ["release", "debug"];
    def distFolder = "/Users/me/Shared Folder(Personal)/MyApplication/apk/"
    def appName = "MyApplication"
    
    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            if (variant.buildType.name in archiveBuildTypes) {
                // Update output filename
                if (variant.versionName != null) {
                    String name = "$appName-${variant.versionName}-${output.baseName}.apk"
                    outputFileName = new File(name)
                }
                def taskSuffix = variant.name.capitalize()
                def assembleTaskName = "assemble${taskSuffix}"
                if (tasks.findByName(assembleTaskName)) {
                    def copyAPKFolderTask = tasks.create(name: "archive${taskSuffix}", type: org.gradle.api.tasks.Copy) {
                        description "Archive/copy APK folder to a shared folder."
                        def sourceFolder = "$buildDir/outputs/apk/${output.baseName.replace("-", "/")}"
                        def destinationFolder = "$distFolder${output.baseName.replace("-", "/")}"
                        print "Copying APK folder from: $sourceFolder into $destinationFolder\n"
                        from(sourceFolder)
                        into destinationFolder
                        eachFile { file ->
                            file.path = file.name // so we have a "flat" copy
                        }
                        includeEmptyDirs = false
                    }
                    tasks[assembleTaskName].finalizedBy = [copyAPKFolderTask]
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题