戻る>2006

SDK でプログラミング」を拡張する.

ライブラリをリビルドしたら,eclipse側で「Refresh(F5)」を実行する必要がありそう.

nativeライブラリの作成


New -> Folder で,HelloActivity 直下に,jni フォルダを作成する.

android-ndk-r4b\samples\hello-jni\jni\ の,Android.mk をjni以下にコピー.
android-ndk-r4b\samples\hello-jni\jni\ の,hello-jni.c をjni以下に,sum-jni.cとしてコピー.

Android.mk を以下のように修正.
LOCAL_LDLIBS の行は,__android_log_print()を使うのに必要.

# 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_LDLIBS    := -llog
LOCAL_MODULE    := sum-jni
LOCAL_SRC_FILES := sum-jni.c

include $(BUILD_SHARED_LIBRARY)


sum-jni.c を以下のように修正.
関数名(この場合は,Java_com_example_android_helloactivity_HelloActivity_sumFromJNI)は非常に重要なので要注意.
__android_log_print()を使うには,#include <android/log.h> も必要.

/*
 * 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>
#include <android/log.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
 */
jlong
Java_com_example_android_helloactivity_HelloActivity_sumFromJNI (JNIEnv* env, jobject thiz, jint endNum)
{

	jlong sum = 0;

	int i;
	for(i = 1;i<=endNum;i++){
		sum += i;
	}

	__android_log_print(ANDROID_LOG_INFO,"SUM","sum= %ld \n",sum);

	return (sum);
}

cygwin を起動してから,jni以下に移動して ~/android-ndk-r4b/ndk-build を実行.

$ ~/android-ndk-r4b/ndk-build
Compile thumb  : sum-jni <= /cygdrive/e/Development/android-sdk-windows/platforms/android-4/samples/HelloActivity/jni/sum-jni.c
SharedLibrary  : libsum-jni.so
Install        : libsum-jni.so => /cygdrive/e/Development/android-sdk-windows/platforms/android-4/samples/HelloActivity/libs/armeabi

native実行を追加


execute ボタンが押された時の処理に,native実行を追加.

		if(arg0==execButton){
			/* execute ボタンが押されたときの処理 */
			{	// native 実行
				String strSum="";
				// 開始時間取得
				long sUpTimeMillis = SystemClock.uptimeMillis(); 
				
				long sum=0;
				sum = sumFromJNI(Integer.valueOf(strEnd));
				strSum = String.valueOf(sum); // 文字列へ変換

				// 終了時間取得
				long eUpTimeMillis = SystemClock.uptimeMillis();
				
				/* 計算結果表示 */
				View txtResult = this.findViewById(R.id.TextView04);
				((TextView)txtResult).setText(strSum);
	
				/* 計算時間表示 */
				View elpTime = this.findViewById(R.id.TextView05);
				((TextView)elpTime).setText("Native : " + String.valueOf(eUpTimeMillis - sUpTimeMillis) + " ms");
			}
			{	// Java 実行
				String strSum="";

JNI呼出のおまじないを追加.

    public native long  sumFromJNI(int nNumber);
    public native long  unimplementedSumFromJNI(int nNumber);
    static {
        System.loadLibrary("sum-jni");
    }

実行結果

Run as -> Android Application



2スレッド化


Android.mkを修正.
LOCAL_SRC_FILES := sum-jni.c sum-func.c

sum-jni.c を修正.
__android_log_print の出力結果が不正なのは何故だろうか?

#include <pthread.h>

void func1( jint );
void func2( jint );
extern jlong sum_func1, sum_func2;

jlong
Java_com_example_android_helloactivity_HelloActivity_sumFromJNI ( JNIEnv* env, jobject thiz, jint endNum)

{
	pthread_t t1 ;
	pthread_t t2 ;
	pthread_create( &t1, NULL, (void *)func1, (void *)endNum );
	pthread_create( &t2, NULL, (void *)func2, (void *)endNum );
	pthread_join( t1, NULL );
	pthread_join( t2, NULL );

	__android_log_print(ANDROID_LOG_INFO,"SUM","sum_func1 = %d",sum_func1);
	__android_log_print(ANDROID_LOG_INFO,"SUM","sum_func2 = %d",sum_func2);

	return (sum_func1 + sum_func2);
}

sum-func.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>
#include <android/log.h>
#include <pthread.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
 */

jlong sum_func1=0, sum_func2=0;

jlong common_func(jint x){
	int i;
	jlong sum=0;
	for(i = 1;i<=x;i++){
		sum += i;
	}
	return sum;

}
void func1( jint x ) {
	sum_func1 = common_func(x);
	__android_log_print(ANDROID_LOG_INFO,"SUM","func1 %d %d\n",x, sum_func1);
}

void func2( jint x ) {
	sum_func2 = common_func(x);
	__android_log_print(ANDROID_LOG_INFO,"SUM","func2 %d %d\n",x, sum_func2);
}

実行結果


Run as -> Android Application



ここまで

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

×

この広告は60日間更新がないwikiに表示されております。

管理人/副管理人のみ編集できます