首页 > 代码库 > jni ndk 入门

jni ndk 入门

1. Linux环境模拟,下载sygwin 安装,选择devl 和shell -> install
sygwin 中的配置ndk环境,进入安装目录c:/cygwin64
etc/profile文件配置ndk的环境
//37行
  PATH="/usr/local/bin:/usr/bin:/cygdrive/d/android-ndk-r9d-windows-x86_64/android-ndk-r9d${PATH:+:${PATH}}"

2. 下载ndk

3. 开发,参考于ndk/sample/HelloJni
创建android 工程在JNTTest,在jni中创建3个文件

3.1 helo-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 <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_jun_jnitest_MainActivity_stringFromJNI( JNIEnv* env, jobject thiz )
{

 return (*env)->NewStringUTF(env, "Hello from JNI !  Compiled with ABI ");

}

3.2 Application.mk 内容如下:
APP_ABI := all

3.3 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)

3.4 java文件编辑
package com.jun.jnitest;
public class MainActivity extends Activity {
   
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();
 
//  静态代码块,先于构造函数执行,在此会对C/C++代码的库即:*.so文件进行加载
  static {
        //hello-jni 为在android.mk 中定义的名称
      System.loadLibrary("hello-jni");
    }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// System.out.println(stringFromJNI());
}

public void click(View view) {
                    //使用
Toast.makeText(this, stringFromJNI(), 0).show();
}
 

}


3.5 利用cygwin编译
cd 到当前android 工程目录下 /cygdrive/d/android/workspace/jnitest
Luv@Luv-PC /cygdrive/d/android/workspace/jnitest
$ ndk-build
Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml
[armeabi-v7a] Compile thumb  : hello-jni <= hello-jni.c
[armeabi-v7a] SharedLibrary  : libhello-jni.so
[armeabi-v7a] Install        : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so
[armeabi] Compile thumb  : hello-jni <= hello-jni.c
[armeabi] SharedLibrary  : libhello-jni.so
[armeabi] Install        : libhello-jni.so => libs/armeabi/libhello-jni.so
[x86] Compile        : hello-jni <= hello-jni.c
[x86] SharedLibrary  : libhello-jni.so
[x86] Install        : libhello-jni.so => libs/x86/libhello-jni.so
[mips] Compile        : hello-jni <= hello-jni.c
[mips] SharedLibrary  : libhello-jni.so
[mips] Install        : libhello-jni.so => libs/mips/libhello-jni.so

3.6 运行


来自为知笔记(Wiz)