NDK配置
1.下载最新版本NDK(android-ndk-r9d-linux-x86_64.tar.bz2)
下载网页:http://developer.android.com/tools/sdk/ndk/index.html
2.拷贝下载文件到环境配置目录解压,解压方式
tar -jxvf android-ndk-r9d-linux-x86_64.tar.bz2
3.控制台进入到账户根目录,编辑.bashrc文件(vi .bashrc)
4.新建环境变量(主要最后一个文件夹为ndk解压后的文件夹名)
export NDK_HOME=/home/lchd/work/tools/android-ndk-r9-linux
5.在PATH中添加改环境
export PATH=$PATH:$NDK_HOME:.
6.退出控制台,重新进入控制台
输入 ndk-build 查看错误提示
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
出现以上错误,恭喜你,环境配置OK
JNI demo
src源代码结构图
./src
├── ./src/com
│ └── ./src/com/example
│ └── ./src/com/example/testedittext
│ └── ./src/com/example/testedittext/MainActivity.java
└── ./src/jni
└── ./src/jni/helloworld.java
/src/com/example/testedittext/MainActivity.java
package com.example.testedittext; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.Toast; import jni.helloworld; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.loadLibrary("helloworld"); Toast.makeText(this, "xx " + new helloworld().dispHelloWorld(), Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
/src/jni/helloworld.java
package jni; public class helloworld { public native String dispHelloWorld(); }
JNI文件目录
├── ./jni
│ ├── ./jni/Android.mk
│ └── ./jni/helloworld.c
/jni/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 := helloworld LOCAL_SRC_FILES := helloworld.c include $(BUILD_SHARED_LIBRARY)
/jni/helloworld.c
#include <jni.h> #include <stdio.h> JNIEXPORT jstring JNICALL Java_jni_helloworld_dispHelloWorld(JNIEnv *env, jobject obj) { char* cstr = "helloworld111"; return (**env).NewStringUTF(env, cstr); }
在项目根目录运行ndk-build 生成以下文件
├── ./libs
│ ├── ./libs/android-support-v4.jar
│ └── ./libs/armeabi
│ └── ./libs/armeabi/libhelloworld.so
├── ./obj
│ └── ./obj/local
│ └── ./obj/local/armeabi
│ ├── ./obj/local/armeabi/libhelloworld.so
│ └── ./obj/local/armeabi/objs
│ └── ./obj/local/armeabi/objs/helloworld
│ ├── ./obj/local/armeabi/objs/helloworld/helloworld.o
│ └── ./obj/local/armeabi/objs/helloworld/helloworld.o.d
运行android程序,正确谈出toast xx helloworld111.