Skip to content

Commit

Permalink
Strip resource names to reduce apk size
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Apr 7, 2021
1 parent 6f33dfb commit e56973d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
26 changes: 26 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.nio.file.Paths

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

Expand All @@ -11,6 +13,7 @@ if (localFile.canRead()) {

android {
compileSdkVersion target_sdk
buildToolsVersion build_tool_version
defaultConfig {
applicationId "moe.haruue.wadb"
minSdkVersion min_sdk
Expand Down Expand Up @@ -56,6 +59,29 @@ android {
dependenciesInfo.includeInApk false
}

def optimizeReleaseResources = task('optimizeReleaseResources').doLast {
def aapt2 = Paths.get(project.android.sdkDirectory.path, 'build-tools', project.android.buildToolsVersion, 'aapt2')
def zip = Paths.get(project.buildDir.path, 'intermediates',
'processed_res', 'release', 'out', "resources-release.ap_")
def optimized = new File("${zip}.opt")
def cmd = exec {
commandLine aapt2, 'optimize', '--collapse-resource-names',
'--shorten-resource-paths',
'-o', optimized, zip
ignoreExitValue false
}
if (cmd.exitValue == 0) {
delete(zip)
optimized.renameTo("$zip")
}
}

tasks.whenTaskAdded { task ->
if (task.name == 'processReleaseResources') {
task.finalizedBy optimizeReleaseResources
}
}

android.applicationVariants.all { variant ->
variant.outputs.all {
outputFileName = "wadb-v${variant.versionName}.apk"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
#Fri Oct 21 14:57:45 CST 2016
android.useAndroidX=true
android.debug.obsoleteApi=true
android.enableResourceOptimizations=true
android.enableResourceOptimizations=false
3 changes: 2 additions & 1 deletion manifest.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ext {
gradle_plugin_version = '4.1.2'
gradle_plugin_version = '4.1.3'
kotlin_version = '1.4.32'
build_tool_version = '30.0.3'

min_sdk = 23
target_sdk = 30
Expand Down

0 comments on commit e56973d

Please sign in to comment.