Windows下,Eclipse的Android NDK(r8e) 配置

2023-11-10

一、关于NDK
Android NDK全称:Native Development Kit,即本地开发包。 
1、NDK是一系列工具的集合。 
NDK提供了一系列的工具,这些工具对开发者的帮助是巨大的。 
它们能帮助开发者快速开发C(或C++)的动态库,并能自动将so库和java应用一起打包成apk。
NDK集成了交叉编译器,并提供了相应的mk文件隔离CPU、平台、ABI等差异,
开发人员只需要简单修改mk文件(指出“哪些文件需要编译”、“编译特性要求”等),就可以创建出so库。 
NDK可以自动地将so和Java应用一起打包,极大地减轻了开发人员的打包工作。  
2、NDK提供了一份稳定、功能有限的API头文件声明。 
Google明确声明该API是稳定的,在后续所有版本中都稳定支持当前发布的API。
从该版本的NDK中看出,这些API支持的功能非常有限,包含有:
C标准库(libc)、标准数学库(libm)、压缩库(libz)、Log库(liblog)。

二、NDK实例的实现
对于Windows环境下NDK的开发,如果使用的NDK是r7之前的版本,必须要安装Cygwin才能使用NDK;
从r7开始,Google的提供了一个ndk-build.cmd的脚本,可以直接用它编译,而不需要使用Cygwin了
只需要为Eclipse Android工程添加一个Builder,就能让Eclipse自动编译NDK。

本文是讲述NDK-r8e下的实现实例。
下面是在windows下为NDK配置自动编译的builder的过程(对于Linux,只需将ndk-build.cmd修改为ndk-build就可以了)。
(1)先下载安装NDK
下载地址:http://developer.android.com/sdk/ndk/index.html
下载后解压缩就可以用了。
(2)新建工程和jni文件夹
打开Eclipse,新建或导入一个Android工程(我的取名为NdkTest),在工程目录下新建jni文件夹;
该jni文件夹就用来保存NDK需要编译的文件代码等。
(3)新建并配置一个Builder:
 (a)Project->Properties->Builders->New,新建一个Builder。 
 (b)在弹出的【Choose configuration type】对话框,选择【Program】,点击【OK】: 
 (c)在弹出的【Edit Configuration】对话框中,配置选项卡【Main】。
      在“Name“中输入新builders的名称(我取名为Ndk_Builder)。
      在“Location”中输入nkd-build.cmd的路径。
      (我的是E:\adt-bundle-windows-x86-20130522\ndk-r8e\ndk-build.cmd,   路径不能有空格
        根据各自的ndk路径设置,也可以点击“Browser File System…”来选取这个路径)。
      在“Working Diretcoty”中输入项目工程的路径${workspace_loc:/project}
      (我的是${workspace_loc:/NdkTest},也可以点击“Browse Workspace”来选取本工程目录)。
  (d)【Edit Configuration】对话框中,配置选项卡【Refresh】。
      勾选“Refresh resources upon completion”,
      勾选“The entire workspace”,
      勾选“Recuresively include sub-folders”。
   (e)【Edit Configuration】对话框中,配置选项卡【Build options】。
       勾选“After a “Clean””,
      勾选“During manual builds”,
      勾选“During auto builds”,
      勾选“Specify working set of relevant resources”。 
      点击“Specify Resources…”
      勾选本工程的“jni“目录,点击”finish“。
点击“OK“,完成配置。到这里Eclipse就能自动调用NDK编译jni目录下的C/C++代码了。

 

出现的提示信息如下:
Multiple markers at this line
  - Syntax error
  - Type 'JNIEnv' could not be resolved
  - Type 'JNICALL' could not be resolved

是由于没有将jni.h导入的缘故,而这个文件在ndk的目录下面。所以,参照以下步骤:
Project Properties -> C/C++ General -> Path and Symbols
选择include标签,Add -> $Android_NDK_HOME/platforms/android-14/arch-arm/usr/include
且选中All languages.
最后Apply -> OK
这样错误就解决了。

 

 

(4)新建JniClient.java
在工程中新建一个JniClient.java(为了调用C/C++代码),保存在MainActivity.java目录下;其内容如下:

[java]  view plain copy
  1. package com.example.ndktest;  
  2. public class JniClient {  
  3.     static public native String HelloWorld();  
  4. }  
[java]  view plain copy
  1. package com.example.ndktest;  
  2. public class JniClient {  
  3.     static public native String HelloWorld();  
  4. }  

(5)生成JniClinet.class文件
用cmd命令定位到JniClient.java所在目录,输入“javac JniClient.java“后回车,生成JniClinet.class文件。
(如果是用的Eclipse建的工程,在bin\classes\com\example\ndktest目录下就已经有JniClinet.class文件了)。
(6)生成C++头文件
将JniClinet.class拷贝到bin\classes\com\example\ndktest目录,将cmd命令定位到bin\classes目录,
输入”javah com.example.ndktest.JniClient“后回车,在bin\classes目录下就生成了C++头文件了。
com_example_ndktest_JniClient.h的文件内容如下:

[cpp]  view plain copy
  1. /* DO NOT EDIT THIS FILE - it is machine generated */  
  2. #include <jni.h>   
  3. /* Header for class com_example_ndktest_JniClient */  
  4.   
  5. #ifndef _Included_com_example_ndktest_JniClient  
  6. #define _Included_com_example_ndktest_JniClient   
  7. #ifdef __cplusplus   
  8. extern "C" {  
  9. #endif   
  10. /* 
  11.  * Class:     com_example_ndktest_JniClient 
  12.  * Method:    HelloWorld 
  13.  * Signature: ()Ljava/lang/String; 
  14.  */  
  15. JNIEXPORT jstring JNICALL Java_com_example_ndktest_JniClient_HelloWorld  
  16.   (JNIEnv *, jclass);  
  17.   
  18. #ifdef __cplusplus   
  19. }  
  20. #endif   
  21. #endif  
[cpp]  view plain copy
  1. /* DO NOT EDIT THIS FILE - it is machine generated */  
  2. #include <jni.h>  
  3. /* Header for class com_example_ndktest_JniClient */  
  4.   
  5. #ifndef _Included_com_example_ndktest_JniClient  
  6. #define _Included_com_example_ndktest_JniClient  
  7. #ifdef __cplusplus  
  8. extern "C" {  
  9. #endif  
  10. /* 
  11.  * Class:     com_example_ndktest_JniClient 
  12.  * Method:    HelloWorld 
  13.  * Signature: ()Ljava/lang/String; 
  14.  */  
  15. JNIEXPORT jstring JNICALL Java_com_example_ndktest_JniClient_HelloWorld  
  16.   (JNIEnv *, jclass);  
  17.   
  18. #ifdef __cplusplus  
  19. }  
  20. #endif  
  21. #endif  

(7)新建一个Android.mk文件
在jni目录下新建一个Android.mk文件,其内容如下(详细的语法以后再另外解释):

[plain]  view plain copy
  1. LOCAL_PATH := $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_MODULE := NdkTest  
  4. LOCAL_SRC_FILES := com_example_ndktest_JniClient.c  
  5. include $(BUILD_SHARED_LIBRARY)  
[plain]  view plain copy
  1. LOCAL_PATH := $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_MODULE := NdkTest  
  4. LOCAL_SRC_FILES := com_example_ndktest_JniClient.c  
  5. include $(BUILD_SHARED_LIBRARY)  

(8)创建C++源文件
将com_example_ndktest_JniClient.h拷贝到本工程的jni目录下;
然后新建一个com_example_ndktest_JniClient.c文件,用来实现头文件中函数,其内容如下:

[cpp]  view plain copy
  1. #include "com_example_ndktest_JniClient.h"  
  2.   
  3. #include <stdlib.h>   
  4. #include <stdio.h>   
  5. #ifdef __cplusplus   
  6. extern "C"  
  7. {  
  8. #endif   
  9.   
  10. JNIEXPORT jstring JNICALL Java_com_example_ndktest_JniClient_HelloWorld  
  11. (JNIEnv *env, jclass arg)  
  12. {  
  13.     jstring str = (*env)->NewStringUTF(env, "HelloWorld from JNI !");  
  14.     return str;  
  15. }  
  16.   
  17. #ifdef __cplusplus   
  18. }  
  19. #endif  
[cpp]  view plain copy
  1. #include "com_example_ndktest_JniClient.h"  
  2.   
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5. #ifdef __cplusplus  
  6. extern "C"  
  7. {  
  8. #endif  
  9.   
  10. JNIEXPORT jstring JNICALL Java_com_example_ndktest_JniClient_HelloWorld  
  11. (JNIEnv *env, jclass arg)  
  12. {  
  13.     jstring str = (*env)->NewStringUTF(env, "HelloWorld from JNI !");  
  14.     return str;  
  15. }  
  16.   
  17. #ifdef __cplusplus  
  18. }  
  19. #endif  
编辑并保存后,可以看到obj/local/armeabi目录下将自动生成libNdkTest.so库。
(9)用java调用c函数
修改MainActivity.java,完成对JniClient.java中函数的调用:
[java]  view plain copy
  1. package com.example.ndktest;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. //remove for NdkTest   
  6. //import android.view.Menu;   
  7. //add for NdkTest   
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     //add for NdkTest   
  12.     static {  
  13.         System.loadLibrary("NdkTest");  
  14.     }  
  15.       
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         //remove for NdkTest   
  20.         //setContentView(R.layout.activity_main);   
  21.         //add  for NdkTest   
  22.         String str = JniClient.HelloWorld();  
  23.         TextView tv = new TextView(this);  
  24.         tv.setText(str);  
  25.         setContentView(tv);  
  26.     }  
  27.   
  28.     //remove for NdkTest   
  29.     /*@Override 
  30.     public boolean onCreateOptionsMenu(Menu menu) { 
  31.         // Inflate the menu; this adds items to the action bar if it is present. 
  32.         getMenuInflater().inflate(R.menu.main, menu); 
  33.         return true; 
  34.     }*/  
  35. }  
[java]  view plain copy
  1. package com.example.ndktest;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. //remove for NdkTest  
  6. //import android.view.Menu;  
  7. //add for NdkTest  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     //add for NdkTest  
  12.     static {  
  13.         System.loadLibrary("NdkTest");  
  14.     }  
  15.       
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         //remove for NdkTest  
  20.         //setContentView(R.layout.activity_main);  
  21.         //add  for NdkTest  
  22.         String str = JniClient.HelloWorld();  
  23.         TextView tv = new TextView(this);  
  24.         tv.setText(str);  
  25.         setContentView(tv);  
  26.     }  
  27.   
  28.     //remove for NdkTest  
  29.     /*@Override 
  30.     public boolean onCreateOptionsMenu(Menu menu) { 
  31.         // Inflate the menu; this adds items to the action bar if it is present. 
  32.         getMenuInflater().inflate(R.menu.main, menu); 
  33.         return true; 
  34.     }*/  
  35. }  
(10)运行测试
运行,在模拟器中可以看到界面输出来自com_example_ndktest_JniClient.c 文件中的“HelloWorld from JNI ! “。
参考: http://www.cnblogs.com/chenjiajin/archive/2012/04/12/2444188.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows下,Eclipse的Android NDK(r8e) 配置 的相关文章

  • 找不到“ANDROID_HOME”环境变量

    我正在尝试构建一个 ionic android 项目 并且安装了 android sdk 我的项目名称是myApp 我已经成功将android平台添加到myApp中 但是当我尝试构建项目时 myApp sudo ionic build an
  • 从数据库中删除项目后如何从 recyclerView 中删除项目

    我正在使用 recyclerView 来显示当用户单击每个项目的删除按钮时可以删除的项目列表 为了做到这一点 我使用了 recyclerview 适配器 我想要的是 从该 recycleView 适配器中 当用户单击某个项目的删除按钮时 会
  • Android Studio Profiler 中显示“无可调试进程”

    当我尝试在 Android Studio 中使用探查器时 我能够看到已连接的设备 但在探查器中收到 无可调试进程 有人可以帮助我为什么我收到 不可调试的进程 吗 您可以尝试以下任意 全部操作 使缓存失效并重新启动Android Studio
  • 在 docker 容器内创建 android 模拟器时出现“sh: 1: file: not found”

    我正在尝试在 docker 容器内创建一个 android 模拟器 但遇到了一些问题 SDK 更新和 AVD 创建成功 我尝试创建模拟器 出现以下错误 emulator avd test 22 no skin no audio no win
  • onSaveInstanceState 之后无法执行此操作(onClick 首选项)

    这是我的PreferenceActivity 我的主要活动的内部类 public static class TestSettings extends PreferenceActivity implements Preference OnPr
  • 如何突出显示在 Textview android 中单击的单词

    我正在创建一个应用程序 在其中我从 EditText 中的用户获取输入 现在 单击按钮后 输入的字符串将显示在 TextView 中 现在我的问题是 当用户单击任何单个单词时 我会使该单词在 TextView 中突出显示 例如 用户输入 你
  • Eddystone Beacon 中广播的 MAC ID 会改变吗?

    我将描述我的设置 我制作了一个模拟 Eddystone 信标的 Android 应用程序 我能够使用 PlayStore 中的 Beacon Toy 应用程序检测手机上的 Eddystone 信标 但问题是 自上次检查以来 显示的 MAC
  • 无法创建变体“android-manifest-metadata”

    我在 Playstore 上上传了我的应用程序 bundle aab 方法 它在 android 6 0 上运行正常 没有错误 但在 android 7 0 设备上出现错误 并且无法运行该应用程序 我使用了 Gradle 构建类路径 3 1
  • Android Studio 1.2.1.1 中 Gradle 项目刷新失败

    我在全新安装的 Android Studio v 1 2 1 1 上创建了示例项目 但遇到了以下错误消息 Gradle project refresh failed in Android Studio 1 2 1 1 见下图 所以我试图在谷
  • 我什么时候应该使用 CGM 设备组和主题?

    我正在考虑将 Google Cloud Messaging 合并到我的应用程序中 我想向与用户关联的所有设备发送消息 乍一看 它看起来像 GCM 的设备群组消息传递 https developers google com cloud mes
  • 在 Phonegap 3.x CLI 上构建 android 发布 apk

    如何使用 Phonegap 3 x CLI 在本地构建 Android 应用程序并准备发布 我检查了项目的platforms android目录中生成的bin文件夹 并且只有 debug APK 顺便说一句 我使用这个命令 phonegap
  • 使用 Android Exoplayer 调整 Dash 流音量

    我正在尝试设置一个搜索栏来控制 exoplayer 流式破折号实例的级别 我正在使用的设置是演示项目的修改版本 并且无法确定我应该尝试影响搜索栏输出的哪个元素 即如何正确使用 MSG SET VOLUME 等 任何意见将不胜感激 我正在寻找
  • 免费和付费版本 Android 应用程序的最佳方法?

    我开发了一个 Android 应用程序 我希望它可以作为免费版本和付费版本提供 最好的方法是什么 我可以想到三种解决方案 将项目分成两个分支并维护它们 创建一个库项目并有两个附加项目 一个 免费 版本和一个 付费 版本 使用应用内结算 问
  • 如何使用特定选项卡启动活动?

    我已经浏览了许多示例 问题和教程 但我从未见过使用特定选项卡启动活动 启动新意图 我知道可以使用 setCurrentTab切换到选项卡 但这只能从父活动选项卡内部完成 从另一个活动启动一个活动中包含的特定选项卡怎么样 是否可以 如果是这样
  • 如果我清理了反向引用,我是否可以观察 ViewModel?

    建议的实施方式ViewModel是通过使用来公开变化的数据LiveData活动 片段和视图的对象 有一些情况 当LiveData不是一个理想的答案或根本没有答案 自然的选择是将观察者模式应用于ViewModel 使其成为可观察的 注册观察员
  • 更改适配器数据时,RecyclerView 滚动到顶部位置

    我正在对某个帖子进行喜欢和不喜欢RecyclerView 我在更改视图后调用api 但问题是RecyclerView当我通知它更新时 总是滚动到顶部 这是我的适配器代码 likeAnim setOnLikeListener object O
  • 尝试在 android 中使用 Google Maps Android API v2 显示 StreetView 但无法使其正常工作

    我很长时间以来一直在尝试在 Android 中显示特定位置的街景 但不幸的是未能成功 我需要的是 如果我向地图提供位置 LAT LONG 它应该显示该特定位置的街景 以下是显示地图 3D 地图 混合地图 卫星视图等的工作代码 但不显示街景
  • 如何在android上使用xamarin表单执行脚本

    我正在尝试在 Android Mono 上使用 Xamarin Forms 运行 C 脚本 当我运行脚本时 我收到错误 System IO FileNotFoundException 找不到文件 mscorlib dll 这是我试图执行的代
  • Android:禁用 1.5 纸杯蛋糕动画过渡

    长话短说 如何禁用活动之间的屏幕转换 如果您愿意的话 我们实现了自己的选项卡处理程序 现在它正在选项卡之间进行转换 这看起来很俗气 谢谢 Chris See android content Intent FLAG ACTIVITY NO A
  • 具有通用类的自定义 Android 适配器

    我正在尝试在 Android 中创建一个通用适配器 所以我不能一遍又一遍地编写它 问题是 它正在工作 但它的回收效果不是很好 它显示了我想要的内容 但是当我滚动时 它的顺序不同 public class CustomListViewAdap

随机推荐