查看完整版本 : Android Studio Build NDK 問題

linuxfedora 2018-1-2 09:59 AM

Android Studio Build NDK 問題

.My android project was built using eclipse, now i want to change to use Android Studio 3.0.1.
The new android project basically ok and can run on the phone, but the part used prebuilt NDK library have problem.

The project stucture is like this:

Project
        -main app
        -MyLibray  (Library module)

On the Library module, it will use NDK to build some C++ source code 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

[[i] 本帖最後由 linuxfedora 於 2018-1-2 10:11 AM 編輯 [/i]]

jasonchan35 2018-1-3 04:03 AM

照睇就係 .so path 唔岩, 無 source code 好難幫你睇咩事
會唔會係 compile .so 時無加 -rpath ?

或者可以參考下 Linux 係點 search .so file
[url]https://www.youtube.com/watch?v=l5aTKMpH4BQ&index=4&list=PLeGk08zVu4576iIpTdYiSxGrKMRBywO5H[/url]
頁: [1]
查看完整版本: Android Studio Build NDK 問題