如何减小 apk 大小(应用程序大小太大)

How to reduce the apk size (App size too big)

检查下面我的 gradle 配置。

    android {
    signingConfigs {
        config {
            keyAlias 'xxx'
            keyPassword 'xxx'
            storeFile file('/home/xxx/AndroidStudioProjects/key/key.jks')
            storePassword 'xxx'
        }
    }
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "app.examinations.com"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            debuggable false
            signingConfig signingConfigs.config
        }
        debug {
            signingConfig signingConfigs.config
        }
    }
}
repositories {

    maven { url "https://jitpack.io" }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    //Gson Dependency
    compile 'com.google.code.gson:gson:2.8.0'
    //Design Layout
    compile 'com.android.support:design:25.3.1'
    //chart
    compile 'com.diogobernardino:williamchart:2.5.0'
    //view
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    //fab
    compile 'com.github.clans:fab:1.6.4'
    //expandable view
    compile 'com.github.aakira:expandable-layout:1.6.0@aar'
    //tables
    compile 'de.codecrafters.tableview:tableview:2.6.0'
    //Circular ImageView
    compile 'com.mikhaellopez:circularimageview:3.0.2'
    //Download Images with Picasso
    compile 'com.squareup.picasso:picasso:2.5.2'
    //Google Services
    compile 'com.google.firebase:firebase-messaging:11.0.2'
    compile 'com.google.android.gms:play-services-auth:11.0.2'
    compile 'com.google.firebase:firebase-core:11.0.2'
    //Crop Image
    compile 'com.github.yalantis:ucrop:2.2.1'
    //Pdf Viewer
    compile 'com.github.barteksc:android-pdf-viewer:2.7.0-beta'
    //Fab reveal layout
    compile 'konifar:fab-transformation:1.0.0'
    testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

我已经使用了 proguard 和资源缩减它仍然是 25mb(大约)。

这是 proguard 配置

-keepattributes SourceFile,LineNumberTable
-keep class com.parse.*{ *; }
-dontwarn com.parse.**
-dontwarn okio.**
-dontwarn com.squareup.picasso.**
-keepclasseswithmembernames class * {
    native <methods>;
}

-assumenosideeffects class android.util.Log { *; }

检查此 Apk 分析器图像

查询

1) 这个 lib 文件夹是否包含我所有的库?为什么它太大了?

2) 有什么方法可以只使用我从使用混淆器的库中使用的 class 吗?或者我应该通过使用更少的库来优化大小?

1) lib 文件夹包含您的库。它很大,因为你使用了很多库。

2) 你应该也能优化库,但有些已经优化了


我不明白你为什么说应用程序太大。 Google Play 最多允许 APK 100 MB。

减小尺寸的最佳方法是减小图像尺寸。看起来你没有任何可以削减的依赖项(google play API 被分成几个实例,如果你导入了整个东西但只使用了 Google玩游戏,你可以删除依赖项,只导入玩游戏。这与你的情况无关,这是一个例子)。

这里有一些关于减小 Apk 大小的有用提示:

  • 使用ProGuard
  • 使用shrinkResources
  • 优化您的依赖项。例如,不要将所有 Google Play 库添加到您的项目中。只需添加您实际需要的那些。
  • 使用SplitApk
  • 使用 Facebook 优化您的最终 Apk ReDEX
  • 使用工具(例如:Lint)查找未使用的布局和其他资源并将其删除。
  • 使用Vector Drawables
  • 使用 resConfigs
  • 删除不需要的本地化配置
  • 使用 debugCompile
  • 编译调试库
  • 如果您的目标是 Android 3.2 或更高版本,您可以使用 WebP 图像文件格式。
  • 如果您使用的是自定义字体,请检查字体大小。
  • 压缩图像。

您可以从Google的官方doc找到更多信息。
这是关于减少 Apk 大小的有用 Medium Article

你的问题是你使用了很多库,其中最糟糕的是:

"com.github.barteksc:android-pdf-viewer:2.7.0-beta" 

像这样的库很难缩小,虽然在其他答案中有很多方法可以减小 APK 的大小,但这个库是您要寻找的主要方法。