Android JNI开发一,HelloJni
来源:互联网 发布:ppp和hdlc对数据的 编辑:程序博客网 时间:2024/06/16 21:11
容易忘记,记个笔记,个人理解,可能有误,求高手指点
本例使用NDK开发的HelloJNI实例
代码结构如下如所示:
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.os.Bundle;import android.widget.TextView;import android.widget.Toast;public class HelloJni extends Activity{private TextViewtext_yuan;private TextViewtext_1;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);text_yuan = (TextView) findViewById(R.id.text_yuan);text_yuan.setText(stringFromJNI());text_1 = (TextView) findViewById(R.id.text_1);text_1.setText(helloTest());System.out.println(helloTest());Toast.makeText(this, helloTest(), Toast.LENGTH_LONG).show();}/* * 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();public native String helloTest();/* * 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{/**** * 加载JNI库 * loadLibrary方法中的String参数 与LOCAL_MODULE后面参数相同hello-jni */System.loadLibrary("hello-jni");}}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-jniLOCAL_SRC_FILES := hello-jni.c \ my_hello.cinclude $(BUILD_SHARED_LIBRARY)my_hello.c的代码如下:
#include <string.h>#include <jni.h>jstringJava_com_example_hellojni_HelloJni_helloTest( JNIEnv* env, jobject thiz ){return (*env)->NewStringUTF(env, "wo cao ni ma de JNI!");}jstring 表示返回值
Java_com_example_hellojni_HelloJni_helloTest
写法是Java+Android工程的包名+Android工程的Activity名+方法名,点号用下划线表示,这个写法很严格。
包名:com_example_hellojni
Activity名:HelloJni
方法名:helloTest
JNIEnv* env, jobject thiz是Native方法自带的参数,可以用来转换一个数据类型。也就是说其实这个helloTest是没有形参的。
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 */jstringJava_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ){#if defined(__arm__) #if defined(__ARM_ARCH_7A__) #if defined(__ARM_NEON__) #if defined(__ARM_PCS_VFP) #define ABI "armeabi-v7a/NEON (hard-float)" #else #define ABI "armeabi-v7a/NEON" #endif #else #if defined(__ARM_PCS_VFP) #define ABI "armeabi-v7a (hard-float)" #else #define ABI "armeabi-v7a" #endif #endif #else #define ABI "armeabi" #endif#elif defined(__i386__) #define ABI "x86"#elif defined(__x86_64__) #define ABI "x86_64"#elif defined(__mips64) /* mips64el-* toolchain defines __mips__ too */ #define ABI "mips64"#elif defined(__mips__) #define ABI "mips"#elif defined(__aarch64__) #define ABI "arm64-v8a"#else #define ABI "unknown"#endif return (*env)->NewStringUTF(env, "Hello from JNI ! Compiled with ABI " ABI ".");}效果如下:
0 0
- Android JNI开发一,HelloJni
- JNI学习(一)hellojni
- Android NDK开发之《HelloJni》
- Android JNI入门第一篇——HelloJni
- Android JNI入门第一篇——HelloJni
- Android上,使用JNI调用C的函数,hellojni
- Android JNI开发(一)
- 配置JNI 傻瓜式HelloJNI
- Android-NDK开发之第一个例子HelloJni
- Android Studio2.2 中支持NDK开发HelloJNI例子
- [Android 内核开发] JNI 开发实例(一)
- Android-NDK______2.helloJni
- Android Studio中通过jni实现对C/C++代码的调用,HelloJni,Ubuntu
- Android Native开发(一)JNI
- JNI(一) - Android Studio简单开发流程
- Android NDK开发(一)JNI初体验
- Android Studio中JNI NDK开发(一)
- JNI——手工编译HelloJNI
- 整形short、int、long和long long
- 将整形数据转化成Stirng类型,带有逗号。Converting Integer to String with comma
- 百度移动统计
- iOS 之OC内存管理(二)
- 软件工程第三次作业
- Android JNI开发一,HelloJni
- [Fixed] Whoa! Google Chrome has Crashed error in Windows®
- Linux驱动开发(2):Hello world模块
- 黑马程序员-iOS基础-Objective-C基础(五)
- Zoho Showtime: 你的演讲听众都在看手机吗?
- 二叉树的深度优先遍历和广度优先遍历
- 关于exlipse启动时jdk版本问题
- bash条件测试之空字符串的困惑
- jsp <c:set>标签的使用