Android App needs com/sun/jna/android-aarch64/libjnidispatch.so | Javacv | libffm

I try to implement an App which is using the JavaCV implementation. The gradle file looks like this

apply plugin: 'com.android.application'

def AAVersion = '4.8.0'
def acraVersion = '5.7.0'

android {
    compileSdkVersion 31

    defaultConfig {
        configurations.all {
            resolutionStrategy { force 'androidx.core:core:1.6.0' }
        }
        applicationId "de.mw.talk2drone"
        minSdkVersion 31
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters 'arm64-v8a'
        }
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [
                        "androidManifestFile": "$projectDir/src/main/AndroidManifest.xml".toString(),
                        "resourcePackageName": android.defaultConfig.applicationId
                ]
            } // annotationProcessorOptions
        } // javaCompileOptions
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    lintOptions {
        checkReleaseBuilds true // Add this
        abortOnError false
    }

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java', 'build/generated/source/apt/${variant.dirName}']
            resources.srcDirs = ['src/main/res']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
        }
    }

    packagingOptions {
        exclude 'META-INF/native-image/ios**'
        exclude 'META-INF/native-image/linux**'
        exclude 'META-INF/native-image/windows**'
        exclude 'META-INF/native-image/android**'
        exclude 'META-INF/native-image/macosx**'

    }
    dependenciesInfo {
        includeInApk false
        includeInBundle false
    }
}

configurations {
    javacpp
}

task javacppExtract(type: Copy) {
    dependsOn configurations.javacpp

    from { configurations.javacpp.collect { zipTree(it) } }
    include "lib/**"
    into "$buildDir/javacpp/"
    android.sourceSets.main.jniLibs.srcDirs += ["$buildDir/javacpp/lib/"]

    tasks.getByName('preBuild').dependsOn javacppExtract
}

dependencies {
    implementation 'net.java.dev.jna:jna:5.10.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'

    implementation group: 'com.alphacephei', name: 'vosk-android', version: '0.3.34'
    implementation project(':models')
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.3.5'
    implementation 'androidx.navigation:navigation-ui:2.3.5'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    // To use constraintlayout in compose
    implementation 'androidx.constraintlayout:constraintlayout-compose:1.0.0'
    implementation "ch.acra:acra-mail:$acraVersion"
    implementation "ch.acra:acra-toast:$acraVersion"
    implementation "ch.acra:acra-core:$acraVersion"
    implementation "androidx.preference:preference:1.1.1"

    implementation "org.androidannotations:androidannotations-api:$AAVersion"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    annotationProcessor 'com.google.dagger:dagger-compiler:2.40.5'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'org.json:json:20211205'

    implementation group: 'org.bytedeco', name: 'javacv', version: '1.5.6'
    javacpp group: 'org.bytedeco', name: 'opencv-platform', version: '4.5.3-1.5.6'
    javacpp group: 'org.bytedeco', name: 'ffmpeg-platform', version: '4.4-1.5.6'
    //implementation group: 'org.bytedeco', name: 'opencv-platform', version: '4.5.3-1.5.6'
    //implementation group: 'org.bytedeco', name: 'ffmpeg-platform', version: '4.4-1.5.6'
}

I have read a lot of postings about it. When I run the application, I see the following error:

Process: de.mw.talk2drone, PID: 30330
    java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/android-aarch64/libjnidispatch.so) not found in resource path

I tried a lot of ways to include the file com/sun/jna/android-aarch64/libjnidispatch.so in the apk, but nothing worked for me. Using

implementation 'net.java.dev.jna:jna:5.10.0@aar'

aborts the compilation with "Duplicate class".

The App has to run on a Google Pixel 4a 5g.

Any hints are appreciated.


Solution 1:

Your problem is the dependency on vosk-android. This pulls in

  <dependencies>
    <dependency>
      <groupId>net.java.dev.jna</groupId>
      <artifactId>jna</artifactId>
      <version>4.4.0</version>
    </dependency>
  </dependencies>

as a transitive dependency. There are two issues:

  • 4.4.0 is OLD as in too old and unsuppored
  • with that transitive dependency the JNA jar and aar are both pulled in, which results in duplicates classes

This should do it:

    implementation 'net.java.dev.jna:jna:5.10.0@aar'
    implementation (group: 'com.alphacephei', name: 'vosk-android', version: '0.3.34') {
        exclude group: 'net.java.dev.jna', module: 'jna'
    }
  1. You declare a direct dependency on the aar variant of the JNA library package
  2. You exclude the transitive dependency on JNA from vosk-android