概述
在android NDK中开发jni, 可下的c和cpp等文件, 得到.so文件.
编译环境配置
下载NDK: https://developer.android.com/ndk/guides 上面有使用说明,有时间可仔细阅读.
下载ndk后, 不需要安装, 解压到本地某个目录,配置环境变量:
Ubuntu环境配置:
1. 配置NDK环境变量, 执行如下命令:
sudo gedit /etc/profile
说明: 打开全局环境变量文件,添加NDK的环境变量,就额可以在任意地方使用ndk-build命令了.
#set android ndk env
export ANDROID_NDK_HOME=/home/hulk/tools/android-ndk-r15c
export PATH=${PATH}:${ANDROID_NDK_HOME}:${ANDROID_NDK_HOME}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin
说明: 我目前使用的还是android-ndk-r15c, 最新版本已经23了,但是ndk里面结构已经变化了,好像会有问题,还没有研究怎么用.
2. 使环境变量立刻生效, 执行如下命令:
source /etc/profile
3. ceshi NDK是否配置完成:
XXXXX-contacts155$ ndk-build -h
Usage: make [options] [target] ...
Options:
-b, -m Ignored for compatibility.
-B, --always-make Unconditionally make all targets.
............只要输出一大串,就成功了.
就这样, NDK开发环境就配置好了,开始使用.
举例说明
如下图;
手动编译jni目录
jni最重要说明:
1. 模块根目录下,新建jni目录, 里面新建两个必不可少的编译文件 Android.mk 和 Application.mk
(1) Application.mk:
NDK_MODULE_PATH := $(call my-dir)
APP_OPTIM := release
APP_STL := stlport_static
#产出的框架 必选
APP_ABI := armeabi armeabi-v7a x86
# 编译版本: 可选,可以不配置
APP_PLATFORM := android-25
(2) Android.mk
# Copyright (C) 2009 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.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#参与编译的c文件和c++文件
LOCAL_SRC_FILES := centraldir.c \
com_hulk_contacts_support_NativeManager.cpp\ #注意出口类的名称必须与java对应
BinPatch.cpp\
BinaryPatch.cpp\
quickfinder.cpp\
telutil.cpp
LOCAL_LDLIBS := -llog
#output so filename
LOCAL_MODULE := hulk-jni
include $(BUILD_SHARED_LIBRARY)
2. 直接在jni目录下编写c和c++文件, 此处省略了, 看官们自己脑补. 大概是这样:
#include <stdio.h>
int main(const int argc,const char* args[]){
const char* str = "hello world!";
printf("%s\n",str);
return 0;
}
3. 编译
执行如下命令:
ndk-build -C ./jni
只要不出现错误,就能看到编译出来的so文件了, 直接看下面的例子:
hulk@hulk-PC:~/byod/code/byod_new3/Byod-contacts155$ ndk-build -C ./jni Android NDK: APP_PLATFORM set to unknown platform: android-25. make: Entering directory `/home/hulk/byod/code/hulk-jni/jni' /home/hulk/tools/android-ndk-r15c/build/core/setup-app-platform.mk:115: *** Android NDK: Aborting . Stop. make: Leaving directory `/home/hulk/byod/code/hulk-jni/jni' hulk@hulk-PC:~/byod/code/byod_new3/Byod-contacts155$ ndk-build -C ./jni Android NDK: WARNING: APP_PLATFORM android-26 is higher than android:minSdkVersion 1 in /home/hulk/byod/code/hulk-jni/AndroidManifest.xml. NDK binaries will *not* be comptible with devices older than android-26. See https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md for more information. make: Entering directory `/home/hulk/byod/code/hulk-jni/jni' [armeabi] Compile thumb : hulk-jni <= centraldir.c [armeabi] Compile++ thumb: hulk-jni <= com_hulk_contacts_support_NativeManager.cpp In file included from /home/hulk/byod/code/hulk-jni/jni/com_hulk_contacts_support_NativeManager.cpp:23: /home/hulk/byod/code/hulk-jni/jni/quickfinder.h:32:20: warning: 'getDuoyinzi' has C-linkage specified, but returns incomplete type 'const list<std::string>' (aka 'const list<basic_string<char, char_traits<char>, allocator<char> > >') which could be incompatible with C [-Wreturn-type-c-linkage] const list<string> getDuoyinzi(int unicode); ^ 1 warning generated. [armeabi] Compile++ thumb: hulk-jni <= BinPatch.cpp [armeabi] Compile++ thumb: hulk-jni <= BinaryPatch.cpp [armeabi] Compile++ thumb: hulk-jni <= quickfinder.cpp In file included from /home/hulk/byod/code/hulk-jni/jni/quickfinder.cpp:6: /home/hulk/byod/code/hulk-jni/jni/quickfinder.h:32:20: warning: 'getDuoyinzi' has C-linkage specified, but returns incomplete type 'const list<std::string>' (aka 'const list<basic_string<char, char_traits<char>, allocator<char> > >') which could be incompatible with C [-Wreturn-type-c-linkage] const list<string> getDuoyinzi(int unicode); ^ 1 warning generated. [armeabi] Compile++ thumb: hulk-jni <= telutil.cpp [armeabi] SharedLibrary : libhulk-jni.so [armeabi] Install : libhulk-jni.so => libs/armeabi/libhulk-jni.so [armeabi-v7a] Compile thumb : hulk-jni <= centraldir.c [armeabi-v7a] Compile++ thumb: hulk-jni <= com_hulk_contacts_support_NativeManager.cpp In file included from /home/hulk/byod/code/hulk-jni/jni/com_hulk_contacts_support_NativeManager.cpp:23: /home/hulk/byod/code/hulk-jni/jni/quickfinder.h:32:20: warning: 'getDuoyinzi' has C-linkage specified, but returns incomplete type 'const list<std::string>' (aka 'const list<basic_string<char, char_traits<char>, allocator<char> > >') which could be incompatible with C [-Wreturn-type-c-linkage] const list<string> getDuoyinzi(int unicode); ^ 1 warning generated. [armeabi-v7a] Compile++ thumb: hulk-jni <= BinPatch.cpp [armeabi-v7a] Compile++ thumb: hulk-jni <= BinaryPatch.cpp [armeabi-v7a] Compile++ thumb: hulk-jni <= quickfinder.cpp In file included from /home/hulk/byod/code/hulk-jni/jni/quickfinder.cpp:6: /home/hulk/byod/code/hulk-jni/jni/quickfinder.h:32:20: warning: 'getDuoyinzi' has C-linkage specified, but returns incomplete type 'const list<std::string>' (aka 'const list<basic_string<char, char_traits<char>, allocator<char> > >') which could be incompatible with C [-Wreturn-type-c-linkage] const list<string> getDuoyinzi(int unicode); ^ 1 warning generated. [armeabi-v7a] Compile++ thumb: hulk-jni <= telutil.cpp [armeabi-v7a] SharedLibrary : libhulk-jni.so [armeabi-v7a] Install : libhulk-jni.so => libs/armeabi-v7a/libhulk-jni.so [x86] Compile : hulk-jni <= centraldir.c [x86] Compile++ : hulk-jni <= com_hulk_contacts_support_NativeManager.cpp In file included from /home/hulk/byod/code/hulk-jni/jni/com_hulk_contacts_support_NativeManager.cpp:23: /home/hulk/byod/code/hulk-jni/jni/quickfinder.h:32:20: warning: 'getDuoyinzi' has C-linkage specified, but returns incomplete type 'const list<std::string>' (aka 'const list<basic_string<char, char_traits<char>, allocator<char> > >') which could be incompatible with C [-Wreturn-type-c-linkage] const list<string> getDuoyinzi(int unicode); ^ 1 warning generated. [x86] Compile++ : hulk-jni <= BinPatch.cpp [x86] Compile++ : hulk-jni <= BinaryPatch.cpp [x86] Compile++ : hulk-jni <= quickfinder.cpp In file included from /home/hulk/byod/code/hulk-jni/jni/quickfinder.cpp:6: /home/hulk/byod/code/hulk-jni/jni/quickfinder.h:32:20: warning: 'getDuoyinzi' has C-linkage specified, but returns incomplete type 'const list<std::string>' (aka 'const list<basic_string<char, char_traits<char>, allocator<char> > >') which could be incompatible with C [-Wreturn-type-c-linkage] const list<string> getDuoyinzi(int unicode); ^ 1 warning generated. [x86] Compile++ : hulk-jni <= telutil.cpp [x86] SharedLibrary : libhulk-jni.so [x86] Install : libhulk-jni.so => libs/x86/libhulk-jni.so make: Leaving directory `/home/hulk/byod/code/hulk-jni/jni' hulk@hulk-PC:~/byod/code/byod_new3/Byod-contacts155$
至此,.so文件就饿生成在与jni同级别沐浴露的libs目录下了:
libs/armeabi/libhulk-jni.so
libs/armeabi-v7a/libhulk-jni.so
libs/x86/libhulk-jni.so
总结
Android jni开发三要点
1. 下载NDK,配置环境变量
2. 新建jni目录.仙剑Android.mk 和 Application.mk
3. 编辑c和c++文件
4. 编译: ndk-build -C ./jni