简体   繁体   中英

How to add “jni/*.c/cpp” files to CMakeLists.txt file?

I have created a new project including c++ ndk. So it automatically created a CMakeLists.txt file and a cpp directory. But When I created a new directory named jni and tried to put test.c , Android.mk and Application.mk files on it, I got this error message when I wanted to sync my project:

This file is not part of the proejct.please include it to appropriaten build file

Android.mk :

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndktest
LOCAL_SRC_FILES := test.c

Application.mk :

APP_ABI := all

test.c :

#include <jni.h>
#include <string.h>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_m_ndktest_MainActivity_testJNI(JNIEnv *env, jobject instance)
{
    return env->NewStringUTF(hello.c_str());

}

JNIEXPORT jstring JNICALL
Java_com_example_m_ndktest_MainActivity_testJNI2(JNIEnv *env, jobject instance)
{
    return (*env)->NewStringUTF(env, "hellooooo");

}

How should I include my .mk files inside the CMakeLists.txt ?

CMakeLists.txt :

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

在此处输入图片说明

***Also I have a question, where can I find the tutorial about the STRANGE syntax that we should write in Android-ndk? (I mean the test.c syntax that I copied from the default native-lib.c )

Android Studio has the concept of external native builds. And for these external native builds, you have two options:

  • ndk-build
  • CMake

CMake seems to be the more modern one, so is probably more future-proof. I advice using that one.

This means that you write a CMakeLists.txt file (which is typically in your jni/ directory.)

You no longer need any of the .mk files, they are used by the ndk-build external native build, not the CMake one.

To indicate which of the external native build systems you use, you use the app's gradle file.

android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                cppFlags "..."
                arguments "..."
            }
        }
    }
}

In the CMakeLists.txt file, you describe how to build the native .so library. This includes listing all the .c and .cpp source files that are required to build it. This is done with CMake's add_library() macro.

So to summarize, your new project definition will consist of gradle files, and one (or more) CMakeLists.txt file, but no longer will you need any .mk files.

if I want to build a library with ndk-build like tesseract I shouldn'g fill the check box including support for c++ at the project wizard?

The check box is not a problem, you simply edit your build.gradle script and replace cmake that the wizard inserted with ndkBuild .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM