AndroidStudio使用CMake编译jni的C/C++文件
来源:互联网 发布:最火的网络电脑游戏荒 编辑:程序博客网 时间:2024/04/28 13:31
Android开发主流工具已变成AndroidStudio,新版AS已经支持CMake编译工具,可以用于编译C/C++文件,增强了Android调用jni代码的便捷性。相比于之前繁杂的ndk配置方式,cmake较为简单方便。
1、添加CMake支持
首先使用的AndroidStudio必须是2.2以上版本,在settings–Appearance&Behavior–System Settings–AndroidSDK中,确保下载安装了CMake、LLDB、NDK等必要组件。
使用CMake编译C/C++文件,有两种添加模式:
- 新建项目,在创建项目名称,配置包名时,勾选包含C/C++代码,如图.
- 在已有的项目中添加C/C++代码文件,只需要在app.gradle文件中配置如下代码
defaultConfig{//配置Cmake运行参数,在defaultconfig节点下配置 externalNativeBuild { cmake { cppFlags "-frtti -fexceptions" } } } //关联CMake的程序清单文件,path 指向cmake文件路径,此处为项目工程根目录下。 externalNativeBuild { cmake { path "CMakeLists.txt" } }
CMakeLists.txt文件如下
# Sets the minimum version of CMake required to build the native# library. You should either keep the default value or only pass a# value of 3.4.0 or lower.cmake_minimum_required(VERSION 3.4.1)# 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 it for you.# Gradle automatically packages shared libraries with your APK.add_library( # Sets the name of the library.AndroidStudio开始支持Cmake了,ndk感觉挺费劲的,这个是不是好玩点,,这里是要生成的库的文件名 libtest.so test # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). # Associated headers in the same location as their source # file are automatically included.对应的C文件的目录位置 src/main/cpp/Test.c)# Searches for a specified prebuilt library and stores the path as a# variable. Because system libraries are included 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 the# build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library.指定依赖库 test # Links the target library to the log library # included in the NDK.关联日志记录库文件,在ndk目录中 ${log-lib} )
如上文件中,主要注意add_library中配置你要生成so文件的名称,以及C/C++文件的路径;而target_link_libraries中指定依赖库的文件名。
如此clean project 然后make,就可以生成so文件,被Android上层java调用。类似如下:
private native static void open(String path); public native void close(); //加载lib库的so文件,要看请lib下面生成的so文件的名字,libtest.so static { System.loadLibrary("test"); }
2、最近项目一点零碎知识点
- 多屏幕适配效果优化
//在AndroidManifest.xml的application节点下,配置<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" />
- Activity快速导航返回到主Activity
使用android:parentActivityName
属性
//在activity节点下,配置属性<activity android:parentActivityName=".activity.WelcomeActivity" .../>
然后该配置属性的Activity,就可以在back按钮或者其他按钮事件处理中:
//NavUtils是android support包里面的工具类Intent intent = NavUtils.getParentActivityIntent(this); NavUtils.navigateUpTo(this, intent);
- EditText不自动获取屏幕焦点
//在需要屏蔽焦点自动获取的EditText的上一级布局文件中,配置属性android:focusableInTouchMode="true"
progress name
属性,在AndroidManifest.xml 中Activitiy或者service的节点属性配置progress name,如果不一致,可能会引起签名打包失败,无法安装apk。
本人技术小白,笔记仅供个人学习记录之中,若能有益于各位网友,也算荣幸。若有问题,肯请指出,莫要责怪。
0 0
- AndroidStudio使用CMake编译jni的C/C++文件
- Androidstudio编译c/c++jni方法
- Android Studio2.2配置MakeList使用cmake编译c文件
- AndroidStudio Cmake学习--如何漂亮的使用jni
- Android Studio 3.0 Jni开发环境配置 cmake编译 多个C/C++文件添加配置
- 使用AndroidStudio中的CMake开发JNI
- Android Cmake 编译c,c++源文件,实现JNI调用
- JNI NDK (AndroidStudio+CMake )实现Java调用C C++代码流程
- JNI NDK (AndroidStudio+CMake )实现C C++调用Java代码流程
- AndroidStudio JNI NDK CMake
- JNI NDK (AndroidStudio+CMake )
- NDK学习笔记<七> 使用AndroidStudio本身,编译C/C++文件生成SO文件
- Windows7下使用Dev-C 编译 JNI
- AndroidStudio调用C方法的介绍(JNI)
- AndroidStudio 编译C/C++文件生成SO文件
- AndroidStudio 编译C/C++文件生成SO文件
- AndroidStudio 编译C/C++文件生成SO文件
- AndroidStudio 编译C/C++文件生成SO文件
- React-native FlexBox初识
- GCD及其扩展GCD详解
- 再次被虐
- Scapy 中文文档:二、下载和安装
- 数据结构--线索二叉树
- AndroidStudio使用CMake编译jni的C/C++文件
- JS字符串替换函数全部替换方法
- easyui-1-基本介绍
- 知识库- 你身边的技术百科全书- CSDN
- ASP.NET Core MVC-WebAPi如何构建路由?
- VirtualAlloc
- nc 常用命令
- Scapy 中文文档:三、使用方法
- Gradle时Execute failed: java.io.IOException: Cannot run program "sdk-linux/build-tools/22.0.0/aapt"