0

I had an examples for two so files for one app which is very simple, and I just have tried to do same code on Android Studio 1.1.0. I refer many web sites, so I put my so files on "src/main/jniLis/[arch]/" and run my app. But it was failed. This is error log from ddms.

UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.mdsedu.twolibstatic-2/base.apk"],nativeLibraryDirectories=[/data/app/com.mdsedu.twolibstatic-2/lib/arm, /vendor/lib, /system/lib]]] couldn't find "libtwolib-first.so

This is the part of java code. (It's similar as jni sample "two-lib".)

public void onCreate(Bundle savedInstanceState)
{ ...
    System.loadLibrary("twolib-first");
    System.loadLibrary("twolib-second");
    int sum = nativeAdd(x, y); ...
}
public native int nativeAdd(int  x, int  y);

This is the Android.mk

include $(CLEAR_VARS)
LOCAL_MODULE    := libtwolib-first
LOCAL_SRC_FILES := first.c
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := libtwolib-second
LOCAL_SRC_FILES := second.c
LOCAL_SHARED_LIBRARIES := libtwolib-first
include $(BUILD_SHARED_LIBRARY)

I didn't modiify build.gradle file.

I tried to compress lib/[arch]/*.so files and convert to jar files, but it was not working neither.

Would anybody please help me for my two-lib samples on Android Studio? I just started Android Studio a few days ago. please apologize me if I have misunderstood something.

KATE KIM
  • 1
  • 2

1 Answers1

2

you have two solutions to use more than one ndk lib from an Android Studio project.

1) remove the jni folder from your Android Studio project, compile your .so files from anywhere outside of your Android studio project, directly using ndk-build, and put them under src/main/jniLibs/(armeabi-v7a, x86, ...)/

2) keep your jni folder with your sources and Makefiles, deactivate the built-in call to ndk-build, change the location of jniLibs to libs, and add a call to ndk-build. In summary, put this inside your build.gradle file:

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

android {
    ...

    sourceSets.main {
        jniLibs.srcDir 'src/main/libs' //set .so files directory to libs
        jni.srcDirs = [] //disable automatic ndk-build call
    }

    // call regular ndk-build(.cmd) script from app directory
    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
        } else {
            commandLine 'ndk-build', '-C', file('src/main').absolutePath
        }
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
}

If you're still encountering issues, look if your .so files are getting properly packaged inside your APK, under lib/(armeabi-v7a, x86, ...), by opening it as a zip file. If that's the case, the issue isn't on packaging.

ph0b
  • 14,353
  • 4
  • 43
  • 41