ancar31 ancar31 - 1 month ago 17
Android Question

undefined reference native library in Android

I am developing an app for Android that needs a small library of c. This C library is compiled with GCMake included in SDK Tools. I use JNI function for connect java and C.

This is jni file (jni_wrapper.c)

#include <jni.h>
#include "check.h"

JNIEXPORT void JNICALL Java_package_ui_fragments_menu_WeatherFragment_checkData( JNIEnv* env, jobject instance,
jobject data) {

checkData(data);
}


My check.h file

typedef struct {
char *date;
char *query;
} Data;

void checkData(Data* data);


My check.c file

#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
.... More imports


void checkData(Data* data) {
//implementation
}


Directories structure

<root>/app/src/main/checkdata/
jni_wrapper.c
check.c
check.h
other_file_dependent_check.c
other_file_dependent_check.c


My CMakeLists.txt

project(checkdata)

add_library(checkdata SHARED
jni_wrapper.c)

target_link_libraries(checkdata
android
log)


My build.gradle file (gradle plugin version 2.2.2)

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

externalNativeBuild {
cmake {
path "src/main/checkdata/CMakeLists.txt"
}
}


FAILURE: Build failed with an exception.


  • What went wrong:
    Execution failed for task ':app:externalNativeBuildDebug'.


    Build command failed.
    Error while executing '/Users/adrian/Library/Android/sdk/cmake/3.6.3155560/bin/cmake' with arguments {--build /App/path/app/.externalNativeBuild/cmake/debug/mips64 --target checkdata}
    [1/2] Building C object CMakeFiles/checkdata.dir/jni_wrapper.c.o
    [2/2] Linking C shared library /Users/adrian/Documents/devel/App/path/app/build/intermediates/cmake/debug/obj/mips64/libcheckdata.so
    FAILED: : && /Users/bob/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -target mips64el-none-linux-android -gcc-toolchain /Users/bob/Library/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/bob/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libcheckdata.so -o /App/path/app/build/intermediates/cmake/debug/obj/mips64/libcheckdata.so CMakeFiles/checkdata.dir/jni_wrapper.c.o -landroid -llog -lm && :
    CMakeFiles/checkdata.dir/jni_wrapper.c.o: In function
    Java_package_ui_fragments_menu_WeatherFragment_checkData':
    /Users/bob/Documents/devel/App/path/src/main/checkdata/jni_wrapper.c:7: undefined reference to
    checkData'
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.



I need help with error /Users/bob/Documents/devel/App/path/src/main/checkdata/jni_wrapper.c:7: undefined reference to `checkData'

Answer

add_library(Checkdata check.c)

Comments