1. 程式人生 > >Android C++高階程式設計--使用NDK 讀書筆記(二)NDK的例子hellojni

Android C++高階程式設計--使用NDK 讀書筆記(二)NDK的例子hellojni

還是eclipse的ndk的hellojni例子的結構:

jni:該目錄包含原生元件的原始碼及描述原生元件構建方法的Android.mk構建檔案

NDK最重要的元件是Android.mk構建檔案,該文件描述了原生元件。

hello-jni.c檔案內容:

#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 !"); }

=================================================

NewStringUTF:構建UTF-8編碼格式的字串例項

==================================================

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)

============================================================================================

Android.mk內容講解:

LOCAL_PATH:用於定位原始檔。android構建系統提供了一個名為my-dir的巨集功能,通過將LOCAL_PATH變數設定為my-dir巨集功能的返回值,可以將原始檔放在當前目錄下

CLEAR_VARSclear-vars.mk片段位置,包含了清除除了LOCAL_PATH以外的LOCAL_<name>變數

LOCAL_MODULE:用來給這些模組設定唯一的名稱

LOCAL_SRC_FILES:用來建立和組裝這個原始檔列表。可以包含多個原始檔,用空格分開

BUILD_SHARED_LIBRARY:build-shared-library.mk檔案儲存位置,包含了將原始檔構建和組裝成共享庫的必要過程

============================================================================================

HelloJni.java的程式碼:

package com.example.hellojni;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;


public class HelloJni extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }

    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    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 !
     */
    public native String  unimplementedStringFromJNI();

    /* 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");
    }
}

loadLibrary的引數名要與Android.mk中LOCAL_MODULE的模組名相同