代码之家  ›  专栏  ›  技术社区  ›  StarWind0

android JNI如何将CMAKE文件转换为JNI android.mk及其支持文件

  •  0
  • StarWind0  · 技术社区  · 4 年前

    我有一个正在运行的Cmake android项目,我正在将其转移到一个我无法使用Cmake的新应用程序中。我需要将其转换为使用Android JNI/Android.mk&Application.mk.。但是,我在2个支持文件目录上遇到了问题。一个具有名称空间jni,另一个似乎主要是标头。不幸的是,转换似乎并不像文档所建议的那样简单。

    文件结构为

    src/main/cpp
                deps/otherlib/include
                deps/jni/include
                Android.mk
                Application.mk
                main.cpp logger.h
                foobar_handler.cpp
                foobar_handler.hpp
                foo_test.hpp
                foo_test.cpp   
    

    当我尝试同步文件时,deps/jni/include下的所有库文件都显示了对尖括号中任何内容的未解析引用。IE #include <jni/bar.hpp>

    我已经有一段时间没有用C++做任何事情了。我注意到jni cpp文件都有 namespace jni 我想我缺少了一个在Android.mk中工作的步骤。CMAKE编译良好,但我无法使用它。我尝试将其添加到LOCAL_EXPORT_C_INCLUDES,但没有成功。

    Android.mk

    LOCAL_PATH:= $(call my-dir)
    include $(CLEAR_VARS)
    
    
    LOCAL_MODULE := native_crash_handler
    
    LOCAL_SRC_FILES := \
        main.cpp logger.h \
        foobar_handler.cpp \
        foobar_handler.hpp \
        foo_test.hpp \
        foo_test.cpp
    
    
    LOCAL_C_INCLUDES := $(LOCAL_PATH)
    
    LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
    
    LOCAL_CFLAGS := -fexceptions -fno-omit-frame-pointer
    LOCAL_CFLAGS += -Wall -Werror
    
    CXX11_FLAGS := -std=gnu++11
    LOCAL_CFLAGS += $(CXX11_FLAGS)
    
    LOCAL_EXPORT_CPPFLAGS := $(CXX11_FLAGS)
    
    LOCAL_LDLIBS := -llog
    
    
    
    
    include $(BUILD_SHARED_LIBRARY)
    

    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)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
    
    # 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.
            foobar_handler
    
            # Sets the library as a shared library.
            SHARED
    
            # Provides a relative path to your source file(s).
            main.cpp
            logger.h
            foobar_handler.cpp
            foobar_handler.hpp
            foo_test.hpp
            foo_test.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.
            foobar_handler
    
            # Links the target library to the log library
            # included in the NDK.
            ${log-lib}
    )
    
    target_include_directories(
            foobar_handler PRIVATE
            deps/otherlib/include
            deps/jni/include
    )
    

    应用.mk

    APP_STL := c++_shared
    APP_CFLAGS += -DASIO_STANDALONE
    APP_CPPFLAGS += -std=c++14 -fexceptions -frtti
    APP_LDFLAGS += -llog -lgcov --coverage
    
    0 回复  |  直到 4 年前