在 Android Studio 中将没有 headers 的共享 object 库与 NDK 链接

Linking shared object library without headers with NDK in Android Studio

我有一个共享库文件 faceblaster-engine.so,为 arm-linux-androideabi 编译,放在 Android Studio 的 jniLibs 文件夹中。我在 jni 文件夹中也有一个简单的 cpp 文件。

我的库是用 Rust 编写的,所以我没有 header 文件,我想通过 cpp 文件调用其中的函数,但似乎无法将库获取到 link正确。为了测试,我做了一个简单的函数:

生锈

#[no_mangle]
pub extern fn rust_test() -> c_int {
    82 as c_int
}

C++

extern "C" {

// Test for calling rust function
int rust_test();

jint
Java_com_fureality_faceblaster_MainActivity_testRustLaunch(JNIEnv* env, jobject thiz)
{
    return rust_test();
}

} // End extern

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := faceblaster-engine
LOCAL_SRC_FILES := ../jniLibs/$(TARGET_ARCH_ABI)/libfaceblaster-engine.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := faceblaster
LOCAL_SRC_FILES := gl-tests.cpp
LOCAL_SHARED_LIBRARIES := faceblaster-engine
include $(BUILD_SHARED_LIBRARY)

错误

/home/nathan/Development/projects/faceblaster-android/app/src/main/jni/gl-tests.cpp

Error:(23) undefined reference to `rust_test'
Error:error: ld returned 1 exit status
make: *** [/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/libfaceblaster.so] Error 1
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /home/nathan/Development/bin/android-ndk-r10d/ndk-build NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/Android.mk APP_PLATFORM=android-21 NDK_OUT=/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/obj NDK_LIBS_OUT=/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/lib APP_ABI=all
  Error Code:
    2
  Output:
    /home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/objs/faceblaster//home/nathan/Development/projects/faceblaster-android/app/src/main/jni/gl-tests.o: In function `Java_com_fureality_faceblaster_MainActivity_testRustLaunch':
    /home/nathan/Development/projects/faceblaster-android/app/src/main/jni/gl-tests.cpp:23: undefined reference to `rust_test'
    collect2: error: ld returned 1 exit status
    make: *** [/home/nathan/Development/projects/faceblaster-android/app/build/intermediates/ndk/debug/obj/local/arm64-v8a/libfaceblaster.so] Error 1

我不知道我的 makefile 是否没有被提取,或者它是否正确?任何人都知道如何正确地 link 针对这个 .so 文件并注册我想使用的函数?

在此先感谢您的帮助!

编辑 - 添加Java源码和rust代码编译方法


Java

public class MainActivity extends Activity {

    // External libraries to load
    static {
        System.loadLibrary("faceblaster-engine");
        System.loadLibrary("faceblaster");
    }

    // External functions to register
    public native int testRustLaunch();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Other stuff omitted for brevity
        Log.d(TAG, "Testing call...");
        int test = testRustLaunch();
        Log.d(TAG, "Received: " + test);
    }
}

Rust 编译

cargo build --target=arm-linux-androideabi

# /project/.cargo/config file
[target.arm-linux-androideabi]
linker = "/opt/ndk_standalone/bin/arm-linux-androideabi-gcc"

# Cargo.toml
[lib]
name = "faceblaster-engine"
crate_type = ["dylib"]

编辑 2


我已经通过 build.gradle 脚本进行了编辑,我知道我的 Android.mk 现在正在被读取和使用,但我仍然遇到相同的编译错误:(

编辑 3


事实证明,以下两个答案都有助于解决问题。主要是 Android Studio 的一部分没有获取我的 makefile,rust 代码没有被正确声明为 #[no_mangle] pub extern 并且我的 makefile 全部被顶起。

试一试:

#[no_mangle]
pub extern fn rust_test() -> i32 {
    82 // Note simplified implementation
}

具体要尝试的是#[no_mangle]pubpub 会将函数标记为可从编译库外部调用。 #[no_mangle] 指示编译器不要更改函数名称,因此导出的符号将是文字 rust_test.

我还冒昧地使实际的方法体更加地道。

另一个注意事项是您应该更紧密地匹配您的 Rust 和 C 类型。如果你想在 C 中使用 int,你应该使用 Rust 类型 c_int。 C 的 int 可以根据您的平台更改大小!你也可以在 Rust 中使用 int32,但是你应该在 C 中使用类似 int32_t 的东西。

您的 Android.mk 没有指定 JNI 包装器实际上应该尝试 link 到 faceblaster-engine - LOCAL_SHARED_LIBRARIES 行反而说它应该 link 到本身。将其更改为 LOCAL_SHARED_LIBRARIES := faceblaster-engine,希望它能更好地工作。

然后要在运行时实际加载它,您需要以反向依赖顺序加载库,即:

System.loadLibrary("faceblaster-engine");
System.loadLibrary("faceblaster");