尝试在Android上使用JNI,但在我的c程序中获取未定义的引用

时间:2016-07-11 07:54:14

标签: android c java-native-interface

我目前正试图在Android这是一个开源信任执行环境中使用OP-TEE,为了让我使用OP-TEE的api,我必须使用JNI接口

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

c file:

#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位于这些变量中,并定义了这些数据结构。

1 个答案:

答案 0 :(得分:0)

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

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