android JNI如何将CMAKE文件转换为带有支持文件的JNI Android.mk

huangapple 未分类评论46阅读模式
英文:

android JNI how to convert a CMAKE file to a JNI Android.mk with supporting files

问题

我有一个正常工作的CMake Android项目,现在我要将其迁移到一个新的应用程序,但我无法使用CMake。我需要将其转换为使用Android JNI / Android.mk和Application.mk。然而,我在处理两个支持文件目录时遇到了问题。其中一个具有命名空间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下的所有库文件显示尖括号中的任何内容都无法解析的引用。例如 `#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

    # 更多关于在Android Studio中使用CMake的信息,请阅读文档:https://d.android.com/studio/projects/add-native-code.html
    
    # 设置构建本机库所需的最低CMake版本。
    
    cmake_minimum_required(VERSION 3.4.1)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
    
    # 创建并命名库,将其设置为STATIC或SHARED,并提供其源代码的相对路径。
    # 您可以定义多个库,CMake会为您构建它们。
    # Gradle会自动将共享库与您的APK打包。
    
    add_library( # 设置库的名称。
            foobar_handler
    
            # 将库设置为共享库。
            SHARED
    
            # 提供到您的源文件的相对路径。
            main.cpp
            logger.h
            foobar_handler.cpp
            foobar_handler.hpp
            foo_test.hpp
            foo_test.cpp
    )
    
    # 搜索指定的预构建库并将路径存储为变量。
    # 因为CMake默认在搜索路径中包括系统库,所以您只需要指定要添加的公共NDK库的名称。
    # CMake在完成构建之前会验证库是否存在。
    
    find_library( # 设置路径变量的名称。
            log-lib
    
            # 指定要CMake定位的NDK库的名称。
            log
    )
    
    # 指定CMake应链接到目标库的库。您可以链接多个库,例如在此构建脚本中定义的库、预构建的第三方库或系统库。
    
    target_link_libraries( # 指定目标库。
            foobar_handler
    
            # 将目标库链接到NDK中包含的log库。
            ${log-lib}
    )
    
    target_include_directories(
            foobar_handler PRIVATE
            deps/otherlib/include
            deps/jni/include
    )

Application.mk

    APP_STL := c++_shared
    APP_CFLAGS += -DASIO_STANDALONE
    APP_CPPFLAGS += -std=c++14 -fexceptions -frtti
    APP_LDFLAGS += -llog -lgcov --coverage
英文:

I have a working Cmake android project and I am moving it to a new application where I cannot use Cmake. I need to convert it to use the Android JNI / Android.mk & Application.mk.. However I am having issues with the 2 directories of support files. One has namespace jni, the other appears to be mostly headers. Unfortunately the conversion does not seem as straight forward as the docs suggest.

The file structure is

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   

When I try to sync the files, all the library files under deps/jni/include show unresolved references to anything in angle brackets. IE #include <jni/bar.hpp>

Its been a while since I have done anything with C++. I notice that the jni cpp files all have namespace jni
I think I am missing a step for it to work in my Android.mk. The CMAKE compiles fine, but I cannot use it. I tried adding it to LOCAL_EXPORT_C_INCLUDES with no success.

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
)

Application.mk

APP_STL := c++_shared
APP_CFLAGS += -DASIO_STANDALONE
APP_CPPFLAGS += -std=c++14 -fexceptions -frtti
APP_LDFLAGS += -llog -lgcov --coverage

huangapple
  • 本文由 发表于 2020年4月11日 09:59:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/61151186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定