歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 4.0 JNI Hello World 開發圖解

Android 4.0 JNI Hello World 開發圖解

日期:2017/3/1 10:57:12   编辑:Linux編程

之前之前用R4,現在一下就跳到用R7了,Android4.0出來過後,應該有不少熱機友敢望資療吧,OK,在網上偶爾浏覽的時候,看到有很多初學者希望了解在ANDROID中NDK應用的開發,不知道它是怎麼開發與運行的,今天我就簡單來圖解一個HelloWorld的簡單實列吧,以好供初學者做給力的參考,OK,不廢話了,直入正題吧:

首先,我們得配置環境,當然這是在你本來就有SDK開發環境的情況下,請去官方下個NDK吧:http://www.android.com.,最新版本為android-ndk-r7-linux-x86.tar.bz2,即R7,我一直用的是LINUX,所以我下的是LINUX版本,如果你是WINDOWS或者MAC的話,你自己看著辦吧,OK,下下來後,解壓在你自己認為可以放的地方就行了,看目錄:,以上為解壓後的R7目錄,先別急新建項目開發,先配置NDK環境:打開終端,輸入命令:sudo vim ~/.baserc,打開後根據下面圖,填入下下來的NDK目錄路徑:

在圖中可以看到有這行:export NDK_HOME=/home/development/android/android-ndk-r7/

即為NDK所需配置的環境路徑。保存後,輸入: source ~/.baserc 來使其配置立即生效,OK後,咋們來新創建一個項目,為jni_demo,下面我需要看當前那個目錄截圖,你會發現目錄中有一個sample目錄,裡面就是其本身已有的列子,在這裡我得提醒各位,不敢你遇到任何新的語言,先看它的Hello World列子,不要急於誤打誤撞的敲代碼,先看清楚它的列子的運行效果,OK,在這裡我們就以hello-jni這個列子來驗證吧,打開這個目錄:

可看到有四個目錄,一個是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命令來構建編譯:

你會看到: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,生成了兩個文件目錄:

即兩個.so為後綴的文件,在這裡基本就沒什麼問題了,這樣就建立了C與JAVA通信的一個過程,OK,使用CTRI+F11開始運行其APP:

,先我們可以單擊來獲取JNI返回回來的數據了:


這就把數據給調出來了,如果你是初學者,你還等什麼呢,趕快動手吧,希望這能給在入門裡做最給裡的參考,謝謝。

Copyright © Linux教程網 All Rights Reserved