As suggested, using proguard should remove unused methods and fields also from included libraries : https://developer.android.com/studio/build/shrink-code.html
Code shrinking is available with ProGuard, which detects and removes
unused classes, fields, methods, and attributes from your packaged
app, including those from included code libraries (making it a
valuable tool for working around the 64k reference limit).
(emphasis mine)
If you would like to do it manually then below is my try at stripping guava to leave only dependencies needed by HashBiMap. It looks like it relies on lots of classes. Also remember that proguard works at the byte level so stripping classes will never work as efficiently as removing unused code with proguard.
I used jdeps from java 9 JDK to find all the dependencies used by HashBiMap
which implements BiMap
interface. This show that it recursively depends on 35% of whole guava jar (actually 666 classes out of 1852 present in the jar) - not to mention java.base classes. The repackaged jar file has 903KB, while original jar is 2.5MB (guava-23.0-rc1-android.jar).
Below is script I used (I also tested resulting jar in example android app):
# 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 *
and sample test code:
BiMap<String, String> myBimap = HashBiMap.create();
myBimap.put("Key", "value");
myBimap.get("key");
myBimap.inverse().get("value");