要使用jni首先要配置NDK以及CDT,配置好之后,下面来简要说明一下使用jni的一些步骤:
- public class UsageJniActivity {
- public native float resultofmultiply(int n1, int n2);
- public native float calculateGapOfTwoPoint(int nx1, int ny1, int nx2,
- int ny2);
- public native void setString(String strText);
- }
- /* DO NOT EDIT THIS FILE - it is machine generated */
- #include <jni.h>
- /* Header for class UsageJniActivity */
- #ifndef _Included_UsageJniActivity
- #define _Included_UsageJniActivity
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class: UsageJniActivity
- * Method: resultofmultiply
- * Signature: (II)F
- */
- JNIEXPORT jfloat JNICALL Java_UsageJniActivity_resultofmultiply
- (JNIEnv *, jobject, jint, jint);
- /*
- * Class: UsageJniActivity
- * Method: calculateGapOfTwoPoint
- * Signature: (IIII)F
- */
- JNIEXPORT jfloat JNICALL Java_UsageJniActivity_calculateGapOfTwoPoint
- (JNIEnv *, jobject, jint, jint, jint, jint);
- /*
- * Class: UsageJniActivity
- * Method: setString
- * Signature: (Ljava/lang/String;)V
- */
- JNIEXPORT jstring JNICALL Java_UsageJniActivity_getString
- (JNIEnv *, jobject);
- #ifdef __cplusplus
- }
- #endif
- #endif
- #include "Calculate.h"
- JNIEXPORT jfloat JNICALL Java_UsageJniActivity_resultofmultiply(
- JNIEnv * env, jobject thiz, jint a, jint b) {
- return a * b;
- }
- JNIEXPORT jfloat JNICALL JNICALL Java_UsageJniActivity_calculateGapOfTwoPoint(
- JNIEnv * env, jobject thiz, jint nx1, jint ny1, jint nx2, jint ny2) {
- float fvalue = (nx1 - nx2) * (nx1 - nx2) + (ny1 - ny2) * (ny1 - ny2);
- return fvalue;
- }
- JNIEXPORT jstring JNICALL Java_UsageJniActivity_getString(
- JNIEnv * env, jobject thiz) {
- char str[] = { "hello everyone" };
- return env->NewStringUTF(str);
- }
- # 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 := Calculate
- LOCAL_SRC_FILES := Calculate.cpp
- LOCAL_LDLIBS := -llog
- include $(BUILD_SHARED_LIBRARY)
- APP_ABI := armeabi x86
- APP_STL := stlport_static
- ${NDK_HOME}/platforms/android-14/arch-arm/usr/include:${NDK_HOME}/sources/cxx-stl/stlport/stlport