2017-08-07 50 views
0

的某些包因爲我需要在我目前的項目bi-directional地圖我看只有添加外部庫

  • 谷歌番石榴的BIMAP和
  • Apache的集合的BidiMap。

的庫兩者都是相當big

那麼,有沒有什麼辦法可以只添加從guava library或只從Apache Collectionorg.apache.commons.collections4.bidimapBiMap class

歡呼聲

+0

提取相關類並將它們添加到自己的項目。或者,將它們用作實施自己班級的靈感。 – CommonsWare

+0

我將如何去提取我需要的類? –

+0

https://docs.oracle.com/javase/tutorial/deployment/jar/unpack.html –

回答

0

您可以使用Pro Guard。

要使您的APK文件儘可能小,您應該啓用收縮以刪除發佈版本中未使用的代碼和資源。本頁面描述瞭如何做到這一點,以及如何指定在構建期間保留或放棄哪些代碼和資源。

參見: Android ProGuard Docs


參見: Stackowerflow Proguard Question

0

至於建議,使用ProGuard應該包含的庫中刪除未使用的方法和字段也:https://developer.android.com/studio/build/shrink-code.html

代碼ProGuard可以縮小尺寸,它會檢測並從您的打包的 應用程序,包括那些來自包含的代碼庫(使其成爲用於解決64k參考極限的有價值工具)中的未使用的類,字段,方法和屬性中刪除 。

(重點煤礦)

如果你想這樣做手工然後下面是我嘗試在剝離番石榴,只留下由依賴關係需要HashBiMap。它看起來像依賴於很多類。還要記住,proguard在字節級別工作,所以剝離類將永遠不會像使用proguard去除未使用的代碼那樣有效。

我用java 9 JDK中的jdeps找到HashBiMap實現BiMap接口所​​使用的所有依賴關係。這表明它遞歸地依賴於35%的整個番石榴jar(實際上存在於jar中的666個類) - 更不用說java.base類。重新打包的jar文件有903KB,而原始jar是2.5MB(guava-23.0-rc1-android.jar)。

下面是劇本我用(我還測試導致例如Android應用的jar):

# cleanup 
rm -rf guava_jar 
rm -rf guava_jar_stripped 

# unzip jar file 
unzip -qq guava-23.0-rc1-android.jar -d guava_jar 

# first lets see how many classes are in guava 
find guava_jar -type f | wc -l 

# now use jdeps to find recursively dependencies for HashBiMap class. Each 
# dependency is a class name which after some string manipulations is used 
# to copy to guava_jar_stripped folder 
jdeps -R -verbose -cp ./guava-23.0-rc1-android.jar ./guava_jar/com/google/common/collect/HashBiMap.class \ 
    | sed -En 's/(.*)->.*/\1/p' \ 
    | sed -e 's/[[:space:]]*$//' \ 
    | sed -En 's/\./\//pg' \ 
    | uniq \ 
    | xargs -n 1 -I file rsync -qavR ./guava_jar/file".class" ./guava_jar_stripped 

# now lets see how many classes were copied 
find guava_jar_stripped -type f | wc -l 

# now copy back manifest files 
rsync -qavR ./guava_jar/META-INF/* ./guava_jar_stripped 

# and finally create new jar from stripped classes 
cd ./guava_jar_stripped/guava_jar 
jar cf ../guava_jar.jar * 

和樣品測試代碼:

BiMap<String, String> myBimap = HashBiMap.create(); 
myBimap.put("Key", "value"); 
myBimap.get("key"); 
myBimap.inverse().get("value");