Android studio使用JNI实例
来源:互联网 发布:农村上网数据 编辑:程序博客网 时间:2024/06/06 06:59
我在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 JNICALLJava_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就完了,还有些地方没注释,可以后续研究一下
- Android studio使用JNI实例
- Android Studio JNI使用实例
- Android studio使用JNI实例
- Android studio使用JNI实例
- Android studio使用JNI实例
- Android studio使用JNI实例(1)
- JNI使用实例:Android Studio使用JNI编译so库
- Android Studio Jni 开发实例
- Android Studio JNI开发实例
- android studio JNI过程实例
- android studio JNI使用
- Android Studio使用JNI
- android studio使用jni
- Android Studio 使用JNI
- ANDROID STUDIO JNI使用
- Android studio 使用jni
- Android studio 使用jni
- Android studio使用JNI调用C语言实例详细解析
- 51nod1006 最长公共子序列Lcs dp
- composer 报 zlib_decode(): data error
- orhanobut/logger的使用
- 响应式图片<picture>标签兼容处理(IE9)
- 击杀大魔王
- Android studio使用JNI实例
- 单片机
- Java 中 byte、byte 数组和 int、long 之间的转换
- Jsp制作验证码
- 实现居中的方法
- dev
- Kafka 设计原理
- POJ 2406 Power Strings(KMP+最小循环节)
- 字符串问题---括号字符串的有效性和最长有效长度