AndroidStudio JNI NDK CMake

原文地址 http://blog.csdn.net/qq_25806863/article/details/70739717
从AndroidStudio2.2开始,AS就整合了CMake来使用NDK。这种方式比之前用过的简单多了。这里记录一下简单使用。

当然,这些东西要先装好:

这里写图片描述

在新项目中使用

创建

在创建新项目时可以直接选中增加JNI。

这里写图片描述

然后一路next

这里写图片描述

点击finish完成创建。这时,AS就生成了ndk开发环境.

和不带JNI的项目新的区别

  1. 首先看目录结构,多了个cpp文件夹和cpp文件,多了个CMakeLists.txt文件
    这里写图片描述

下面是native-lib.cpp文件,返回Hello from C++

#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_sunlinlin_jniapp_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

下面是CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )
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.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
  1. 然后是构建文件的配置
    这里写图片描述

使用

在Activity中就正常使用:

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

直接运行就是下图:
这里写图片描述

在现有项目上增加jni

其实就是照猫画虎,手动加上上面的那些文件就行了。

  1. 在Java中定义载入库的名字和native方法名:
public class MainActivity extends AppCompatActivity {

    static{
        System.loadLibrary("myjni");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText(strFromJNI());
    }
    public native String strFromJNI();
}
  1. 新建myjin.cpp文件在src/main/cpp/myjni.cpp,里面实现strFromJNI这个方法。

方法名是Java_包名_方法名,把包名的.改成_

#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_sunlinlin_myapp_MainActivity_strFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from MYJNI";
    return env->NewStringUTF(hello.c_str());
}
  1. 在app目录下建立CMakeLists.txt文件,必须说这个名字
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
             myjni

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/myjni.cpp )

target_link_libraries( myjni
                        android
                        log)
  1. 然后在app的build.gradle文件中配置
    这里写图片描述

然后运行就能得到下图:
这里写图片描述

增加cpp文件

要增加一个hisjni.cpp ,就在cpp文件夹中建立hisjni.cpp:

#include <jni.h>
#include <string>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_sunlinlin_myapp_MainActivity_strFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from MYJNI";
    return env->NewStringUTF(hello.c_str());
}

然后在CMakeLists.txt中增加配置:

cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
             myjni

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/myjni.cpp )

target_link_libraries( myjni
                        android
                        log)

add_library( # Sets the name of the library.
             hisjni

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/hisjni.cpp )

target_link_libraries( hisjni
                        android
                        log)

就能在Java中使用了:

public class MainActivity extends AppCompatActivity {

    static{
        System.loadLibrary("myjni");
        System.loadLibrary("hisjni");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText(strFromHisJNI());
    }
    public native String strFromJNI();
    public native String strFromHisJNI();
}

这里写图片描述

更多配置。

上面只是简单的使用,在build.gradle中还能有更多的灵活的配置,参考官方文档


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值