我試圖擺脫Google Play Services
庫中未使用的類。我已經創建了具有單個空活動的全新的android項目。該項目不使用任何來自Google Play Services
庫。所以我期望,當我構建版本(其中包括在我的配置中運行proguard)時,我會發現二進制大小與具有/不具有play-services
依賴性的構建相比沒有差異。但實際上,我在apk
大小看到〜700 KB差異。我發現relatively complex solution, using gradle script,其中涉及重新打包play-services.jar文件。另外,這個解決方案需要明確指定每個不打算使用的包。但我不明白爲什麼proguard
在我的情況下做這項工作?使用Proguard刪除Google Play Services庫中未使用的類
的build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
// !!! when I comment the line below, release APK is 700 KB smaller !!! //
compile 'com.google.android.gms:play-services:6.5.87'
}
proguard-rules.pro:
-assumenosideeffects class android.util.Log {
public static *** d(...);
}
的AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.noplayservices">
<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<activity android:name=".ui.activities.MainActivity" android:icon="@drawable/ic_launcher">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.test.noplayservices.ui.activities;
import android.app.Activity;
import android.os.Bundle;
import com.test.noplayservices.R;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.main_activity);
}
}
Proguard刪除未使用的類,但我不確定它是否會刪除'公共靜態'數據,或'static initializers'發生了什麼。我敢打賭,這些課程並沒有被刪除。作爲谷歌播放服務的所有類相互關聯的多功能圖書館,我想這種方法將類似於你所說的:只要嘗試從圖書館jar中提取你需要的類。順便說一句 - 我使用的是比現在小得多的舊罐子,去年它們的尺寸增長了3倍。 – rupps
此外,您可以做的是將您的700kb APK上傳到網站,例如http://www.decompileandroid.com/,將您的APK反編譯爲源代碼,並查看包含哪些GPS類,並試着瞭解爲什麼。至少你可以定義規則來排除這些類。記住要禁用混淆,以清楚地看到包含的類! – rupps
您可能還想添加'shrinkResources true' - 請參閱[這裏](https://plus.google.com/u/0/+TorNorbye/posts/eHsStybrrBf) – natario