Using APK Splits for Release but not Debug build t

2020-02-18 20:52发布

I've successfully implemented APK Splits so that separate APKs are generated for different ABIs.

However, for efficiency (and since I have no need for non-armeabi-v7a APKs in Debug), I would like to limit Debug builds to only generate armeabi-v7a APKs.

How can this be done?

One idea is with this:

abi {
    enable true
    reset()
    include 'x86', 'armeabi-v7a', 'mips'
    universalApk false
}

Maybe there is some way to set enable based on the Build type?

4条回答
不美不萌又怎样
2楼-- · 2020-02-18 21:16

I'm a bit late to this party, but having a problem with different flavors and tasks names, I've come with this:

ext.isRelease = { array ->
    array.each { name ->
        if (name.contains("Debug")) {
            return false
        }
    }
    return true
}

android {

...

    splits {
        abi {
            enable isRelease(gradle.startParameter.taskNames)
            reset()
            include "x86_64", "x86", "arm64-v8a", "armeabi-v7a"
            universalApk false
        }
    }

}

It's just a small update to Jeff P's answer but works well with different flavors and build configurations.

查看更多
冷血范
3楼-- · 2020-02-18 21:36

Update for @Jeff P's answer to make it more flexible based on app name and to support Android App Bundle (.aab) format

splits {
    abi {
      enable gradle.startParameter.taskNames.any { it.contains("Release") }
      reset()
      include 'x86', 'armeabi-v7a', 'mips'
      universalApk false
    }
}
查看更多
三岁会撩人
4楼-- · 2020-02-18 21:37

You can try a variation on @Geralt_Encore's answer, which avoids the separate gradlew command. In my case, I only cared to use APK splitting to reduce the released APK file size, and I wanted to do this entirely within Android Studio.

splits {
    abi {
      enable gradle.startParameter.taskNames.any { it.contains("Release") }
      reset()
      include 'x86', 'armeabi-v7a', 'mips'
      universalApk false
    }
}

From what I've seen, the Build | Generate Signed APK menu item in Android Studio generates the APK using the assembleRelease Gradle target.

查看更多
ゆ 、 Hurt°
5楼-- · 2020-02-18 21:38

You can set enable based on command line argument. I have solved kinda similar problem when I wanted to use splits only for the release version, but not for regular debug builds.

splits {
    abi {
        enable project.hasProperty('splitApks')
        reset()
        include 'x86', 'armeabi-v7a'
    }
}

And then ./gradlew -PsplitApks assembleProdRelease (prod is a flavor in my case).

查看更多
登录 后发表回答