首页 > 代码库 > jni

jni

Windows下搭建ndk环境

 1.直接解压缩ndk,然后搭建环境变量path目录下面C:\android-ndk-r7b .  这是直接在cmd命令行下运行   ndk-build

 2.安装Cygwin Terminal  这个配置环境变量在  cygwin/etc/profile32

/cygdrive/c/android-ndk-r7b

 每个环境变量用:分隔.

 有时候cynwin下 运行一直找不到目录 .执行./ndk-build -C samples/hello-jni 这个代码 博客:http://blog.sina.com.cn/s/blog_4c73bcc80101177e.html

 Jni.h 目录:C:\android-ndk-r7b\platforms\android-14\arch-arm\usr\include

log.h目录:C:\android-ndk-r7b\platforms\android-14\arch-arm\usr\include\android

第一个:helloworld

创建jni目标.

创建Hello.c

 

#include<stdio.h>#include<jni.h>jstring Java_com_example_myhello_MainActivity_helloworldFromc(JNIEnv* env,jobject obj){    return (*env)->NewStringUTF(env,"onehello");}

 

创建Android.mk

LOCAL_PATH := $(call my-dir)   include $(CLEAR_VARS)   LOCAL_MODULE    := hello   LOCAL_SRC_FILES := Hello.c   include $(BUILD_SHARED_LIBRARY)

然后activity里面加载library

package com.example.myhello;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {    public native String helloworldFromc();        static{        System.loadLibrary("hello");    }        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }        public void click(View view) {        // TODO Auto-generated method stub        Toast.makeText(getApplicationContext(), helloworldFromc(), 0).show();    }}

 

 

 

 

jni