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的模块名相同

猜你喜欢

转载自blog.csdn.net/css33/article/details/80754087
今日推荐