Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shrink the size of a shared library?

I'm a Java programmer.
I included an open source C library, say A, into my android project to do some native work through function F1. Function F1 works well but the shared library compiled by A is, let's say, 5M in size. That means my final APK will increase about 5M which is big.
My question is, how can I shrink the shared library to only include functions that I need? Or at least remove most functions that I don't need? Is there any tools that can achieve this?

Update 1:
@Employed Russian has answered big part of the question above, which is by 'strip' tool.
My further question is, is there any tools can achieve things like what ProGuard does to Java, that is completely removing code I didn't call?

like image 935
free6om Avatar asked May 06 '16 03:05

free6om


2 Answers

Function F1 works well but the shared library compiled by A is, let's say, 5M in size.

It is exceedingly likely that your shared library contains debug info, which can easily be 80% of the size, and which you don't actually need. Most open-source libraries build with -O2 -g flags, and the -g is what contributes to the size of the final library.

To get a library without debug info, you run strip, like so:

cp libA.so libA.so.orig # keep original in case you need to debug it later
strip -g libA.so

Now compare sizes of libA.so and libA.so.orig. Chances are, libA.so will be significantly smaller.

You should be able to squeeze libA.so even more, by running strip libA.so. This removes all symbols, not just debugging symbols, but makes it harder to debug any crashes related to libA.so. However, since you kept original full-debug copy, that shouldn't be a big problem.

like image 149
Employed Russian Avatar answered Oct 27 '22 09:10

Employed Russian


Since you said the library is open source.. Just track the function implementation and along with classes it depends and recompile your own smaller version of the library.

Trust me this sound hard but will actually save you more time than playing with compiled c/c++ code.

like image 22
konzo Avatar answered Oct 27 '22 08:10

konzo