android之JNI开发步骤总结

阅读: 评论:0

android之JNI开发步骤总结

android之JNI开发步骤总结

转自:.html

jni介绍:=0Amg7hW3jfnH2InHKy0S48QQkl2A2t_XKzjPLdhmzDM8cdOffT7PYbNa109WZV8txzVA3Tz5dgMrSWQGvCIrPa

这几天一直在研究JNI的开发过程,顺便把NDK环境搭建一起总结下。在windows环境下开发jni需要c/c++编译器的支持,网络上我看很多人使用cygwin。呵呵我不是很喜欢使用它,感觉安装起来挺麻烦的。我使用GNUStep,下载地址 .html 。


下载安装后,验证是否成功。打开GNUstep->Shell,输入make -v 和 gcc -v命令,如图所示。




下载NDK,地址.html。下载完后解压即可。
配置ndk环境变量,gnustep是模拟linux的环境的,打开gnustep的安装目录下的G:softinstallGNUstepf文件,添加以下内容:
复制内容到剪贴板
代码:
NDK=/g/softinstall/Android/android-ndk-r8b
export=NDK

说明如果不知道ndk目录在linux下应该是在哪里,你可以打开gnustep的命令窗口,输入mount,就可以找到对应的盘符。
验证环境变量,如下图。 


以上就配置成功了。

下载进入正题啦。Jni的开发步骤。
打开eclipse,新建工程名为testJni。在activity中添加以下代码 复制内容到剪贴板
代码:
w.jni;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
/**

* @author XuZhiwei (xuzhiwei@gmail)
* sina:

* Create at 2012-8-30 上午10:49:45
*/
public class TestJni extends Activity { 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Create(savedInstanceState); 
    }
   //natvie必须声明,用于生成C/C++代码
    public native String hello();
   
    static{
            System.loadLibrary("testJni");
    }
    
}
编译后的文件在bin目录下,通过javah命令生成c/c++的文件头。如下图  


会在项目目录下生成jni/com_xzw_jni_TestJni.h。
头文件代码如下: 复制内容到剪贴板
代码:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_xzw_jni_TestJni */

#ifndef _Included_com_xzw_jni_TestJni
#define _Included_com_xzw_jni_TestJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     com_xzw_jni_TestJni
* Method:    hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_xzw_jni_TestJni_hello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
这里我们可以根据头文件编写c代码 复制内容到剪贴板
代码:
#include <string.h>
#include <jni.h>


jstring
Java_com_xzw_jni_TestJni_hello
  (JNIEnv* env, jobject thiz){
          return (*env)->NewStringUTF(env, "哈哈完成自动化编译 !");
}
接下来编写 Android.mk,该文件可以直接从NDK的samples下的hello-jni的jni文件下直接靠过来改改就可以了。也贴下代码哈。 复制内容到剪贴板
代码:
# 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
#
#      .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.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := testJni
LOCAL_SRC_FILES := testJni.c

include $(BUILD_SHARED_LIBRARY)
其中你只需要该LOCAL_MODULE和LOCAL_SRC_FILES就可以了。
说明:LOCAL_MODULE是描述模块的,用来给java调用的模块名,会生成对应的libtestJni.so
LOCAL_SRC_FILES就是源文件啦,多个文件空格隔开即可。
接下来,我们要开始编译生成so文件咯。
打开gnustep的命令窗口,进入到项目底下,输入$NDK/ndk-build命令,即可自动生成libs/armeabi/libtestJni.so文件。



接下来就是java调用了。直接上代码 复制内容到剪贴板
代码:
w.jni;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
/**

* @author XuZhiwei (xuzhiwei@gmail)
* sina:

* Create at 2012-8-30 上午10:49:45
*/
public class TestJni extends Activity { 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Create(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText(hello()); //这里的hello() 就是调用c
        setContentView(tv);
    }

    public native String hello();
   
    static{
            System.loadLibrary("testJni");
    }
    
}
运行结果如下


以上就是jni的开发步骤了。
在这里我还要补充下:
在我们开发过程中,改一个c/c++的文件,我们都要手动去编译一下有点儿麻烦。这里我们可以使用让eclipse帮助我们自己编译。

右击jni工程的properties-->Builders-->NEW -->;Program 可以看到以下内容:
 


argument:--login -c "cd /e/myWorkSpace/android/hellJni && $NDK/ndk-build"
切换到Refresh 标签页
 

切换到Build Options标签页
 


这样就完成了配置,点击确定可看到控制台自动编译程序了


 

demo地址:
hellJni.rar

本文发布于:2024-01-31 05:21:39,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170664969825856.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:E
标签:步骤   android   JNI
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23