Modifying a exiting aosp app

Hello i’m trying to modify android’s settings to use the optee os on a hikey 960. I’m currently having trouble with linking libteec. this is unique because i’m not developing a standalone app or buildling it on my computer. the file sturcture of the project is this:

settings
+--jni 
     +--android.mk
     +--hellojni.c
     +--tee_client_api.h
     +
+--android.mk

and in the android make at settings i’m including my libhello-jni.so file in this manner.

ZPATH := $(LOCAL_PATH)
include $(ZPATH)/jni/Android.mk
LOCAL_JNI_SHARED_LIBRARIES := libhello-jni

I was able to get that working just calling a native library. Here is my make file within the jni folder trying to include .so file.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libteec
LOCAL_SRC_FILES := /storage/optee_android_manifest/out/target/product/hikey960/vendor/lib/libteec.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libhello-jni
LOCAL_SRC_FILES := hello-jni.c

LOCAL_C_INCLUDES +=
$(JNI_H_INCLUDE)

LOCAL_CFLAGS += -O0 -g3

include $(BUILD_SHARED_LIBRARY)

this is the error i get

[ 16% 4/25] target SharedLib: libhello-jni (out/target/product/hikey960/obj/SHARED_LIBRARIES/libhello-jni_intermediates/LINKED/libhello-jni.so)
FAILED: out/target/product/hikey960/obj/SHARED_LIBRARIES/libhello-jni_intermediates/LINKED/libhello-jni.so 
/bin/bash -c "prebuilts/clang/host/linux-x86/clang-4691093/bin/clang++ -nostdlib -Wl,-soname,libhello-jni.so -Wl,--gc-sections -shared out/target/product/hikey960/obj/lib/crtbegin_so.o out/target/product/hikey960/obj/SHARED_LIBRARIES/libhello-jni_intermediates/hello-jni.o -Wl,--whole-archive  -Wl,--no-whole-archive   out/target/product/hikey960/obj/STATIC_LIBRARIES/libclang_rt.ubsan_minimal-aarch64-android_intermediates/libclang_rt.ubsan_minimal-aarch64-android.a  out/target/product/hikey960/obj/STATIC_LIBRARIES/libcompiler_rt-extras_intermediates/libcompiler_rt-extras.a   out/target/product/hikey960/obj/STATIC_LIBRARIES/libatomic_intermediates/libatomic.a out/target/product/hikey960/obj/STATIC_LIBRARIES/libgcc_intermediates/libgcc.a -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--build-id=md5 -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined-version -Wl,-m,aarch64_elf64_le_vec -Wl,--hash-style=gnu -fuse-ld=gold -Wl,--icf=safe -Wl,--fix-cortex-a53-843419   -target aarch64-linux-android -Bprebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/aarch64-linux-android/bin -Wl,--exclude-libs,libclang_rt.ubsan_minimal-aarch64-android.a -Wl,--no-undefined out/target/product/hikey960/obj/lib/libc++.so out/target/product/hikey960/obj/lib/libc.so out/target/product/hikey960/obj/lib/libm.so out/target/product/hikey960/obj/lib/libdl.so -o out/target/product/hikey960/obj/SHARED_LIBRARIES/libhello-jni_intermediates/LINKED/libhello-jni.so out/target/product/hikey960/obj/lib/crtend_so.o"
packages/apps/Settings/jni/hello-jni.c:42: error: undefined reference to 'TEEC_InitializeContext'
packages/apps/Settings/jni/hello-jni.c:50: error: undefined reference to 'TEEC_OpenSession'
packages/apps/Settings/jni/hello-jni.c:80: error: undefined reference to 'TEEC_InvokeCommand'
packages/apps/Settings/jni/hello-jni.c:95: error: undefined reference to 'TEEC_CloseSession'
packages/apps/Settings/jni/hello-jni.c:97: error: undefined reference to 'TEEC_FinalizeContext'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
[ 20% 5/25] target SharedLib: libhello-jni_32 (out/target/product/hikey960/obj_arm/SHARED_LIBRARIES/libhello-jni_intermediates/LINKED/libhello-jni.so)
FAILED: out/target/product/hikey960/obj_arm/SHARED_LIBRARIES/libhello-jni_intermediates/LINKED/libhello-jni.so 
/bin/bash -c "prebuilts/clang/host/linux-x86/clang-4691093/bin/clang++ -nostdlib -Wl,-soname,libhello-jni.so -Wl,--gc-sections -shared out/target/product/hikey960/obj_arm/lib/crtbegin_so.o out/target/product/hikey960/obj_arm/SHARED_LIBRARIES/libhello-jni_intermediates/hello-jni.o -Wl,--whole-archive  -Wl,--no-whole-archive   out/target/product/hikey960/obj_arm/STATIC_LIBRARIES/libunwind_llvm_intermediates/libunwind_llvm.a  out/target/product/hikey960/obj_arm/STATIC_LIBRARIES/libclang_rt.ubsan_minimal-arm-android_intermediates/libclang_rt.ubsan_minimal-arm-android.a  out/target/product/hikey960/obj_arm/STATIC_LIBRARIES/libcompiler_rt-extras_intermediates/libcompiler_rt-extras.a   out/target/product/hikey960/obj_arm/STATIC_LIBRARIES/libatomic_intermediates/libatomic.a out/target/product/hikey960/obj_arm/STATIC_LIBRARIES/libgcc_intermediates/libgcc.a -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--build-id=md5 -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined-version -Wl,--icf=safe -Wl,--hash-style=gnu -Wl,-m,armelf    -target arm-linux-androideabi -Bprebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/arm-linux-androideabi/bin -Wl,--exclude-libs,libunwind_llvm.a -Wl,--exclude-libs,libclang_rt.ubsan_minimal-arm-android.a -Wl,--no-undefined out/target/product/hikey960/obj_arm/lib/libc++.so out/target/product/hikey960/obj_arm/lib/libc.so out/target/product/hikey960/obj_arm/lib/libm.so out/target/product/hikey960/obj_arm/lib/libdl.so -o out/target/product/hikey960/obj_arm/SHARED_LIBRARIES/libhello-jni_intermediates/LINKED/libhello-jni.so out/target/product/hikey960/obj_arm/lib/crtend_so.o"
packages/apps/Settings/jni/hello-jni.c:42: error: undefined reference to 'TEEC_InitializeContext'
packages/apps/Settings/jni/hello-jni.c:50: error: undefined reference to 'TEEC_OpenSession'
packages/apps/Settings/jni/hello-jni.c:80: error: undefined reference to 'TEEC_InvokeCommand'
packages/apps/Settings/jni/hello-jni.c:95: error: undefined reference to 'TEEC_CloseSession'
packages/apps/Settings/jni/hello-jni.c:97: error: undefined reference to 'TEEC_FinalizeContext'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
[ 24% 6/25] build out/target/common/obj/APPS/Settings_intermediates/classes-combined.jar

What is JNI_H_INCLUDE? Does it include the path to tee_client_api.h?

that only includes jni.h i’m not too sure how would i include libteec?

Try adding something like LOCAL_C_INCLUDES += path/to/tee_client_api.h. Not sure if it has to be absolute or can be relative path. Try both and see which works. If both don’t work then sorry I don’t have better ideas.

so I decided to use a daemon and tcpip to communicate with the TA i have a question. How do you build a specific module (e.g settings app) with ./build-p-hikey960.sh? Or how do you use the the lunch as you do normally when just working with aosp normally to build one of the folders in examples?

So looking at Use prebuilt libraries  |  Android NDK  |  Android Developers, you might need something like

include $(CLEAR_VARS)
LOCAL_MODULE := teec-prebuilt
LOCAL_SRC_FILES := path/to/$(TARGET_ARCH_ABI)/libteec.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include #path to tee_client_api.h here
include $(PREBUILT_SHARED_LIBRARY)

Note that there’s a 32- and 64- bit version of libteec.so. I think you might have to point to the proper one. Also see android ndk - NDK: How to include Prebuilt Shared Library Regardless of Architecture - Stack Overflow for more info.

How do you build a specific module (e.g settings app) with ./build-p-hikey960.sh?

Just do
./build-p-hikey960.sh <list of make targets you want to build separated by a space>
e.g.
./build-p-hikey960.sh foo bar libteec vendorimage whatever

I may just use the tcpip route instead. So to build the aosp settings apk it would be
./build-p-hikey960 packages/apps/Settings?

I don’t know. You’ve to check the name of LOCAL_MODULE in the appropriate Android.mk file. Or name in the appropriate Android.bp file, although it seems less obvious in this case since there can be many sections with name in them. From packages/apps/Settings/Android.bp, seems like it might be just Settings, so perhaps ./build-p-hikey960 Settings. You can get a list of all make targets with ./build-p-hikey960 modules. ./build-p-hikey960 help might be useful too.

when i ran modules or help it does not give me a list of modules or help does not give me a man like response

https://pastebin.ubuntu.com/p/22N6XB9hgw

Can’t seem to find anything for ‘Settings’ app though.

could the normal build system build the optee modules? how just which build system would be able to compile both the optee_examples and the settings apps?