找不到 android_native_app_glue.h
近日在学习ndk时,书本的一个示例 中 需要导入android_native_app_glue.h 头文件,
示例代码很简单的就是直接就可以用了
#include <android_native_app_glue.h>
原来的项目使用的Ndk-build来构建的,
Android.mk如下
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LS_CPP=$(subst $(1)/,,$(wildcard $(1)/*.cpp))
LOCAL_MODULE := droidblaster
LOCAL_SRC_FILES := $(call LS_CPP,$(LOCAL_PATH))
LOCAL_LDLIBS := -landroid -llog
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
Application.mk如下
APP_ABI := armeabi armeabi-v7a x86
但现在使用的Cmake的方式,直接在头文件中引用,
#include <android_native_app_glue.h>
出现了构建错误,“android_native_app_glue.h not found”,作为一个初学者,一开始想的是不是ndk源码中是不是真的缺了这个文件,搜索后找到了,路径如下:android_sdk\ndk\22.1.7171670\sources\android\native_app_glue\android_native_app_glue.h
排除了这个原因,简单百度了一下 ,也一时没有找到答案,突然想起这个是一个 android-native-activity的例子,而谷歌应该有官方的sample也是用来展示的,在github找到了这个仓库
https://github.com/android/ndk-samples.git
当中就有一个 google-android-ndk\native-activity 的例子,其中的CMakeLists.txt的内容如下:
#
# Copyright (C) The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
cmake_minimum_required(VERSION 3.4.1)
# build native_app_glue as a static lib
set(${CMAKE_C_FLAGS}, "${CMAKE_C_FLAGS}")
add_library(native_app_glue STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
# now build app's shared lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -Wall -Werror")
# Export ANativeActivity_onCreate(),
# Refer to: https://github.com/android-ndk/ndk/issues/381.
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
add_library(native-activity SHARED main.cpp)
target_include_directories(native-activity PRIVATE
${ANDROID_NDK}/sources/android/native_app_glue)
# add lib dependencies
target_link_libraries(native-activity
android
native_app_glue
EGL
GLESv1_CM
log)
按照这个模版改一改,构建就成功了。
参照 这篇博客 https://blog.csdn.net/s402178946/article/details/54140271
说的是 引用的android_native_app_glue是一个独立的库,在ndk中是以源码的形式存在,
因此需要对这个它编译成静态库,再链接。
而 这个成功构建的原因
target_include_directories(native-activity PRIVATE
${ANDROID_NDK}/sources/android/native_app_glue)
对于jni/ndk 只是一个初学者,仅供参考,如果出错了,麻烦指出,谢谢
网友评论