Search code examples
java-mejarproguardbouncycastlefilesize

Using Bouncy Castle library causes massive increase in output .jar file size


I'm working on a Java ME/J2ME project which makes use of the Bouncy Castle J2ME library. When adding it to my project, however, I've noticed the resulting .jar file size increases 40 times (50kB vs. 2000kB). Other than setting ProGuard's obfuscator settings to level 9 (max), is there any other way I can minimize this increase in file size?

I'm only using a few of the libraries actual classes, namely:

import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.crypto.digests.SHA256Digest;

Thanks in advance.


Solution

  • Now this is a very tricky one!

    Before anything else please can you unzip your jar file and see if the Proguard has obfuscated the class files of BouncyCastle? Since you have included bouncycastle jar file as a library and there are class files in it so I am assuming the Proguard hasn't obfuscated these class files which are taking much of the space.

    Anyways I have just downloaded the bouncy castle jar file and can see it is purely made up of class files, so there are no extra files which we might be able to delete and get rid of to reduce the resultant jar file.

    Now If you really want to reduce then read on I am going to produce two solutions, both of them are risky:

    Solution 1 (Not Recommended):

    1. This is easy but I wouldn't recommend, It may cause runtime exceptions or errors: Unzip the bouncycastle jar file. Then navigate to the directory src\org\bouncycastle As you have mentioned you are only using these below packages org.bouncycastle.util.* org.bouncycastle.crypto.*

    2. Now here I will be assuming that the source code inside these two packages is not dependant on the source code/class files inside other packages.

    3. Now delete the other directories namely "asn1", "bcpg". At this stage I will not become a butcher and delete everything but will go by deleting a couple and then come back and delete others if it worked.

    4. Now we will regenerate the jar file. After you have deleted the directories now go back to the root of the directory where you will see other directories like META-INF org utils

    This is an important step you need to this correctly otherwise jar file will not work. Select all the directories in the root directory and then right click-> send to zip

    Once the zip file is created then rename it to having *.jar instead of *.zip

    1. The size of the jar file would have reduced and now include this jar file in your project. See if it compiles and executes. Then check your functionality if it is working correctly at Runtime. If all goes fine, then you are done. Now you can repeat from above steps to delete further directories to reduce the jar file size.

    Solution 2:

    1. Download the source code of the bouncycastle jar file that you are using.
    2. You will have to remove the jar file from the project.
    3. Include the source code into the project. Build the project and see if you dont get any compilation error. You have to be sure at this stage that you are not using the original jar file, and only using the code.
    4. Now start deleting the packages containing code that you dont need. Keep rebuilding the project that it doesn't come up compilation error, and if it does then you dont want to delte that package and move on deleting others.
    5. Once this butchring is done then Build the project and generate the jar file.
    6. By this way the Proguard will obfuscate code of bouncy castle and because of deleting most of the code not needed you should get a substantial decrease in the resultant jar file.