android - 尝试在 Android 上使用 JNI,但在我的 c 程序中获取 undefined reference

标签 android c java-native-interface

我目前正在尝试将 OP-TEE 与 Android 一起使用,这是一个开源信任执行环境,为了让我使用 OP-TEE 的 api,我必须使用 JNI 接口(interface)。

我当前在 Android Studio 中的 c 程序是一个 hello_world 示例,它是用 c 语言为 OP-TEE 编写的,我只是尝试从 hello_world.c 示例构建一个共享库:

c文件:

#include<jni.h>
#include<string.h>
#include <err.h>
#include "tee_client_api.h"
#include "ta_hello_world.h"
#include <stdio.h>

jstring Java_com_example_jsherman_ndktest_MainActivity_helloWorld(JNIEnv* env,jobject obj){

    TEEC_Result res;
    TEEC_Context ctx;
    TEEC_Session sess;
    TEEC_Operation op;
    TEEC_UUID uuid = TA_HELLO_WORLD_UUID;
    uint32_t err_origin;

    /* Initialize a context connecting us to the TEE */
    res = TEEC_InitializeContext(NULL, &ctx);
    if (res != TEEC_SUCCESS)
        errx(1, "TEEC_InitializeContext failed with code 0x%x", res);

    /*
     * Open a session to the "hello world" TA, the TA will print "hello
     * world!" in the log when the session is created.
     */
    res = TEEC_OpenSession(&ctx, &sess, &uuid,
                   TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
    if (res != TEEC_SUCCESS)
        errx(1, "TEEC_Opensession failed with code 0x%x origin 0x%x",
            res, err_origin);

    /*
     * Execute a function in the TA by invoking it, in this case
     * we're incrementing a number.
     *
     * The value of command ID part and how the parameters are
     * interpreted is part of the interface provided by the TA.
     */

    /*
     * Prepare the argument. Pass a value in the first parameter,
     * the remaining three parameters are unused.
     */
    memset(&op, 0, sizeof(op));
    op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT, TEEC_NONE,
                     TEEC_NONE, TEEC_NONE);
    op.params[0].value.a = 42;

    printf("Invoking TA to increment %d\n", op.params[0].value.a);
    res = TEEC_InvokeCommand(&sess, TA_HELLO_WORLD_CMD_INC_VALUE, &op,
                 &err_origin);
    if (res != TEEC_SUCCESS)
        errx(1, "TEEC_InvokeCommand failed with code 0x%x origin 0x%x",
            res, err_origin);
    printf("TA incremented value to %d\n", op.params[0].value.a);

    /*
     * We're done with the TA, close the session and
     * destroy the context.
     *
     * The TA will print "Goodbye!" in the log when the
     * session is closed.
     */

    TEEC_CloseSession(&sess);

    TEEC_FinalizeContext(&ctx);

    return (*env)->NewStringUTF(env,"Hello world");
}

Android.mk 文件:

################################################################################
# Android optee-hello-world makefile                                           #
################################################################################

LOCAL_PATH := $(call my-dir)
CFG_TEEC_PUBLIC_INCLUDE = /home/jsherman/devel/optee/optee_client/public

################################################################################
# Build hello world                                                            #
################################################################################
include $(CLEAR_VARS)


LOCAL_C_INCLUDES := /home/jsherman/devel/optee/optee_hello_world/ta/include \
        $(CFG_TEEC_PUBLIC_INCLUDE) \

$(info $(LOCAL_C_INCLUDES))

LOCAL_SHARED_LIBRARIES := libteec

LOCAL_MODULE := ndktest
LOCAL_SRC_FILES := ndktest.c

include $(BUILD_SHARED_LIBRARY)

由于某种原因,我收到错误:

/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:18: undefined reference to `TEEC_InitializeContext'
/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:26: undefined reference to `TEEC_OpenSession'
/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:50: undefined reference to `TEEC_InvokeCommand'
/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:65: undefined reference to `TEEC_CloseSession'
/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:67: undefined reference to `TEEC_FinalizeContext'

我对为什么会收到此错误感到有点困惑,因为我在 LOCAL_C_INCLUDES 变量中引用了正确的目录,tee_client_api.h 所在的目录定义了这些数据结构。

最佳答案

OP-TEE 是一个非平台库,因此您必须使用 PREBUILT_SHARED_LIBRARY 告知二进制文件位置 block :

include $(CLEAR_VARS)
LOCAL_MODULE := libteec
LOCAL_SRC_FILES := <path_to_libteec_so>
include $(PREBUILT_SHARED_LIBRARY)

关于android - 尝试在 Android 上使用 JNI,但在我的 c 程序中获取 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38301786/

相关文章:

java - 在 ListView 中在 TextView 旁边显示 ImageView

android - 更新之前的经纬度

C - 无法访问内存地址

在 GLib 中创建一个新的 GSource

Android JNI原生C++代码GetPrimitiveArrayCritical()问题

android父 Activity 在startActivity上被杀死

Android 以字节为单位设置 edittext 最大限制

c - C中没有类型的参数

java - 如何使用作业对象数组? (Jni)

android - 从 jni 访问 ArrayList 的元素