特此说明:按照网上的教程Android Studio 3.0版本会出现无法生成.so文件的问题,此方法针对Android Studio 3.0版本解决以上问题。

感谢博客作者:zzyyppqq,Android开发者社区

说明:

通过Jni调用在界面上显示“hello Maybe”。

方法步骤:

1.Android Studio 3.0配置

下载好NDK,并设置路径


image.png

2.新建工程HelloNDK并新建jni文件夹

新建NdkJniUtils.java

package com.example.android.hellondk;/** * Created by Maybe on 2017/12/7. */public class NdkJniUtils {    static {        //通过此方法加载库,库的名字(JniTest)由你自己命名,但是需要与后面CMakeLists.txt文件里一致        System.loadLibrary("JniTest");    }       //native关键字  自定义调用函数sayHello()     public native String sayHello();}

然后在main目录下新建jni文件夹


Jni.png

3.生成.h头文件

有两种方法,一种是在terminal中进入到相应的路径使用命令行代码生成;
命令行命令:javah com.kissdream.androidjnitest.myJNIUtils
另一种是在Android Studio 3.0中进行配置,具体步骤如下。

(1)执行操作 File-Setting-Tools-External Tools

image.png
(2)在工程里面找到你新建的NdkJniUtils.java,执行如下图中操作。
image.png

正常情况下会在新建的jni文件夹下面产生一个后缀名为.h的头文件,打开该头文件。


image.png

4.编辑.c文件

(3)在jni文件夹里新建.c文件,我这里为main.c。将头文件中全部内容复制到main.c里并实现sayHello方法如下:(里面逻辑属于c语言内容,给我们返回一个字符串Hello Maybe)

/* DO NOT EDIT THIS FILE - it is machine generated */#include /* Header for class com_example_android_hellondk_NdkJniUtils */#ifndef _Included_com_example_android_hellondk_NdkJniUtils#define _Included_com_example_android_hellondk_NdkJniUtils#ifdef __cplusplusextern "C" {#endif/* * Class:     com_example_android_hellondk_NdkJniUtils * Method:    sayHello * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_example_android_hellondk_NdkJniUtils_sayHello  (JNIEnv *env, jobject jobj){    return (*env)->NewStringUTF(env,"Hello Maybe");  }#ifdef __cplusplus}#endif#endif

5.添加CMakeLists.txt文件

在app目录下添加CMakeLists.txt文件,如图所示


image.png

该文件里面的内容如下(只需要注意汉字注释的位置,#为注释)

# 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.#CMakeLists.txtcmake_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.      # 设置so文件名称.与上面加载的一致       JniTest       # Sets the library as a shared library.      # 设置这个so文件为共享.       SHARED       # Provides a relative path to your source file(s).       # 提供你C文件的路径,这里我的main.c文件放在了刚才新建jni路径里.       src/main/jni/main.c)#此处的括号不可删除,是add_library的括号      # 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.            # 制定目标库,此处与你加载的库名称一致.            JniTest            # Links the target library to the log library            # included in the NDK.            ${log-lib} )

6.修改app下build.gradle

具体修改内容分为:

  1. cmake工具 在defaultConfig节点内添加
  2. 配置CMakeLists.txt路径 在andriod节点内添加
  3. 同步(Sync Now)
    此处添加的内容可不用修改,复制到你的工程即可(注意是复制添加的内容,其他依赖之类的根据你的工程而定
apply plugin: 'com.android.application'android {    compileSdkVersion 26    defaultConfig {        applicationId "com.example.android.hellondk"        minSdkVersion 21        targetSdkVersion 26        versionCode 1        versionName "1.0"        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"        // 1.需要添加的位置 使用Cmake工具        externalNativeBuild {            cmake {                cppFlags ""                //生成多个版本的so文件                abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64'            }        }    }    //2.需要添加的位置 配置CMakeLists.txt路径    externalNativeBuild {        cmake {            path "CMakeLists.txt"  // 设置所要编写的c源码位置,以及编译后so文件的名字        }    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    implementation 'com.android.support:appcompat-v7:26.1.0'    implementation 'com.android.support.constraint:constraint-layout:1.0.2'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'com.android.support.test:runner:1.0.1'    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'}

7.生成.so文件

执行操作:Rebuild Project,正常情况下在app/build/intermediates/cmake/debug/obj路径中会生成.so文件

image.png

注意:如果你之有一个文件夹里生成了.so文件,那么你需要在jni文件夹下新建Application.mk文件,该文件里只有一句话APP_ABI := all,然后重新Rebuild Project。

8.调用

代码如图不多说了,自己体会吧。

package com.example.android.hellondk;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView;/** * Project Name: * Class desc. : *     date    : 2017/12/7 *    author   : Maybe **/public class MainActivity extends AppCompatActivity {    private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView)findViewById(R.id.text);        NdkJniUtils jni = new NdkJniUtils();        textView.setText(jni.sayHello());    }}

9.结语

image.png

效果如图,以上。

更多相关文章

  1. android 零星调试笔记(二)
  2. Android(安卓)studio 使用 ImageView 加载 gif 文件
  3. Settings设置页面的Preference使用方法
  4. Android(安卓)Gradle文件下获取自定义properties文件,并且获取值
  5. 踩坑日记-Android版本9 API28 NetworkSecurityConfig
  6. Android(安卓)Studio layout中的布局文件创建分类子目录
  7. Android手动清除APP中Data目录下的文件
  8. android之基于Echart的图表
  9. Android(安卓)9 使用okhttp3报错 CLEARTEXT communication to XX

随机推荐

  1. Android手机开发:ListView的使用
  2. 使用Toast显示提示信息框
  3. Android(安卓)Studio项目.gitignore
  4. Android(安卓)Code Review
  5. android 开发真机测试,无法安装调试解决
  6. Android(安卓)Push Notifications using
  7. [Android] 简单的状态机实现
  8. download android-4.0 source code
  9. 自定义PopupWindow动画效果
  10. CTS/GTS 常见问题汇总