Android studio使用JNI实例

     我在studio中使用jni,一直都是直接在创建项目的时候勾选了includeC++,可是我看到好多网上的教程,都是先创建项目,然后再配置jni需要的东西。我照着网上的教程,做了好久好久都没成功,附上别人的链接地址,你们去试试吧,我反正从来没成功过。都想放弃了。

http://www.cnblogs.com/wzben/p/5733571.html

http://blog.csdn.net/yanbober/article/details/45309049/

心力憔悴,好无力。难道是因为帖子太久,现在有了新的方式,故而那些教程再也教不了我们么?


你其实完全可以照着我之前写的那篇文章做,就不用花任何力气,也不必心力憔悴了。文章地址附上http://blog.csdn.net/hycfire/article/details/59517795


最后我还是搞通了。复制之前项目的配置,在需要添加的项目中照搬。在一次次尝试后,终于心力憔悴地写下以下步骤,仅供参考,你完全可以按照我上面说的那篇文章来做。

心力憔悴地实现步骤如下:

1.下载插件,CMake,NDK。

2.配置信息

2.1在local.properties 的末尾加上你NDK的路径,如图


2.2在 gradle.properties 的末尾加上          android.useDeprecatedNdk=true

这句版本高的话可有可无,是为了适配旧版本的NDK,不上图了。

2.3配置gradle这必须来图片


我知道你要不想敲代码,要复制粘贴。好吧。在defaultConfig节点末尾加上

externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }

在Android节点里加上

externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

好了。一会给你讲CMakeLists.text是干啥的,也是我这篇文章的重点。

3.编写java代码。

public class MyJNI {
    static {
        System.loadLibrary("native-lib");
    }
    public static native String stringFromJNI();
}

4.编写C代码     此处省略一万字。不过可以提供个例子。我自己也没有研究多少jni相关api,不浅学误人

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

extern "C"

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

这个函数命名你得稍微留意一下了。Java+包名_类名_方法名。错一个字母也不行,中间是下划线。

这就完了。接下来重点说说CMakeLists.text

先贴上text文件,在文件里给你注释

# 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最小的版本。
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.
#给library命名,一般是so文件,对应java代码中System.loadLibrary("native-lib");
#shared代表是不是提供真实路径;
#你的c代码路径,这个不能写错,对应你项目中,c代码存放的路径。
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 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.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
到这儿CMakeLists.text就完了,还有些地方没注释,可以后续研究一下
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页