使用组合java文件减少apk大小

时间:2015-06-12 21:25:29

标签: android size apk

减少.java文件的数量会减少apk尺寸吗? 例如,我在ListAdapter类中定义Activity而不是单独的.java文件更好吗?

那些xml文件呢?例如,制作shape.xml并在selector.xml中引用它,或在选择器的item内定义该形状?

3 个答案:

答案 0 :(得分:3)

  

减少.java文件数量会减少apk尺寸吗?

不一般。

  

xml文件怎么样?例如,创建一个shape.xml并在selector.xml中引用它,或者在选择器项目中定义该形状?

这可能会节省少量字节。

  

我有一些危急情况,甚至小尺寸的变化都非常重要(例如~5kb)

然后reduce your APK size

  • 摆脱使用其中一小部分的库,并使用尽可能少的库集(例如,特定的Play服务,而不是整个“厨房水槽”Play服务库)

  • 如果在测试Android的自动重新采样符合您的需求时,减少可绘制的密度变体的数量,并使用Lint识别可能在项目中挥之不去的资源,而这些资源是您未引用的

  • 使用ProGuard

  • 如果您使用的是Android Studio,请使用Gradle构建文件中的minifyResourcesresConfigs选项

  • Cyril Mottier在博客文章中指出了其他一切

答案 1 :(得分:0)

你最好减少图像资源,如果你使用maven / gradle构建系统,你可以通过使用progaurd工具推卸代码来减少apk大小。

答案 2 :(得分:0)

APK尺寸:

一般来说,这取决于您使用的资产。尝试在android studio中使用LINT,它可以为您提供未使用资源的摘要,您可以检查和删除它们。

此外,proguard对此非常有帮助,请尝试在您的应用程序中使用proguard。

http://dominoc925.blogspot.in/2015/01/enabling-proguard-obfuscation-in.html

使用minifyenabled = true以及我在Stackoverflow上找到的以下proguard基本配置:

-dontwarn org.**
-dontwarn okio.**
-dontwarn com.squareup.**
##---------------Begin: proguard configuration common for all Android apps ----------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''

-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
-dontnote com.android.vending.licensing.ILicensingService

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
    native <methods>;
}

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

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

# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* {
  public static <fields>;
}

# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep public class * {
    public protected *;
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
##---------------End: proguard configuration common for all Android apps ----------

# Remove Logging statements
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** e(...);
    public static *** i(...);
}

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.antew.redditinpictures.library.imgur.** { *; }
-keep class com.antew.redditinpictures.library.reddit.** { *; }

##---------------End: proguard configuration for Gson  ----------

#QuickBlox
-keep class org.jivesoftware.smack.** { *; }
-keep class com.quickblox.** { *; }
-keep class * extends org.jivesoftware.smack { public *; }
-keep class org.jivesoftware.smack.** { public *; }
-keep class org.jivesoftware.smackx.** { public *; }
-keep class com.quickblox.** { public *; }
-keep class * extends org.jivesoftware.smack { public *; }
-keep class * implements org.jivesoftware.smack.debugger.SmackDebugger { public *; }