mac编译android so文件,Mac 下 Android Studio 3.3 简单生成so文件

本文内容参考多位大神的截图和代码。只是总结一下,可以更快的配置好。

一. 配置环境安装 NDK。

2. Android Studio里安装。菜单栏 Tools ----> SDK Manger ----> SDK TOOLS .查找 到NDK下载。

二. 打开AndroidStudio 新建一个类,声明native方法。这个类是java与C/C++交互的中介,方法由java声明,由C/C++实现。

5747133d148915f05c78aefff0f4bcf9.png

8b5d93f2abaf7783250dd8fd1f278c5c.png

ce74c3d862966df95099765e3bafdf70.png

三.打开android studio终端,使用javac编译上述文件,生成class文件。

1.终端 cd myJNI类所在的目录

2.使用javac编译上述文件. javac myJNI.java 生成 myJNI.class

3.cd 到项目里的java目录。然后在java目录使用 javah -jni 包名.类名 命令生成.h头文件 然后就能看到生成了一个h文件。 注意一定要在java层目录下输入命令,不然不会报错:找不到xxx类

四. 新建一个jni文件夹,新建test.c,把.h里面的内容复制进去,并实现里面的函数。

1. 新建一个jni文件夹

59af294bf6c7abd4a597c35e28bb13c7.png

2.在jni文件夹下,新建test.c

ab136f994285ed18a04a853185707d33.png

3.把 ”javah -jni 包名.类名 命令生成.h头文件“ 生成的.文件放入jni文件夹下。并在test.c 实现它的方法。

fbb3818543e9d641ab4f4195067bc0bc.png

4.接着在jni文件夹下新建Android.mk和Application.mk文件。

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := MyJni

LOCAL_SRC_FILES := Test.c

include $(BUILD_SHARED_LIBRARY)

APP_ABI := all

86d055fc603939ff2f6752b8e84d55f1.png

五. 使用CMake 。创建CMakeLists.txt文件

1.在Project 目录下,右键app,点击新建File文件,命名为CMakeLists.txt

a3e7f877f481f105618a0b757702dba1.png

72cb8373f08c434cbd661e59e13c827e.png

2.配置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)

# 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.

MyJni #.so库名 可自定义

# Sets the library as a shared library.

SHARED

# Provides a relative path to your source file(s).

src/main/jni/test.c ) #源文件所在目录

# 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.

MyJni #.so库名 可自定义

# Links the target library to the log library

# included in the NDK.

${log-lib} )

3.右键app,点击Link C++ Project with Gradle,选择path:刚才的CMakeLists.txt,它会在 App build 生成相应的代码。

六. make projext 大功告成。

82fe94f14be607afeeb28aed02834ac8.png

七. so文件在本项目里的使用。

app Module 在Android 节点下:

sourceSets {

main() {

jniLibs.srcDirs = ['src/main/libs']

jni.srcDirs = [] //屏蔽掉默认的jni编译生成过程

}

}

标签:CMake,library,libraries,Mac,Studio,3.3,java,jni,Android

来源: https://blog.csdn.net/qq_24143647/article/details/89642794

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值