Search code examples
androidapache-minaapache-sshd

How to run an Apache-Mina SSHD-Server (2.2.0) on Android Pie?


I'm trying to run an Apache MINA SSHD Server (v. 2.2.0) on an Android Pie (API 28.0) device. I found various post about it running on Android 4 devices, but none of them seems to work on my Android 9 device. So I tried to implement it myself but got stuck at the initialization of the Server with a ReflectionException/NoClassDefFoundError.

When I try to set up the server with SshServer.setUpDefaultServer(); I get this exception:

java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/management/ReflectionException;
        at org.apache.sshd.common.util.GenericUtils.peelException(GenericUtils.java:730)
        at org.apache.sshd.common.util.GenericUtils.peelException(GenericUtils.java:728)
        at org.apache.sshd.common.util.security.SecurityEntityFactory$2.getInstance(SecurityEntityFactory.java:134)
        at org.apache.sshd.common.util.security.SecurityUtils.getMessageDigest(SecurityUtils.java:726)
        at org.apache.sshd.common.digest.DigestUtils.checkSupported(DigestUtils.java:53)
        at org.apache.sshd.common.digest.BuiltinDigests.<init>(BuiltinDigests.java:61)
        at org.apache.sshd.common.digest.BuiltinDigests.<clinit>(BuiltinDigests.java:36)
        at org.apache.sshd.common.cipher.ECCurves.<clinit>(ECCurves.java:61)
        at org.apache.sshd.common.keyprovider.KeyPairProvider.<clinit>(KeyPairProvider.java:63)
        at org.apache.sshd.common.signature.BuiltinSignatures.<clinit>(BuiltinSignatures.java:62)
        at org.apache.sshd.common.BaseBuilder.<clinit>(BaseBuilder.java:133)
        at org.apache.sshd.server.ServerBuilder.builder(ServerBuilder.java:165)
        at org.apache.sshd.server.SshServer.setUpDefaultServer(SshServer.java:429)
        at ch.zhaw.init.sshshell.SshShellService.<init>(SshShellService.java:20)

From some older posts for Android 4 it seems that this is was a known problem and the solution there was to add $useLibrary 'org.apache.http.legacy' to gradle and add to the manifest.xml. I did that but the exception still occurs.

build.gradle file:

android {
    compileSdkVersion 28
    useLibrary 'org.apache.http.legacy'
    ...
}
dependencies {
    ...
    implementation 'org.apache.sshd:sshd-core:2.2.0'
    implementation 'org.slf4j:slf4j-simple:1.6.2'
    implementation 'org.apache.mina:mina-core:2.1.2'
    implementation "org.bouncycastle:bcprov-jdk16:1.46"
}

manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="ch.zhaw.init.sshshell">
    <application
            android:networkSecurityConfig="@xml/network_security_config">
        <uses-library android:name="org.apache.http.legacy"
                      android:required="false" />
        <service android:name=".SshShellService">
            <intent-filter>
                <action android:name="StartSshServer"/>
            </intent-filter>
        </service>
    </application>
</manifest>

My Java-Code:

import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;


private static final int PORT = 8022;
private final SshServer sshd = SshServer.setUpDefaultServer(); // Here I get the error
private final SimplePasswordAuthenticator passwordAuth = new SimplePasswordAuthenticator();
private final SimplePublicKeyAuthenticator publicKeyAuth = new SimplePublicKeyAuthenticator();
private final SimpleForwardingFilter forwardingFilter = new SimpleForwardingFilter();

It seems that older versions worked, but I search for days and did not find any working example for the newest version. Does anyone has a working example which runs the latest version of Apache Mina SSHD (2.2.0) on Android 9 (without rooting the device)?

or if not is there an alternative SSH-Server (not client - there are many of them) for Android Pie?

Thank you for any help.


Solution

  • For this specific failure, it suffices to create a dummy ReflectionException class (example). Given that the class doesn't even exist, nothing outside of your own code would ever instantiate it, so SSHD's instanceof ReflectionException check would never match, and you don't need to program any of its members.

    You'll also want to create a dummy MBeanException (example).