一、首先下载android-ndk
,官方网站是: http://developer.android.com/tools/sdk/ndk/index.html 目前最新的版本是 android-ndk-r8e-windows-x86.zip,下载地址: http://dl.google.com/android/ndk/android-ndk-r8e-windows-x86.zip
下载后把压缩包解压出来,例如:D:\ndk,目录下的ndk-build.cmd就是用来编译的批处理命令。
这里以D:\ndk\samples\hello-jni为例,打开D:\ndk\samples\hello-jni\jni\hello-jni.c查看代码:
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */#include <string.h>#include /* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java */jstringJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,                                                  jobject thiz ){    return (*env)->NewStringUTF(env, "Hello from JNI !");}

因为我们是拿这个c源码文件来使用,如果迁就 Java_com_example_hellojni_HelloJni_stringFromJNI 函数名的话,在我们的android工程中java类的声明就要是: com/example/hellojni/HelloJni.java
反之,如果我们的android工程已经创建好,并且包名是com.example.hellojni,stringFromJNI函数我们写在了MainActivity.java类里, 那么这里的c函数就要修改为:Java_com_example_hellojni_MainActivity_stringFromJNI,否则在寻找函数时会找不到。



二、编译,打开cmd命令行窗口,cd进入目录: D:\ndk\samples\hello-jni, 然后执行命令:D:\ndk\ndk-build.cmd(如果设置过环境变量则直接使用ndk-build.cmd)来编译hello-jni,如果没有错误会输出: Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver Gdbsetup       : libs/armeabi/gdb.setup
"Compile thumb : hello-jni <= hello-jni.c
SharedLibrary  : libhello-jni.so
Install        : libhello-jni.so => libs/armeabi/libhello-jni.so


编译成功会在D:\ndk\samples\hello-jni\libs\armeabi目录下生成libhello-jni.so文件。


三、创建android应用程序并使用so文件 打开eclipse创建一个android应用程序HelloJni,默认的com.example.hellojni包下面有一个MainActivity.java, 在此包下添加一个HelloJni.java,
package com.example.hellojni;public class HelloJni {    public native String  stringFromJNI();        /* This is another native method declaration that is *not*          * implemented by 'hello-jni'. This is simply to show that          * you can declare as many native methods in your Java code          * as you want, their implementation is searched in the          * currently loaded native libraries only the first time          * you call them.          *          * Trying to call this function will result in a          * java.lang.UnsatisfiedLinkError exception !     */  }

MainActivity.java修改为:
package com.example.hellojni;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;import android.util.Log;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //setContentView(R.layout.activity_main);                HelloJni jni = new HelloJni();        TextView  tv = new TextView(this);         String str = jni.stringFromJNI();        Log.d("HelloJni", str);        tv.setText(str);          setContentView(tv);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    static {          System.loadLibrary("hello-jni");               }}

把编译生成的libhello-jni.so文件复制到F:\eclipse_workspace\HelloJni\libs\armeabi目录下(armeabi如果不存在则手动创建之),然后编译运行,效果图如下:




参考资料: Android Eclipse JNI 调用 .so文件加载问题: http://blog.sina.com.cn/s/blog_4d0afbdb0100m7y7.html Android NDK 生成、调用so文件: http://blog.csdn.net/vestigge/article/details/8597993


更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. C语言函数的递归(上)
  6. Android(安卓)DataBinding 快速入门
  7. android Eclipse执行项目提示错误: unable to execute dex: GC or
  8. 搭建Android编译环境
  9. Android(安卓)全屏或者取消标题栏

随机推荐

  1. Android(安卓)Camera预览过程数据流浅析
  2. WebService开发实例(Axis2实现,无需安装,快
  3. Android(安卓)Studio启动前sdk下载不了
  4. 《android Graphics(一):概述及基本几何图形
  5. Android实现启用GPRS/3G网络
  6. Hello Android(安卓)- Bitmap转换为黑白
  7. Android(安卓)Audio代码分析20 - queryEf
  8. android:nextFocus 属性介绍
  9. Android OpenGL ES 画球体
  10. Android错误解决方法之:Debug certificate