Mac Android Studio Jni的总结

一、环境配置

1、安装NDK

在使用android studio进行jni开发的时候,需要配置ndk,ndk是Android的开发工具。

把下载下来的android-ndk-r15c-x86_64.bin放到你要解压的文件夹中。 之后打开你的终端,cd到android-ndk-r15c-x86_64.bin所在的目录,修改bin文件的执行权限,并进行安装。

$  chmod a+x  android-ndk-r15c-x86_64.bin  回车

$ ./android-ndk-r15c-x86_64.bin  回车

2、配置环境变量

在终端回到根目录下,输入open -e .bash_profile进入环境配置,

$ vi ~/.bash_profile

export NDK_PATH=/Users/linyong/Documents/android/android-ndk-r10e

export PATH=$NDK_PATH:$PATH

$ source ~/.bash_profile

二、创建jni工程

1、在项目的目录下创建关于native接口的java文档

public class UrlBusiness{

    static {
        System.loadLibrary("retrofit");
    }
    
    public static String native xxx();
}

2、编译项目生成class文件

3、通过终端进入到在app/build/intermediates/classes/debug,生成.h文件

     

$ javah -d jni com.j1.retrofit.UrlBusiness   //UrlBusiness所在的包名也要加上

将生成的com_j1_retrofit_UrlBusiness.h文件拷贝到项目的app/main/jni/的目录下;

4、创建.c文件实现响应的代码。最好有一个empty.c的空文件

5、创建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)

LOCAL_MODULE    := retrofit  //编译的so文件名
LOCAL_SRC_FILES := UrlBusiness.c //指定的编译文件

include $(BUILD_SHARED_LIBRARY)

三、Android  studio的jni环境的配置

1、 配置Android  studio 的NDK

在local.properties中添加配置

ndk.dir=/Users/j1/Documents/android/sdk-macosx/ndk-bundle

2、添加旧版本的支持

在gradle.properties中添加

android.useDeprecatedNdk=true 

3、添加ndk节点

在build.gradle中添加

 defaultConfig {
        ........

        ndk {
            moduleName "retrofit"
            abiFilters  "arm64-v8a", "armeabi", "armeabi-v7a","mips","mips64", "x86", "x86_64"
        }
    }

    sourceSets{ 
        main{
            jni.srcDirs = []
        }
    }

四、打开终端,进入到app/src/main/jni下生成so文件

$ndk-build

将会在src/main/下生成两个文件夹libs和obj,只要将libs文件夹重命名为jniLibs即可。

五、编译项目,可以获取到c层的代码

在java中只要调用UrlBusiness.getBaseUrl()就可以获取到对应c层代码对应的功能

猜你喜欢

转载自blog.csdn.net/nihaomabmt/article/details/81477885