Android Studio 3.0 JNI 开发环境配置 ndk cmake编译

Android Studio 3.0 配置JNI环境走了很多弯路  这里记录一下,通过  javah + Android.mk + Application.mk 的方法行不通在AS 3.0之后,在根目录添加 gradle.properties:android.useDeprecatedNdk=true (作用支持旧版本的NDK)在3.0之后的版本不起作用。这里通过cmake

一:开始配置

1.新建一个支持C++的项目





2.安装NDK

File>Setting>System Setting>Android SDK


生成配置文件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.
             jni-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/jni-lib.c      #c代码文件路径
             src/main/cpp/native-lib.cpp )
# 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.
                       jni-lib
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

3.解耦native 本地方法

创建jni 包并创建 JNI.java 类


package android.itcast.com.testjni.jni;

/**
 * Created by lenovo on 2018/3/31.
 */

public class JNI {

    static {
        System.loadLibrary("jni-lib");
    }

    public native String stringFromJNI();
}

在cpp中添加  .c文件

#include <stdio.h>
#include <stdlib.h>
#include <jni.h>

/**
 * jstring :返回值
 * Java_全类名_方法名
 * JNIEnv* env:里面有很多方法
 * jobject jobj:谁调用了这个方法就是谁的实例
 * 当前就是JNI.thi
 */
jstring  Java_android_itcast_com_testjni_jni_JNI_stringFromJNI(JNIEnv* env,jobject jobj){
//jstring     (*NewStringUTF)(JNIEnv*, const char*);
    char* text = "I am from c !";
    return  (*env)->NewStringUTF(env,text);
}

4.在UI中调用native方法

package android.itcast.com.testjni;

import android.itcast.com.testjni.jni.JNI;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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
        String result = new JNI().stringFromJNI();
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(result);
    }

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

5.编译项目



代码链接

链接:https://pan.baidu.com/s/16PZy81xCHXw12DKQw7Vg0Q 密码:3453



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值