Android Spongy Castle NoClassDefFoundError

If you are using Spongy Castle (which is a repackage of Bouncy Castle for Android), and are coming across a java.lang.NoClassDefFoundError like below on Android 4.x, you should perhaps check if you multi-dex is properly setup.

If you are using multi-dex, go through the documentation, and make sure it is properly setup.

In one app, where I was getting this error, it turned out the <application> tag was missing an attribute:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.example.myapp">
    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>  

The error I was getting from Spongy Castle because of incorrect multi-dex setup:

08-09 12:50:56.815 24586-24586/com.company.appname.st E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.company.appname.st, PID: 24586  
java.lang.NoClassDefFoundError: org.spongycastle.util.Strings  
  at org.spongycastle.jcajce.provider.config.ProviderConfigurationPermission.calculateMask(ProviderConfigurationPermission.java:64)
  at org.spongycastle.jcajce.provider.config.ProviderConfigurationPermission.<init>(ProviderConfigurationPermission.java:58)
  at org.spongycastle.jce.provider.BouncyCastleProviderConfiguration.<clinit>(BouncyCastleProviderConfiguration.java:16)
  at org.spongycastle.jce.provider.BouncyCastleProvider.<clinit>(BouncyCastleProvider.java:51)
  at com.company.appname.utils.KeyManager.<clinit>(KeyManager.java:47)
  at com.company.appname.api.controllers.BaseController.<init>(BaseController.java:62)
  at com.company.appname.api.controllers.SystemController.<init>(SystemController.java:26)
  at com.company.appname.api.API.<init>(API.java:90)
  at com.company.appname.api.API.getInstance(API.java:65)
  at com.company.appname.BaseActivity.onCreate(BaseActivity.java:79)
  at com.company.appname.MainActivity.onCreate(MainActivity.java:81)
  at android.app.Activity.performCreate(Activity.java:5231)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  at android.app.ActivityThread.access$800(ActivityThread.java:135)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:136)
  at android.app.ActivityThread.main(ActivityThread.java:5017)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  at dalvik.system.NativeStart.main(Native Method)
comments powered by Disqus