jniチュートリアル

ここのJNIちゅーとりあるまとめ(講師bols_blue)
http://tweetvite.com/event/drocafe0xf

目次

  • はじめに
  • JNIの得意なところ
  • JNIのしくみ

JNIの得意なところ

  • フレームレートが一定数必要
    • フレームレートを固定できる
  • Posixなライブラリが使いたい
  • 他のプラットフォーム対応をもくろんでいる

JNIのしくみ

ダイナミックorスタティックリンク

  • スタティックリンク
  • ダイナミックリンク
    • 複数のファイル
    • ファイルサイズが小さくなる
    • OS側でリンクを変更することができる
    • dlopenで開く

jbyte、jint javaでcの変数の型を使うときにごにょごにょしてくれるあれ

インストール

http://developer.android.com/sdk/ndk/index.html

※ここにツールチェインがある ~/xxx/android-ndk-r5b/toolchains

ツールチェイン

arm-eabi-addr2line	arm-eabi-gcc-4.4.0	arm-eabi-objdump
arm-eabi-ar		arm-eabi-gcov		arm-eabi-ranlib
arm-eabi-as		arm-eabi-gdb		arm-eabi-readelf
arm-eabi-c++		arm-eabi-gdbtui		arm-eabi-run
arm-eabi-c++filt	arm-eabi-gprof		arm-eabi-size
arm-eabi-cpp		arm-eabi-ld		arm-eabi-strings
arm-eabi-g++		arm-eabi-nm		arm-eabi-strip
arm-eabi-gcc		arm-eabi-objcopy

ebi:Embedded Application Binary Interfaceの略

arm-eabi-addr2line アーカイブ
arm-eabi-as アセンブラ
arm-eabi-cpp プリプロセッサ
arm-eabi-gcov カバレッジ
arm-eabi-gdbtui UI使ったデバッグ
arm-eabi-gprof プロファイラ (どの関数にどのくら時間かかったなど)
arm-eabi-ld リンカ
arm-eabi-objcopy elf -> ihex オブジェクトのフォーマット変換
arm-eabi-objdump arm-eabi-nmより詳しい情報が見れる
arm-eabi-nm ネームリスト
arm-eabi-readelf elf限定のarm-eabi-objdump的な
arm-eabi-run エミュレータがどうのとかその辺
arm-eabi-size セクションのサイズを表示
arm-eabi-strings オブジェクト内のストリングを取り出す
arm-eabi-strip バイナリを小さくする

BinaryHacks(オライリー)に詳しく書いてある

サンプル

~/xxx/android-ndk-r5b/samples

  • bitmap-plasma
  • hello-neon
  • native-audio
  • test-libstdc++
  • hello-gl2
  • module-exports
  • native-plasma
  • two-libs
  • hello-jni
  • native-activity
  • san-angeles

サンプルを動かす

ダイナミックリンクライブラリの作成をする

ndk-build -C hello-jni/

コマンド "ndk-build -C hello-jni/" を実行すると、soファイル
soファイル:ダイナミックリンクライブラリのファイルの実態

cd samples
ndk-build -C hello-jni/

Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
Compile thumb  : hello-jni <= hello-jni.c
SharedLibrary  : libhello-jni.so
Install        : libhello-jni.so => libs/armeabi/libhello-jni.so

Eclipseにインポートする
新規作成>create project from existing source>Browse... > hello-jniを選択

こんなのができる

ソース

HelloJni.java

/*
 * 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.
 */
package com.example.hellojni;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;


public class HelloJni extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }

    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    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();

    /* this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.HelloJni/lib/libhello-jni.so at
     * installation time by the package manager.
     */
    static {
        System.loadLibrary("hello-jni");
    }
}

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 <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_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI !");
}