Skip to content

Instantly share code, notes, and snippets.

@ultraon
Last active May 24, 2017 14:10
Show Gist options
  • Save ultraon/e70217c23cdbff62ef2e095587b51a83 to your computer and use it in GitHub Desktop.
Save ultraon/e70217c23cdbff62ef2e095587b51a83 to your computer and use it in GitHub Desktop.

Revisions

  1. ultraon revised this gist May 24, 2017. No changes.
  2. ultraon created this gist May 24, 2017.
    67 changes: 67 additions & 0 deletions build.gradle.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    apply plugin: 'com.android.application'
    //...

    //Adds support for splitted Apkes by abi (arch platform)
    android {
    //...

    splits {
    // Configures multiple APKs based on ABI.
    abi {

    // Enables building multiple APKs.
    enable true

    // By default all ABIs are included, so use reset() and include to specify that we only
    // want APKs for x86, armeabi-v7a, and mips.
    reset()

    // Specifies a list of ABIs that Gradle should create APKs for.
    include "x86", "armeabi-v7a", "arm64-v8a"

    // Specify that we want to also generate a universal APK that includes all ABIs.
    universalApk true
    }
    }
    }

    //Creates renameApk task (supports the android.split feature)

    import com.android.build.OutputFile

    android.applicationVariants.all { variant ->
    variant.outputs.each { output ->
    def baseAbi = output.getFilter(OutputFile.ABI)?.capitalize() ?: ""

    def baseAbiSuffix = baseAbi.isEmpty() ? baseAbi : "-${baseAbi.toLowerCase()}"
    def apkName ="my_apk_name-${variant.versionCode}-${variant.name}${baseAbiSuffix}"
    output.outputFile = file("${output.outputFile.parent}/${apkName}.apk")

    def renameApkTaskName = "renameApk"

    task "${renameApkTaskName}${variant.name.capitalize()}$baseAbi" {
    group "renaming"
    description "Renames apk file of the ${variant.name} build to apk name with buildId"
    dependsOn "assemble${variant.name.capitalize()}"

    if (!rootProject.tasks.findByName(renameApkTaskName)) {
    rootProject.tasks.create(renameApkTaskName) {
    group "renaming"
    description "Renames apk files of the builds to apk name with buildId"
    }
    }
    rootProject.tasks.findByName(renameApkTaskName).dependsOn it

    def targetApkName ="$apkName-${buildId}"
    def apkFile = output.outputFile
    def targetApkFile = file("${output.outputFile.parent}/${targetApkName}.apk")

    doLast {
    if (!apkFile.exists()) {
    throw new GradleException("$apkFile doesn't exist to rename it")
    }
    apkFile.renameTo(targetApkFile);
    }
    }
    }
    }