第一篇:AndroidStudio NDK的配置和使用,jni的第一个demo
2017-04-03 11:20
429 查看
1、NDK 是一组允许您将 C 或 C++(“原生代码”)嵌入到 Android 应用中的工具。
2、好处:
①在平台之间移植其应用。
②重复使用现有库,或者提供其自己的库供重复使用。
③在某些情况下提高性能,特别是像游戏这种计算密集型应用。
3、怎么使用?
现在要做的是从jni中把数据返回,在java中把数据显示。
开发环境是AndroidStudio,先把NDK的环境配置好。
在项目目录下的gradle.properties文件中,添加NDK支持,
具体放在:
使用:
在main目录下,建一个jni文件夹,把以下三个文件复制进去,Android.mk,Application.mk,hello-jni.c
Android.mk:
Application.mk:
新建一个HelloJni的类:
ps:注意,包名是com.example.hellojni,如果你的包名不是这个的,可以改hello-jni.c文件中的Java_com_example_hellojni_HelloJni_stringFromJNI。要不然会找不到生成的so库。比如说你的包名是com.xxx.xxx。那改成Java_com_xxx.xxx_hellojni_HelloJni_stringFromJNI
System.loadLibrary("hello-jni");//"hello-jni"是so库的名字。
在这里可以看到生成的so库:
可能会有疑问,生成的不是libhello-jni么,怎么调用的时候,只是写hello-jni。有可能查找的时候,是去掉或者是默认加上lib,再查找文件名。我猜的,没有去验证。哈哈
Android.mk:必须在 jni 文件夹内创建 Android.mk 配置文件。 ndk-build 脚本将查看此文件,其中定义了模块及其名称、要编译的源文件、版本标志以及要链接的库。
Application.mk:此文件枚举并描述您的应用需要的模块。 这些信息包括:
用于针对特定平台进行编译的 ABI。
工具链。
要包含的标准库(静态和动态 STLport 或默认系统)。
源码下载:http://download.csdn.net/detail/u013147860/9802553
2、好处:
①在平台之间移植其应用。
②重复使用现有库,或者提供其自己的库供重复使用。
③在某些情况下提高性能,特别是像游戏这种计算密集型应用。
3、怎么使用?
现在要做的是从jni中把数据返回,在java中把数据显示。
开发环境是AndroidStudio,先把NDK的环境配置好。
在项目目录下的gradle.properties文件中,添加NDK支持,
android.useDeprecatedNdk=true然后在app目录下的build.gradle文件中写入生成so库的名字和输入指定的平台。
ndk{ moduleName "hello-jni" //生成的so文件名字,调用C程序的代码中会用到该名字 abiFilters "armeabi", "armeabi-v7a", "x86" //输出指定三种平台下的so库 }
具体放在:
defaultConfig { applicationId "com.example.hellojni" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" ndk{ moduleName "hello-jni" //生成的so文件名字,调用C程序的代码中会用到该名字 abiFilters "armeabi", "armeabi-v7a", "x86" //输出指定三种平台下的so库 } }到此为,NDK的环境已经配置好了。
使用:
在main目录下,建一个jni文件夹,把以下三个文件复制进去,Android.mk,Application.mk,hello-jni.c
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)#此变量表示源文件在开发树中的位置。在这里,构建系统提供的宏函数 my-dir 将返回当前目录(包含 Android.mk 文件本身的目录)的路径。 include $(CLEAR_VARS)#CLEAR_VARS 变量指向特殊 GNU Makefile,可为您清除许多 LOCAL_XXX 变量,例如 LOCAL_MODULE、LOCAL_SRC_FILES 和 LOCAL_STATIC_LIBRARIES。 请注意,它不会清除 LOCAL_PATH。此变量必须保留其值,因为系统在单一 GNU Make 执行环境(其中所有变量都是全局的)中解析所有构建控制文件。 在描述每个模块之前,必须声明(重新声明)此变量。 LOCAL_MODULE := hello-jni#LOCAL_MODULE 变量将存储您要构建的模块的名称.生成一个名为 libhello-jni.so 的库。 LOCAL_SRC_FILES := hello-jni.c#LOCAL_SRC_FILES 变量必须包含要构建到模块中的 C 和/或 C++ 源文件列表。 include $(BUILD_SHARED_LIBRARY)#帮助系统将所有内容连接到一起.BUILD_SHARED_LIBRARY 变量指向 GNU Makefile 脚本,用于收集您自最近 include 后在 LOCAL_XXX 变量中定义的所有信息。 此脚本确定要构建的内容及其操作方法。
Application.mk:
APP_ABI := all #Application.mk 文件实际上是定义要编译的多个变量的微小 GNU Makefile 片段。 #它不会实际进入生成的共享库或最终软件包。APP_ABI := all指的是所有支持的指令集。 #也可以指定多个值,将它们放在同一行上,中间用空格分隔。例如: #APP_ABI := armeabi armeabi-v7a x86 mipshello-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 Lic 4000 ense 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 ) { #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 "."); }
新建一个HelloJni的类:
/* * 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"); } }
ps:注意,包名是com.example.hellojni,如果你的包名不是这个的,可以改hello-jni.c文件中的Java_com_example_hellojni_HelloJni_stringFromJNI。要不然会找不到生成的so库。比如说你的包名是com.xxx.xxx。那改成Java_com_xxx.xxx_hellojni_HelloJni_stringFromJNI
System.loadLibrary("hello-jni");//"hello-jni"是so库的名字。
在这里可以看到生成的so库:
可能会有疑问,生成的不是libhello-jni么,怎么调用的时候,只是写hello-jni。有可能查找的时候,是去掉或者是默认加上lib,再查找文件名。我猜的,没有去验证。哈哈
Android.mk:必须在 jni 文件夹内创建 Android.mk 配置文件。 ndk-build 脚本将查看此文件,其中定义了模块及其名称、要编译的源文件、版本标志以及要链接的库。
Application.mk:此文件枚举并描述您的应用需要的模块。 这些信息包括:
用于针对特定平台进行编译的 ABI。
工具链。
要包含的标准库(静态和动态 STLport 或默认系统)。
源码下载:http://download.csdn.net/detail/u013147860/9802553
相关文章推荐
- Android Studio NDK JNI配置Demo超详细2016
- Android Studio NDK环境配置和JNI的使用
- Android studio NDK笔记1-NDK配置,你的第一个JNI
- Android Studio NDK环境配置及JNI使用方法
- Android Studio NDK JNI配置Demo超详细2016
- JNI基础(二)Android Studio 配置NDK
- Android Studio NDK-Jni开发小Demo
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法实现边缘检测代码(2)
- Android Studio JNI配置及.so库使用
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码
- Android Studio使用新的Gradle构建工具配置NDK环境(一)
- android studio ndk 配置使用(好用的链接)
- Android Studio JNI/NDK 编程(二) Windows 下环境搭建 demo 开发
- Android的JNI【实战教程】2⃣️--AS下NDK环境配置及第一个工程
- Android Studio NDK环境配置(opencv)以及使用
- Android Studio jni的开发与ndk的配置
- Android中JNI使用详解(1)---Eclipse中NDK配置So文件生成
- android-studio下ndk的配置和简单JNI调用遇到的问题
- Android中JNI使用详解(2)---Android Studio中NDK环境配置