Android studio NDK开发

15 篇文章 0 订阅

1、下载NDK开发工具

打开SDK Manager并下载如下工具

 

2、在Android studio中配置环境变量

 

3、在java中声明native方法

 

4、在main中和java同级创建jni文件夹,并将生成的头文件拷贝到jni文件夹中

5、创建c文件并实现声明的native方法

6、在app的build.gradle中,android下的defaultConfig模块和android模块下分别添加如下的代码

 

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.dongnaoedu.nkdfilecrypt"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        //使用Cmake工具
        externalNativeBuild {
            cmake {
                cppFlags ""
                abiFilters 'arm64-v8a','armeabi-v7a','x86'
            }
        }
    }

    sourceSets.main {
        jni.srcDirs = []
        jniLibs.srcDir 'src/main/libs'
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

 

7、在src目录下创建名为CMakeLists.txt的文件夹,并修改如下所示的三处

cmake_minimum_required(VERSION 3.4.1)



add_library( # Sets the name of the library.
# 设置so文件名称.
ndk_file_crypt
# Sets the library as a shared library.
SHARED
# 设置这个so文件为共享.
# Provides a relative path to your source file(s).
# 设置这个so文件为共享.
src/main/jni/ndk_file_crypt.c)


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 )

target_link_libraries( # Specifies the target library.
# 制定目标库.
ndk_file_crypt
# Links the target library to the log library
# included in the NDK.
${log-lib} )

8、点击Build->Make Project ,运行完成后会在app/build/intermediates中自动生成文件夹cmake,里面包含生成的.so文件

 

8、在代码中直接调用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值