Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android java.lang.NoClassDefFoundError: org.bouncycastle.crypto.engines.AESEngine api 16

Tags:

java

android

I am using com.nimbusds.jose.crypto library in my android client for doing some jwt stuff.

This is what i declare in my gradle file :

    compile 'com.nimbusds:nimbus-jose-jwt:4.23'

Everything works fine on api >=19, but when I am running the code on api 16, I am getting this exception :

java.lang.NoClassDefFoundError: org.bouncycastle.crypto.engines.AESEngine.

What's the issue here? Why is the class AESENGINE not available on api 16?

like image 506
Yash Avatar asked Nov 13 '16 09:11

Yash


2 Answers

If you look into the dependecy list of nimbus-jose-jwt there is no bouncycastle library. However, if you look into the source code, and more precisely into the package com.nimbusds.jose.crypto.bc then you can see, that it uses bouncycastle without declaring it as dependency. So the library just assumes the bouncycastle is present.

The solution is to add the dependencies manually. First of all, follow the link to implement a standard way of using bouncycastle on Android.

However that does not solve the problem, because org.bouncycastle.crypto.engines.AESEngine is not in one of those libraries. The solution is to add one more dependency:

dependencies { compile 'org.bouncycastle:bcprov-jdk15on:1.54' }

Then everything should work fine.

SUMMARY:

gradle dependencies should lool like:

dependencies {
    compile 'com.nimbusds:nimbus-jose-jwt:4.23'
    compile 'com.madgag.spongycastle:core:1.54.0.0'
    compile 'com.madgag.spongycastle:prov:1.54.0.0'
    compile 'com.madgag.spongycastle:pkix:1.54.0.0'
    compile 'com.madgag.spongycastle:pg:1.54.0.0'
    compile 'org.bouncycastle:bcprov-jdk15on:1.54'
}

and you should register java.security.Provider:

static {
    Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
}
like image 196
R. Zagórski Avatar answered Nov 03 '22 19:11

R. Zagórski


Android OS is shipped with BouncyCastle already present in it. In API level 16 AESEngine class was not shipped with Android OS(Android decided to ship that class later on).Therefore, it is unable to find that class.

However, at compile time this class is present but at run time it is not.In such cases when you need to use this Class at or below API 16, you have to ship that class/jar with APK.

In addition of writing

compile "com.madgag.spongycastle:prov:1.54.0.0"

You have to write this also

apk "com.madgag.spongycastle:prov:1.54.0.0"

This will extend the scope of this class from compile time to APK and this will be available in all versions of Android.

Note:You will have to use SpongyCastle instead of BouncyCastle to avoid class name conflicts.

like image 2
Syed Taruf Naqvi Avatar answered Nov 03 '22 19:11

Syed Taruf Naqvi