之前之前用R4,现在一下就跳到用R7了,Android4.0出来过后,应该有不少热机友敢望资疗吧,OK,在网上偶尔浏览的时候,看到有很多初学者希望了解在ANDROID中NDK应用的开发,不知道它是怎么开发与运行的,今天我就简单来图解一个HelloWorld的简单实列吧,以好供初学者做给力的参考,OK,不废话了,直入正题吧:
首先,我们得配置环境,当然这是在你本来就有SDK开发环境的情况下,请去官方下个NDK吧:
即为NDK所需配置的环境路径。保存后,输入: source ~/.baserc 来使其配置立即生效,OK后,咋们来新创建一个项目,为jni_demo,下面我需要看当前那个目录截图,你会发现目录中有一个sample目录,里面就是其本身已有的列子,在这里我得提醒各位,不敢你遇到任何新的语言,先看它的Hello World列子,不要急于误打误撞的敲代码,先看清楚它的列子的运行效果,OK,在这里我们就以hello-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
#
#
#
# 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
*
*
*
* 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类中: