如何优化(减少)我的安卓应用程序的大小[英] How to optimize(reduce) the size of my android app

本文是小编为大家收集整理的关于如何优化(减少)我的安卓应用程序的大小的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我创建了一个Android应用程序,其中包含了2个WebView,其中包含一些Java脚本,这是一个围绕2 MB的内置PDF读取器API,所有的PDF(压缩)约为25 MB和其他图像(也压缩)等在生成应用程序后,少于1 MB,它在生成应用程序后,我的应用尺寸约为55 MB.我相信是不可接受的. 所以,为什么我的应用程序具有如此多的大尺寸,也是如何更加减少我的Android应用程序的大小.

推荐答案

发现"lib"为15 MB,大约35%的应用大小,这是不可接受的,因为我只使用了2 MB的单个EXT库>

您错误地误解了该库的大小及其依赖项.假设您自己不使用NDK,那么该库正在使用NDK,或其依赖项正在使用该库.该库大小大致匹配这个pdf查看器,尽管我不知道这是特定的你正在使用的.

PDF查看库的文档具有一个部分为什么图书馆让你的APK大

android pdfviewer取决于pdfiumandroid,它是许多架构的本机库(近16 MB). APK必须包含所有这些库以在市场上提供的每个设备上运行.幸运的是,Google Play允许我们上传多个APK,例如,每个架构一个.有很好的文章有关自动将应用程序分成多个APK,可用 .最重要的部分是改进多个APKS创建和VersionCode处理APK拆分,但整篇文章值得阅读.您只需要在您的应用程序中执行此操作,无需分叉PDFiumAndroid或因此.

您的选项是:

其他推荐答案

使用proguard.从build.gradle文件启用如此,

的proguard
  buildTypes {

        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

然后打开"proguard-rules.pro"并提及所有规则.如果您不使用任何外部库,则在您将其粘贴为"proguard-rules.pro"文件. .

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-allowaccessmodification
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-keepattributes EnclosingMethod

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keep public class com.google.android.gms.ads.** {
   public *;
}

-keep public class com.google.ads.** {
   public *;
}

如果您使用Okhhtp for Web服务,请在Proguard-Rules.pro

中使用此操作
# For OkHttp3
-keepattributes Signature
-keepattributes Annotation
-keep class okhttp3.** { *; }
-keep interface okhttp3.* { *; }
-dontwarn okhttp3.*

本文地址:https://www.itbaoku.cn/post/1937915.html

问题描述

I created an android app that contains just 2 webviews with some java script in them,an inbuilt PDF reader API which weighs around 2 MB and all the PDF's(Compressed) which are around 25 MB and other images(Also compressed) etc less than 1 MB which tallies to around 30 MB.So after generating the app my app size is around 55 MB.Which i believe is not acceptable. So,why is my app having so much large size and also how can i even more reduce the size of my android app.

推荐答案

found out that the "lib" is 15 MB which is around 35% of my app size which is not acceptable as i had used only a single ext library of 2 MB

You miscounted the size of that library and its dependencies. Assuming that you are not using the NDK yourself, then either that library is using the NDK, or its dependencies are using that library. That library size roughly matches that of this PDF viewer, though I do not know if that is the specific one that you are using.

The documentation for that PDF viewing library has a section on why the library makes your APK big:

Android PdfViewer depends on PdfiumAndroid, which is set of native libraries (almost 16 MB) for many architectures. Apk must contain all this libraries to run on every device available on market. Fortunately, Google Play allows us to upload multiple apks, e.g. one per every architecture. There is good article on automatically splitting your application into multiple apks, available here. Most important section is Improving multiple APKs creation and versionCode handling with APK Splits, but whole article is worth reading. You only need to do this in your application, no need for forking PdfiumAndroid or so.

Your options are:

  • Follow the advice in the documentation and use ABI splits to have separate APKs per CPU architecture (which only helps if you are distributing by some means that supports separate APKs per architecture, such as the Play Store)

  • Use some other PDF rendering option

  • Eliminate the PDF rendering feature from your app, relying on ACTION_VIEW and the user's chosen PDF viewer

  • Eliminate the PDFs from your app, converting them to HTML/CSS/images and rendering them in a WebView

其他推荐答案

Use Proguard. From build.gradle file enable Proguard like this,

  buildTypes {

        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

Then open your "proguard-rules.pro" and mention all the Rules. If you are not using any external libraries, then paste this in you "proguard-rules.pro" file. .

-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-allowaccessmodification
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-keepattributes EnclosingMethod

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keep public class com.google.android.gms.ads.** {
   public *;
}

-keep public class com.google.ads.** {
   public *;
}

If you are using Okhhtp for web service then use this too in your proguard-rules.pro

# For OkHttp3
-keepattributes Signature
-keepattributes Annotation
-keep class okhttp3.** { *; }
-keep interface okhttp3.* { *; }
-dontwarn okhttp3.*