android studio jni ndk 例子
来源:互联网 发布:如何评价北京折叠 知乎 编辑:程序博客网 时间:2024/06/07 12:47
谢谢 叶俊杰 网友的分享,我自己也做了些总结。
我使用的是android studio 2.2版本。把编写过程留下来,便于日后翻阅。
1. 创建一个新的工程
2. 创建一个新的类 JniTest.java 点击Build--Make Project 后
package com.jnitest;public class jniTest { static { System.loadLibrary("jnitest"); } public native static String getCLangString();}
配置ndk开发包,File->Project Structure...
或者,选中工程 点击F4键 sdk location 中 Android ndk location 选择配置ndk 的路径
保险起见 点击查看 local.properties 文件
ndk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk\\ndk-bundle
sdk.dir=C\:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk下一步
查看 项目目录下的 E:\appdemo\app\build\intermediates\classes\debug 生成了class文件
接下来 点击底下的Terminal窗口 cd 到当前项目src目录
E:\appdemo>cd E:\appdemo\app\src\main\java
使用javah 生成头文件
E:\appdemo\app\src\main\java>javah -jni com.jnitest.jniTest
在 E:\appdemo\app\src\main\java
下可以看到生成的com_jnitest_jniTest.h
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_csht_jniTest */#ifndef _Included_com_csht_jniTest#define _Included_com_csht_jniTest#ifdef __cplusplusextern "C" {#endif/* * Class: com_csht_jniTest * Method: getCLangString * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_csht_jniTest_getCLangString (JNIEnv *, jclass);#ifdef __cplusplus}#endif#endif
在项目的src 文件下的 创建jni文件夹 注意 (右键main - new - Folder - JniFolder )
将生成的文件com_jnitest_jniTest.h 剪切到jni文件夹下,(否则可能出现以下错误)Error:Execution failed for task ':app:compileDebugNdk'.
> Error: Your project contains C++ files but it is not using a supported native build system.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
http://tools.android.com/tech-docs/new-build-system/gradle-experimental.
在jni 目录下创建一个com_jnitest_jniTest.c 文件
生成 .so 库
此时报了 Error:(13, 0) Error: NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. For details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration.
<a href="openFile:E:\text\MyJniDemo\app\build.gradle">Open File</a>
没关系
此时 补全 .appdemo下MainActivity的代码
D:\Work\appDemo\app\build\intermediates\ndk\debug\lib\armeabi\libjnitest.so
> Error: Your project contains C++ files but it is not using a supported native build system.
Consider using CMake or ndk-build integration with the stable Android Gradle plugin:
https://developer.android.com/studio/projects/add-native-code.html
or use the experimental plugin:
http://tools.android.com/tech-docs/new-build-system/gradle-experimental.
在jni 目录下创建一个com_jnitest_jniTest.c 文件
#include "com_jnitest_jniTest.h"JNIEXPORT jstring JNICALL Java_com_csht_jniTest_getCLangString(JNIEnv *env, jobject obj){ return (*env)->NewStringUTF(env,"NDK JNI developer!");}
下一步, 在app的build.gradle;
1 defaultConfig下添加2 ndk{3 moduleName "jnitest" //生成的so名字4 abiFilters "armeabi", "armeabi-v7a", "x86" //输出指定三种abi体系结构下的so库5 }
点击build-> make project
生成 .so 库
此时报了 Error:(13, 0) Error: NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. For details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration.
<a href="openFile:E:\text\MyJniDemo\app\build.gradle">Open File</a>
没关系
我们在 工程下的gradle.properties 添加属性
android.useDeprecatedNdk=true
此时 补全 .appdemo下MainActivity的代码
1 package com.appdemo.jnitest; 2 3 import android.app.Activity; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.widget.TextView; 7 8 public class MainActivity extends Activity { 9 11 private TextView txtView;12 13 @Override14 protected void onCreate(Bundle savedInstanceState) {15 super.onCreate(savedInstanceState);16 setContentView(R.layout.activity_main);17 txtView= (TextView) findViewById(R.id.txt_jnitext);18 JniText jniText=new JniText();19 txtView.setText(jniText.getCLangString());20 }21 }
D:\Work\appDemo\app\build\intermediates\ndk\debug\lib\armeabi\libjnitest.so
0 0
- android studio jni ndk 例子
- Android Jni 例子,ndk
- android studio + NDK + JNI
- Android Studio NDK环境配置与JNI简单的例子
- Android Studio NDK jni 开发
- android studio NDK-JNI调用
- Android Studio ndk-Jni开发
- Android Studio NDK/JNI开发
- Android studio jni Ndk 配置
- Android Studio-NDK-JNI调用
- Android Studio ndk-Jni开发
- Android Studio NDK JNI 配置
- android studio初试ndk,jni
- Android Studio开发NDK JNI
- Android Studio NDK JNI 实践
- 【android 开 发 】 - Android studio 下 NDK Jni 开发 简单例子
- Android NDK之JNI使用例子
- android ndk jni 编程入门例子
- Linux常用命令(26)之yum在线安装
- Oracle 11g 使用TDE及ORA-28362
- 设计模式学习笔记——抽象工厂(Abstract Factory)模式
- lock ---1
- 总结阻塞和非阻塞,同步和异步
- android studio jni ndk 例子
- markdown编辑器
- maven-resources-plugin修改了我的证书
- tomcat配置JNDI数据源
- 倒排索引 基础知识
- 招标、投标、竞标、中标的概念整理
- java NIO总结
- cnetOS 安装 libreoffice(解决linux下找不到LibreOffice安装目录问题)
- 英语语法