bytedeco / javacpp

The missing bridge between Java and native C++
Other
4.48k stars 582 forks source link

Do we have to specify the platform property on Android? #687

Closed lancewoo closed 1 year ago

lancewoo commented 1 year ago

For Android apks, some may only inlcude armeabi-v7a libraries but they want to run on an arm64-v8 device. If we don't set the javacpp.platform, it seems those apps won't run properly.

saudet commented 1 year ago

No, we don't need to, for example: https://github.com/bytedeco/sample-projects/blob/master/JavaCV-android-example/app/build.gradle

lancewoo commented 1 year ago

Great! Then I only used an ndk abifilters block to filter out those unwanted, like this:

        ndk {
            // in case you want to use some arch only
            abiFilters "armeabi-v7a"//, "arm64-v8a"
        }

There's another question here. How can I package the executables like ffmpeg and ffprobe into the apk? Any other tricks?

saudet commented 1 year ago

There's another question here. How can I package the executables like ffmpeg and ffprobe into the apk? Any other tricks?

We need to use Android API 28 or less for that to work, see https://github.com/bytedeco/javacv/issues/1127#issuecomment-643700534

lancewoo commented 1 year ago

Hi @saudet , the problem here is ffmpeg didn't get packaged. Even though I changed targetSdkVersion to 26(Android 8.0), I find that the ffmpeg executable is still missing in the final apk. So I am wondering why the executable didn't get packaged.

But when I compiled the app using the dependency jar file directly, the executable file is right there in the apk. It seems different.

saudet commented 1 year ago

The Android plugin won't do that for us, you'll need to add them as resources somehow. Probably copying them in the right subdirectory for Gradle to pick them up should do the trick.

lancewoo commented 1 year ago

OK, got it. Thanks! @saudet

Then I am posting my workaround code snippet here for somebody else's information:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        ndk {
            abiFilters "armeabi-v7a"//, "arm64-v8a"
        }
    }

....
    packagingOptions {
        pickFirst 'META-INF/native-image/android-arm*/jnijavacpp/jni-config.json'
        pickFirst 'META-INF/native-image/android-arm*/jnijavacpp/reflect-config.json'
    }

   ....
}

dependencies {
//    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // java
    implementation 'org.bytedeco:javacpp:1.5.9'
    implementation 'org.bytedeco:ffmpeg:6.0-1.5.9'
    // so
    implementation files("libs/javacpp-1.5.9-android-arm.jar",
            "libs/ffmpeg-6.0-1.5.9-android-arm.jar"
    )
}