欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android4.0 jni Hello World 开发~图解  

程序员文章站 2022-05-06 18:53:57
...

之前之前用R4,现在一下就跳到用R7了,ANDROID4.0出来过后,应该有不少热机友敢望资疗吧,OK,在网上偶尔浏览的时候,看到有很多初学者希望了解在ANDROID中NDK应用的开发,不知道它是怎么开发与运行的,今天我就简单来图解一个HelloWorld的简单实列吧,以好供初学者做给力的参考,OK,不废话了,直入正题吧:

首先,我们得配置环境,当然这是在你本来就有SDK开发环境的情况下,请去官方下个NDK吧:http://www.android.com.android4.0 jni Hello World 开发~图解
            
    
    
         ,最新版本为android-ndk-r7-linux-x86.tar.bz2,即R7,我一直用的是LINUX,所以我下的是LINUX版本,如果你是WINDOWS或者MAC的话,你自己看着办吧,OK,下下来后,解压在你自己认为可以放的地方就行了,看目录:android4.0 jni Hello World 开发~图解
            
    
    
         ,以上为解压后的R7目录,先别急新建项目开发,先配置NDK环境:打开终端,输入命令:sudo vim ~/.baserc,打开后根据下面图,填入下下来的NDK目录路径:

android4.0 jni Hello World 开发~图解
            
    
    
         在图中可以看到有这行:export NDK_HOME=/home/development/android/android-ndk-r7/

即为NDK所需配置的环境路径。保存后,输入: source ~/.baserc 来使其配置立即生效,OK后,咋们来新创建一个项目,为jni_demo,下面我需要看当前那个目录截图,你会发现目录中有一个sample目录,里面就是其本身已有的列子,在这里我得提醒各位,不敢你遇到任何新的语言,先看它的Hello World列子,不要急于误打误撞的敲代码,先看清楚它的列子的运行效果,OK,在这里我们就以hello-jni这个列子来验证吧,打开这个目录:

android4.0 jni Hello World 开发~图解
            
    
    
         可看到有四个目录,一个是JNI为C~比源码处,一个是APP的RES,一个SRC为JAVA源代码,再一个就是测试目录,另外两个文件就不在这里说了,我们新建了项目后,需要写JNI代码与JAVA代码,所以在这里,我就直接把这里的jni里面的代码拷贝到我的项目中去,记得在自己的项目中需要新建一个jni文件,其拷贝的两个文件分别是:Android.mk与hello-jni.c,其Android.mk文件内容是:

# 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.
#
LOCAL_PATH := $(call my-dir)


include $(CLEAR_VARS)


LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c


include $(BUILD_SHARED_LIBRARY)


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_jsd_jni_demo_JsdJniActivity_getJniDatas
( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "This is for Datas from JNI !");
}

注意观察红色字体,其为项目目录路径地址,根据自己实际项目目录来定,

好了,在新建ACTIVITY类中:

package com.jsd.jni.demo;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


/**
*
* @author jankey
*
*/
public class JsdJniActivity extends Activity {

private Button mJniDemo = null;
private TextView mGetJni = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findById();
}

private void findById(){
mJniDemo = (Button) findViewById(R.id.jniDemo);
mGetJni = (TextView) findViewById(R.id.getJni);
mJniDemo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mGetJni.setText(getJniDatas());
}
});
}

/**
* A native method that is implemented by the
* 'hello-jni' native library, which is packaged
* with this application.
* @return
*/
public native String getJniDatas();

/**
*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 !
* @return
*/
public native String unimplementedGetJniDatas();

/**
* this is used to load the 'hello-jni' library on application
* startup. The library has already been unpacked into
* /data/data/com.example.HelloJni/lib/libhello-jni.so at
* installation time by the package manager.
*/
static{
System.loadLibrary("hello-jni");
}
}

上面有注释,其native方法为JNI需要调用与执行的方法,

在布局里就添加了一个按钮与一个文本,使用按钮单点后来获得文本显示的字符为JNI所得来的数据,其实这个过程很简单,基本就是NDK里面本身的列子般过来运行一样,等这都完了,先不要急于运行,你需要先把C文件进行编译后才能执行运行APP,在命令行进入其目录的JNI目录,使用ndk-build命令来构建编译:

android4.0 jni Hello World 开发~图解
            
    
    
         你会看到:jankey@jankey-ThinkPad-Edge:~/workspace/jni_demo/jni$ ndk-build
Compile thumb : hello-jni <= hello-jni.c
SharedLibrary : libhello-jni.so
Install : libhello-jni.so => libs/armeabi/libhello-jni.so,生成了两个文件目录:

android4.0 jni Hello World 开发~图解
            
    
    
         即两个.so为后缀的文件,在这里基本就没什么问题了,这样就建立了C与JAVA通信的一个过程,OK,使用CTRI+F11开始运行其APP:

android4.0 jni Hello World 开发~图解
            
    
    
         ,先我们可以单击来获取JNI返回回来的数据了:

android4.0 jni Hello World 开发~图解
            
    
    
         

这就把数据给调出来了,如果你是初学者,你还等什么呢,赶快动手吧,希望这能给在入门里做最给里的参考,谢谢,有时间,我会继续慢慢的写一些深入的内容,可敬请期待