设为首页 加入收藏

TOP

Android 4.0 JNI Hello World 开发图解(一)
2014-11-24 13:24:42 来源: 作者: 【 】 浏览:1
Tags:Android 4.0 JNI Hello World 开发 图解

首先,我们得配置环境,当然这是在你本来就有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
#include


/* 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.setO

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇在Eclipse中配置NDK自动编译环境b.. 下一篇Linux下C语言之HelloWorld

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: