Android Studio上实现一个最简单的ndk项目的步骤

这2天在测试ndk,但是看了几个blog都没实现。后来看官方文档,终于实现了一个最简单的demo,放blog上自己忘记的时候也可以看下。

1,首先需要配置好ndk环境(建议下载ndk10以上的版本,否则可能报错),并在AS上配置好



在项目文件gradle.properties文件末尾加

android.useDeprecatedNdk=true



2,新建一个android项目,再新建一个调用本地接口的类,并在里面加载库

public class JniUtils {

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

    public native String getHelloString();
}


在MainActivity中调用上面接口的方法

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        JniUtils utils=new JniUtils();
        TextView tv=new TextView(this);
        tv.setText(utils.getHelloString());
        setContentView(tv);
    }
}


3,在AS上点击Bulid选择Make Project,生成class文件,找到生成class文件的目录


打开cmd命令,先进入到当前class的debug目录

cd D:\androidspace\gzydemo\TestNDK\app\build\intermediates\classes\debug

再运行javah命令生成JniUtils类的.h文件

javah -jni com.gzy.testndk.JniUtils

4,把生成的.h文件复制到项目的jni目录下

在jni目录下新建hello-jni.c文件,方法直接复制.h文件中的接口方法就可以,只需要实现该接口

#include <jni.h>
//
// Created by Administrator on 2016-11-25.
//

JNIEXPORT jstring JNICALL Java_com_gzy_testndk_JniUtils_getHelloString
        (JNIEnv *env, jobject obj){
    return (*env)->NewStringUTF(env,"jni-hello");
}


在jni目录新建Android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)


5,在项目build.gradle文件添加如下

ndk{
    moduleName "hello-jni"
}


到这里直接运行项目就可以了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Android Studio创建NDK项目步骤如下: 1. 创建新项目:在Android Studio中选择“Start a new Android Studio project”。 2. 添加NDK支持:在“Add an Activity to Mobile”页面中,选择“Native C++”作为Activity类型,然后单击“Next”。 3. 配置C++支持:在“Customize C++ Support”页面中,选择“C++11”作为C++标准库,并选择“None”作为异常支持。单击“Finish”。 4. 配置gradle文件:在项目的build.gradle文件中,添加以下代码: ``` externalNativeBuild { cmake { path "CMakeLists.txt" } } ``` 5. 配置CMakeLists.txt文件:在项目的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 ) # Searches for a specified prebuilt library and stores the path as a # variable. Because system libraries are included 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 the # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} ) ``` 6. 编写C++代码:在src/main/cpp目录下创建native-lib.cpp文件,并添加以下代码: ``` #include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_example_myapplication_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } ``` 7. 在MainActivity.java文件中添加以下代码: ``` static { System.loadLibrary("native-lib"); } public native String stringFromJNI(); ``` 8. 运行应用程序:构建并运行应用程序,您将看到“Hello from C++”消息。 --相关问题--: 1. 如何在Android Studio中使用OpenCV? 2. 如何在NDK中使用CMake? 3.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值