Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

Android : could not find org.spongycastle.util.io.pem.PemReader

I am using spongycastle in my android code. code is working fine in android 5 and android 6 but in android 4 its showing the error :

java.lang.NoClassDefFoundError: org.spongycastle.util.io.pem.PemReader

Its failing at the time of initialization of PemReader in the below code :

    private PublicKey getPublicKey(AssetManager manager, String keyPath) {
    PublicKey key = null;
    try {
        final KeyFactory keyFactory = KeyFactory.getInstance(TicketVerifier.ENCRYPTION_ALGORITHM);
        InputStream stream = manager.open(keyPath);
        final PemReader reader = new PemReader(new InputStreamReader(stream));
        final byte[] pubKey = reader.readPemObject().getContent();
        reader.close();
        final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKey);
        key = keyFactory.generatePublic(publicKeySpec);
    } catch (IOException e) {
        Log.e(TAG, "error verifying ticket", e);
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "error verifying ticket", e);
    } catch (InvalidKeySpecException e) {
        Log.e(TAG, "error verifying ticket", e);
    }
    return key;
} 

build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
useLibrary 'org.apache.http.legacy'
defaultConfig {
    applicationId "com.xx.xx.android"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 22
    versionName "5.0.0.22"
    multiDexEnabled true
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile ('com.viewpagerindicator:library:2.4.1'){
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.google.guava'
}
compile 'com.google.zxing:android-integration:2.2'
compile 'com.google.zxing:core:2.3.0'
compile ('com.google.android.gms:play-services:4.2.42@aar'){
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.google.guava'
}
compile 'com.google.guava:guava:17.0'
compile 'org.altbeacon:android-beacon-library:2.1.3'
compile 'com.fasterxml.jackson.core:jackson-core:2.2.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.2.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
compile 'com.madgag:scprov-jdk15on:1.47.0.3'
compile 'net.oauth.core:oauth:20100527'
compile 'org.jsoup:jsoup:1.8.3'
compile 'com.android.support:support-v4:23.3.0'
}

Can anyone tell me how to handle it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Spongycastle is split up into several Packages. The PemReader is not part of the base Package. So make sure to add all dependencies:

dependencies {
     ....
    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'

    }

Don't forget to insert it as a security provider in your app.

static {
    Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...