首页 > 解决方案 > 无法减小 APK 大小

问题描述

我正在尝试通过启用minifyEnabled trueshrinkResources true在 Gradle 文件中来减小 APK 大小,并且在我构建 APK 时启用后,它的大小不会像以前一样减小。

这是我设置配置的方式:

apply plugin: 'com.android.application'

android {

compileSdkVersion 29

defaultConfig {
    applicationId "com.mobile.go"
    minSdkVersion 16
    targetSdkVersion 29
    versionCode 19
    versionName "1.18"
    multiDexEnabled true
    ndk.abiFilters 'armeabi-v7a','arm64-v8a'
}
dexOptions {
    javaMaxHeapSize "4g"
}
buildTypes {
 
    debug {
        minifyEnabled true
        shrinkResources true
        useProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

configurations.all {
    resolutionStrategy.force 'com.android.support:multidex:1.0.3'
}
packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
lintOptions {
    // Returns whether lint should be quiet (for example, not write informational messages such as paths to report files written)
    quiet true

    // Whether lint should set the exit code of the process if errors are found
    abortOnError false

    // Returns whether lint will only check for errors (ignoring warnings)
    ignoreWarnings true

    // Returns whether lint should check for fatal errors during release builds. Default is true.
    // If issues with severity "fatal" are found, the release build is aborted.
    checkReleaseBuilds false
  }

}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation project(':logging')
implementation project(':library')
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'de.greenrobot:eventbus:2.4.0'
implementation 'com.mikepenz:iconics-core:2.5.10@aar'
implementation 'com.mikepenz:google-material-typeface:2.2.0.1.original@aar'
// implementation 'com.google.android.gms:play-services-gcm:15.0.1'
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.github.amlcurran.showcaseview:library:5.4.3'
implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'de.hdodenhof:circleimageview:2.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.PhilJay:MPAndroidChart:v2.2.4'
implementation 'com.parse:parse-android:1.10.0'
implementation 'pl.bclogic:pulsator4droid:1.0.3'
implementation 'com.hedgehog.ratingbar:app:1.1.2'
implementation('com.inscripts:CometChat:7.33.+')
        {
            transitive = true;
            exclude module: 'gson'
        }
implementation('com.inscripts:CometChatUI:7.33.+')
        {
            transitive = true;
            exclude module: 'gson'
        }

implementation 'org.jsoup:jsoup:1.8.1'
implementation 'com.android.support:multidex:1.0.1'
implementation('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
    transitive = true;
}
// for push notification
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-messaging:20.0.1'
implementation 'com.google.firebase:firebase-auth:19.1.0'
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.firebase:firebase-crashlytics:17.0.0-beta01'
// Required for Flurry Analytics integration
implementation 'com.flurry.android:analytics:12.0.3'

//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'
}

repositories {
maven { url "https://jitpack.io" }
}
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

我错过了什么?

标签: android

解决方案


在上面的 gradle 代码中,minifyenabled 和 shrinkresources 对于调试构建都是正确的,请检查您是否在发布版本中构建 apk 。

减少apk大小的其他机制是将drawables转换为webp格式,如下所示。

选择drawables并右键单击以获取如下。

在此处输入图像描述

在此处输入图像描述

然后选择有损或无损编码,因为有些drawables不能用无损转换成webp。

如需更多技术,

https://medium.com/@kevalpatel2106/how-you-can-decrease-application-size-by-60-in-only-5-minutes-47eff3e7874e


推荐阅读