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