怎么能减少apk大小(应用程序大小太大)

时间:2017-07-17 09:40:46

标签: android apk proguard

检查下面的 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分析器图像

Imgur

查询

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

2)有没有办法只使用我使用proguard从库中使用的类?或者我应该使用更少的库来优化大小?

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

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

2)您也应该能够优化库,但有些已经过优化

我不知道的是为什么你说应用程序太大了。 Google Play允许APK高达100 MB。

缩小尺寸的最佳方法是减小图像尺寸。看起来你没有任何你可以削减的依赖关系(谷歌播放API被分成几个实例,如果你导入整个东西但只使用谷歌PLay游戏,你可以删除依赖并且只导入Play游戏。这与你的情况无关,这是一个例子。

答案 2 :(得分:1)

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

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

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

相关问题