Skip to content

Android unit tests with Conscrypt #649

Closed

Description

@desokroshan

I am unable to run android unit tests when using org.conscrypt:conscrypt-android:2.0.0 as my dependency. I am running into following error when trying to add OpenSSLProvider as a Security Provider :

java.lang.UnsatisfiedLinkError: no conscrypt_jni in java.library.path

	at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
	at java.lang.Runtime.loadLibrary0(Runtime.java:870)
	at java.lang.System.loadLibrary(System.java:1122)
	at org.conscrypt.NativeCryptoJni.init(NativeCryptoJni.java:28)
	at org.conscrypt.NativeCrypto.<clinit>(NativeCrypto.java:65)
	at org.conscrypt.OpenSSLProvider.<init>(OpenSSLProvider.java:60)
	at org.conscrypt.OpenSSLProvider.<init>(OpenSSLProvider.java:53)
	at org.conscrypt.OpenSSLProvider.<init>(OpenSSLProvider.java:49)

Since unit tests run on the local machine, it fails to link native libraries that are compiled for different platform(mac in my case)? Is there a workaround/solution available for this?

PFA a simple android project where running the unit test reproduces the issue
ConscryptRepro.zip

Activity

  1. flooey commented on Mar 28, 2019

    @flooey
    Contributor

    Ah, I see the problem. Yeah, as far as I know (or have been able to find on the Internet), there isn't a simple solution to this.

    As you note, the local unit tests run on your local machine in a JVM, rather than in an Android environment. As a result, the Android dependency (which is packaged differently and such) doesn't work out of the box, but the OpenJDK dependency does. (If you change your dependency to org.conscrypt:conscrypt-openjdk-uber:2.0.0 in the sample project, you can see that it works.)

    Unfortunately, I wasn't able to find a good way to deal with that. The main things people mentioned were:

    • Skip the local unit tests and only use Android unit tests, so you never deal with this situation
    • Rewrite the classpath for your tests in gradle to remove the Android dependency and replace it with the OpenJDK one
    • Use LD_LIBRARY_PATH to point the library loader at the Android library, which will probably work (since we don't use much from the NDK) but doesn't really come with any guarantees

    Sadly, I don't think this is something we can improve on the Conscrypt side. Fundamentally, native library loading works differently in Android and on a JVM, and we need to package things so they work on Android in the Android distribution. If you find that one of those solutions works well for you, though, we could add some docs that show how it's done to help future users.

  2. desokroshan commented on Mar 28, 2019

    @desokroshan
    Author

    Skip the local unit tests and only use Android unit tests, so you never deal with this situation

    Do you mean Android instrumented tests because Android unit tests(which I think is same as local unit tests you mentioned.) are the ones we cannot run.

    Sadly, I don't think this is something we can improve on the Conscrypt side.

    I was thinking about packaging native libraries for armeabi-v7a, and arm64-v8a in the uber version since the uber version is supposed to have the shared libraries for all of the published platforms ?? In this case I can use the uber version for all both test and source code. Please let me know you thoughts. Thanks!

  3. flooey commented on Mar 28, 2019

    @flooey
    Contributor

    Do you mean Android instrumented tests because Android unit tests(which I think is same as local unit tests you mentioned.) are the ones we cannot run.

    Yeah, instrumented tests.

    I was thinking about packaging native libraries for armeabi-v7a, and arm64-v8a in the uber version since the uber version is supposed to have the shared libraries for all of the published platforms ?? In this case I can use the uber version for all both test and source code. Please let me know you thoughts. Thanks!

    Unfortunately that won't work. The Android and JVM library loaders look in different places for native libraries, so putting ARM native libraries in the JVM jar won't make them load on Android (in the same way that x86 libraries are already in the AAR, but the JVM native library loader can't find them). The Java code is also different between the Android and OpenJDK builds.

  4. desokroshan commented on Mar 28, 2019

    @desokroshan
    Author

    That makes sense. Thanks for the prompt response. This issue should be good to close.

  5. pkpfr commented on Jun 27, 2020

    @pkpfr

    I was thinking about packaging native libraries for armeabi-v7a, and arm64-v8a in the uber version since the uber version is supposed to have the shared libraries for all of the published platforms ?? In this case I can use the uber version for all both test and source code. Please let me know you thoughts. Thanks!

    This would be a big help. Trying to do anything with Conscrypt on a Raspberry Pi is really tough. With the announcement of Apple Silicon, there may also be increased demand for ARM builds.

  6. StefanLobbenmeier commented on Feb 24, 2021

    @StefanLobbenmeier

    Rewrite the classpath for your tests in gradle to remove the Android dependency and replace it with the OpenJDK one

    I would very much appreciate documentation on this

    I tried adding

    configurations {
        testRuntime.exclude group: "org.conscrypt", module: "conscrypt-android"
    }

    but that did not fix it

  7. StefanLobbenmeier commented on Feb 24, 2021

    @StefanLobbenmeier

    It looks like

    configurations {
        testImplementation.exclude group: "org.conscrypt", module: "conscrypt-android"
        testRuntime.exclude group: "org.conscrypt", module: "conscrypt-android"
    }

    is the way to go

  8. icksonb commented on Mar 26, 2021

    @icksonb

    I tried this:

    dependencies {
        ...
        compileOnly 'org.conscrypt:conscrypt-openjdk-uber:2.5.1'
        runtimeOnly 'org.conscrypt:conscrypt-android:2.5.1'
        testImplementation 'org.conscrypt:conscrypt-openjdk-uber:2.5.1'
        ...
    }
    

    and it worked for me.

  9. StefanLobbenmeier commented on Mar 26, 2021

    @StefanLobbenmeier

    What about androidTest, do they also work with that setup?

  10. yinyinnie commented on Sep 18, 2025

    @yinyinnie

    I tried this:

    dependencies {
        ...
        compileOnly 'org.conscrypt:conscrypt-openjdk-uber:2.5.1'
        runtimeOnly 'org.conscrypt:conscrypt-android:2.5.1'
        testImplementation 'org.conscrypt:conscrypt-openjdk-uber:2.5.1'
        ...
    }
    

    and it worked for me.

    yeah. i tried this too:

    implementation org.conscrypt:conscrypt-android:${conscryptAndroidVersion}"
    testImplementation org.conscrypt:conscrypt-openjdk-uber:${conscryptUberVersion}
    
    

    and it worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions