android studio ndk编程(二)——两种方式编译
来源:互联网 发布:用友u8数据备份 编辑:程序博客网 时间:2024/06/06 01:25
序言
android studio自2.2版本以后,强化了c++的开发能力,可以直接在studio中采用ndk-build或者cmake方式将c/c++代码构建成native libary,然后打包到apk中,比直接用grandle编译c/c++代码更好.本文将介绍这两种方式的ndk开发.
环境配置
需要下载的工具 有:
The Android Native Development Kit (NDK): 让你能在 Android 上面使用 C 和 C++ 代码的工具集。
CMake: 外部构建工具。如果你准备只使用 ndk-build 的话,可以不使用它。
LLDB: Android Studio 上面调试本地代码的工具。
相关的环境配置可参考上一篇博客android studio NDK编程(一)
ndk-build方式编译
在以上环境都配置成功的条件下,ndk-build方式编译c/c++代码,需要以下五个步骤:
在android project中在main文件夹下建立与java目录同级的jni目录,如图所示
在jni目录下新建c/c++文件,文件内容为native方法对应的实现方法。实例为:
/* * 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. * */#include <string.h>#include <jni.h>/* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java */jstringJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ){#if defined(__arm__) #if defined(__ARM_ARCH_7A__) #if defined(__ARM_NEON__) #if defined(__ARM_PCS_VFP) #define ABI "armeabi-v7a/NEON (hard-float)" #else #define ABI "armeabi-v7a/NEON" #endif #else #if defined(__ARM_PCS_VFP) #define ABI "armeabi-v7a (hard-float)" #else #define ABI "armeabi-v7a" #endif #endif #else #define ABI "armeabi" #endif#elif defined(__i386__) #define ABI "x86"#elif defined(__x86_64__) #define ABI "x86_64"#elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */ #define ABI "mips64"#elif defined(__mips__) #define ABI "mips"#elif defined(__aarch64__) #define ABI "arm64-v8a"#else #define ABI "unknown"#endif return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI ".");}
- 在jni目录中新建Application.mk文件,此文件主要用于将要编译几种类型的so文件。实例代码:
APP_ABI := all
- 在jni目录中新建Android.mk文件。该脚本用来编译c/c++文件。实例代码:
# 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)#编译的模块名,也是activity调用的so库名字LOCAL_MODULE := hello-jni#需要编译的文件LOCAL_SRC_FILES := hello-jni.c#编译共享库include $(BUILD_SHARED_LIBRARY)
- 将Android.mk文件与grandle关联到一起,Gradle 会将 ndk-build作为一个依赖运行,然后将so打包到 apk 中,编译好的so在目录/build/intermediates/ndkBuild下面。使用方式:鼠标右键要链接的本地库所在的模块,选择Link C++ Project with Gradle,在弹出的对话框中选择Build System为ndk-build,再指定Amdroid.mk的路径点击ok即可.如图所示:
以上都完成后,就可以运行项目,会自动编译好so文件。最后附上两张编译完成后demo的结构图。
最后,demo下载地址:链接: https://pan.baidu.com/s/1nuJDP7F 密码: pus9
PS:如果引入第三方so文件,需要在grandle中设置abiFilters。
cmake方式编译
cmake方式编译可参考文章 在 Android Studio 2.2 中愉快地使用 C/C++
阅读全文
0 0
- android studio ndk编程(二)——两种方式编译
- ndk交叉编译两种方式
- JNI编程基础(二)——Android Studio NDK开发
- Android Studio NDK 编译
- Android Studio NDK编译.
- Android studio编译NDK
- Android studio ndk 编译
- Android Studio NDK 编译
- android NDK 环境配置两种方式
- android studio NDK编程
- Android Studio Ndk 编程
- Android Studio NDK 编程
- Android Studio Ndk 编程
- Android逆向基础笔记—Android NDK开发4之Android studio NDK自动编译
- Android JNI编程和NDK学习 --静态、动态两种方式实现JNI
- Android JNI编程和NDK学习 --静态、动态两种方式实现JNI
- Android JNI编程和NDK学习 --静态、动态两种方式实现JNI
- Android Studio NDK编译Error
- 浅谈C/C++中的typedef和#define
- 【转载】BAPI_GOODSMVT_CREATE FUNCITON FOR MIGO 各种移动类型 源代码参考
- python解析页面DOM树形成xpath列表,并计算DOM树的最大深度
- ==与equals()方法
- VMware虚拟机中如何配置静态IP
- android studio ndk编程(二)——两种方式编译
- 关于list集合传到jsp,在struts的select标签中显示下拉列表问题
- Apache Kafka源码分析-模块简介
- [JZOJ5163] 【NOIP2017模拟6.25】PS的烦恼
- Spring Boot 集成MyBatis
- jsp两种不同的include
- 剑指offer--合并两个排序的链表
- Hystrix系列-4-Hystrix的动态配置
- 给 TI BLE 添加自己UUID