Android Studio project with library module using prebuilt so

311 views
Skip to first unread message

AppDeveloper

unread,
Jan 1, 2018, 9:28:20 PM1/1/18
to android-ndk
My android project was built using eclipse, now i want to change to use Android Studio 3.0.1.
The new android studio project basically okey and can run on the phone, but when the source code use the native function, it will crash.

The project stucture is like this:

Project
        -app
        -MyLibray  (Library module) (Which will use the C++ source code with ffmpeg library)

On the Library module, it will use NDK to build some C++ source code (Render.cpp) to use ffmpeg library. And the prebuilt is using ndk-build script, and the compile is fine, but when the source code on MyLibrary call the C++ function, it crash with:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/ ......  /system/lib]]] couldn't find "libRender.so"

I have tried to solve it for 2 days, but still has no solution...

The build.gradle on the library module is like that

ndk {
moduleName = "Render"
abiFilters "armeabi", "armeabi-v7a"
}

externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}

The Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE   := avformat
#LOCAL_SRC_FILES   := $(TARGET_ARCH_ABI)/libavformat.a
LOCAL_SRC_FILES    := lib/libavformat.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE   := avcodec
#LOCAL_SRC_FILES   := $(TARGET_ARCH_ABI)/libavcodec.a
LOCAL_SRC_FILES    := lib/libavcodec.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE   := avdevice
#LOCAL_SRC_FILES   := $(TARGET_ARCH_ABI)/libavdevice.a
LOCAL_SRC_FILES    := lib/libavdevice.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE   := avfilter
#LOCAL_SRC_FILES   := $(TARGET_ARCH_ABI)/libavfilter.a
LOCAL_SRC_FILES    := lib/libavfilter.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE   := avutil
#LOCAL_SRC_FILES   := $(TARGET_ARCH_ABI)/libavutil.a
LOCAL_SRC_FILES    := lib/libavutil.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE   := swscale
#LOCAL_SRC_FILES   := $(TARGET_ARCH_ABI)/libswscale.a
LOCAL_SRC_FILES    := lib/libswscale.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE   := swresample
#LOCAL_SRC_FILES   := $(TARGET_ARCH_ABI)/libswresample.a
LOCAL_SRC_FILES    := lib/libswresample.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := Render
LOCAL_SRC_FILES := Render.cpp
LOCAL_STATIC_LIBRARIES := avformat avcodec avdevice avfilter avutil swscale swresample
LOCAL_LDLIBS :=  -lm -lz -llog -lGLESv1_CM
LOCAL_ALLOW_UNDEFINED_SYMBOLS := false
include $(BUILD_SHARED_LIBRARY)

The java file to use it

public class NativeInterface {
// Native functions
static {
System.loadLibrary("Render");
}

public static native int nativeVersion();
}

When i call nativeVersion on MyLibrary, then it crash with java.lang.UnsatisfiedLinkError, But it is okey on ecsplise project.

When i unzip the aar file output on the library module, it has 

aar\MyLibrary-debug\jni\armeabi\libRender.so
aar\MyLibrary-debug\jni\armeabi-v7a\libRender.so

Alex Cohn

unread,
Jan 7, 2018, 6:26:25 AM1/7/18
to android-ndk
First of all, check that if you unzip the APK file, you have libRender.so (Android Studio provides a handy menu "Analyze APK…" under Build).
The question is how you defined dependency your app on MyLibrary in build.gradle.

BR,
Alex
Reply all
Reply to author
Forward
0 new messages