windows下使用NDK

一、首先下载android-ndk,官方网站是: http://developer.android.com/tools/sdk/ndk/index.html
目前最新的版本是  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 <jni.h> /* 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 */ jstring Java_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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值