我已经安装了谷歌播放服务,并创建了一个Hello World应用程序来测试一切正常,我认为应用程序的大小太大了: 4.98MB。我正在使用Android Studio,我已经按照Android开发者网络中详细说明的进行了操作。
这是我的gradle文件:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:4.3.23'
}
编辑
这是我的proGuard文件:
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
**编辑2**
我已经使用Intellij Idea的最后一个版本安装了Google Play服务,现在apk是3.52MB。我不知道这是否是一个可接受的大小。
这正常吗?
不,这不正常——我的GPSTest应用程序的早期版本,包括谷歌地图播放服务,在使用ProGuard进行混淆后只有808KB——当前版本(添加另一个库后)在混淆后大约为1497KB。
我建议使用以下步骤从命令行导出APK,以避免Android Studio的潜在问题:
gradlew程序集Release
/app/build/apk
文件夹如果您通过Android Studio导出APK,请注意存在一个已知问题,即Android Studio将默认使用assembleDebug
任务而不是assembleRelease
任务导出。因此,您的build. gradle
文件中用于运行ProGuard的任何特定于发布buildType的配置都不会被执行。
作为通过Android Studio导出的解决方法,您可以通过以下步骤更改默认Build Variant:
现在当你做“建造-
如果你想从GPSTest复制我的设置,这里是proGuard. cfg
:
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.preference.Preference
-keepclasseswithmembers 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 enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-dontwarn **CompatHoneycomb
-dontwarn **CompatCreatorHoneycombMR2
-dontwarn **AccessibilityServiceInfoCompatJellyBeanMr2
-dontwarn android.support.v4.view.**
-dontwarn android.support.v4.media.**
-dontwarn com.actionbarsherlock.internal.**
-keep class android.support.v4.** { *; }
-keepattributes *Annotation*
-keep public class * extends android.view.View
-keep public class * extends android.view.ViewGroup
-keep public class * extends android.support.v4.app.Fragment
-keepclassmembers class * extends com.actionbarsherlock.ActionBarSherlock {
<init>(android.app.Activity, int);
}
…和build. gradle
:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
if (project.hasProperty("secure.properties")
&& new File(project.property("secure.properties")).exists()) {
Properties props = new Properties()
props.load(new FileInputStream(file(project.property("secure.properties"))))
signingConfigs {
debug {
storeFile file("gpstest.debug.keystore")
}
release {
storeFile file(props['key.store'])
keyAlias props['key.alias']
storePassword "askmelater"
keyPassword "askmelater"
}
}
} else {
signingConfigs {
debug {
storeFile file("gpstest.debug.keystore")
}
release {
// Nothing here
}
}
}
buildTypes {
release {
runProguard true
proguardFile 'proguard.cfg'
signingConfig signingConfigs.release
}
}
}
task askForPasswords << {
// Must create String because System.readPassword() returns char[]
// (and assigning that below fails silently)
def storePw = new String(System.console().readPassword("\nKeystore password: "))
def keyPw = new String(System.console().readPassword("Key password: "))
android.signingConfigs.release.storePassword = storePw
android.signingConfigs.release.keyPassword = keyPw
}
tasks.whenTaskAdded { theTask ->
if (theTask.name.equals("packageRelease")) {
theTask.dependsOn "askForPasswords"
}
}
dependencies {
compile project(':ShowcaseViewLibrary')
compile 'com.google.android.gms:play-services:3.2.65'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile 'org.jraf:android-switch-backport:1.2'
compile 'com.google.maps.android:android-maps-utils:0.2.1'
}
如果您想将其用作示例,可以在Github上获得完整的GPSTest源代码。
编辑
使用Google Play Services v6.5或更高版本的功能时,另一种帮助缩小APK的方法是仅包含您实际使用的Google Play服务中功能的库。
例如,如果您使用的唯一Google Play服务API是MapsAPIv2,而不是在build. gradle
中包含整个Google Play服务库:
compile 'com.google.android.gms:play-services:7.8.0'
…您可以只包含地图APIv2部分:
compile 'com.google.android.gms:play-services-maps:7.8.0'
有关可以拆分哪些API的详细信息,请参阅Google Play服务-“有选择地将API编译到您的可执行文件中”部分。这是截至2015年9月的列表:
好的,我终于遇到了这个问题(感谢@Sean Barbeau)。这是我的模块的build. gradle
文件。在BuildTypes
部分,我有release
而不是debug
,我正在部署调试版本…我已经改变了,现在我的应用程序只有1.17MB!
buildTypes {
debug {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
编辑
正如@SeanBarbeau所指出的,在调试模式下,runProGuard
设置为true
。如果我设置了false
,那么我的应用程序又大了(5MB大小)。