热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

AndroidJNI学习之Concepts

2019独角兽企业重金招聘Python工程师标准ConceptsBeforeBeginningThisguideassumesthatyouare:Alreadyfamili

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Concepts

Before Beginning


This guide assumes that you are:

  • Already familiar with concepts inherent in native programming and in Android development.
  • Working in Eclipse, and using the Android Development Tools (ADT), except where otherwise noted.

 

Introduction


This section provides a high-level explanation of how the NDK works. The Android NDK is a set of tools allowing you to embed C or C++ (“native code”) into your Android apps. The ability to use native code in Android apps can be particularly useful to developers who wish to do one or more of the following:

  • Port their apps between platforms.
  • Reuse existing libraries, or provide their own libraries for reuse.
  • Increase performance in certain cases, particularly computationally intensive ones like games.

How it Works


This section introduces the main components used in building a native application for Android, and goes on to describe the process of building and packaging.

 

Main components

You should have an understanding of the following components as you build your app:

在你建造你的应用程序时你应该对一下的组件有一个认识

  • ndk-build: The ndk-build script launches the build scripts at the heart of the NDK. These scripts:

    ndk-build  ndk-build  脚本在NDK内部启动这个建造脚本

    • Automatically probe your development system and app project file to determine what to build.
    • 自动检测你的开发系统和应用程序工程文件去检测要如何去构造。
    • Generate binaries.
    • 生成二进制文件
    • Copy the binaries to your app's project path.
    • 复制这二进制文件到你的应用程序工程路径下

    For more information, see ndk-build.

  • Java: From your Java source, the Android build process generates .dex (Dalvik EXecutable) files, which are what the Android OS runs in the Dalvik Virtual Machine (“DVM”). Even if your app contains no Java source code at all, the build process still generates a .dex executable file within which the native component runs.

    java:

  • When developing Java components, use the native keyword to indicate methods implemented as native code. For example, the following function declaration tells the compiler that the implementation is in a native library:

  • 在开发java组件时,使用native 关键字去表明方法是本地代码的实现。例如,一下的方法声明告诉编辑器这实现是在本地库中,

    public native int add(int  x, int  y);

  • Native shared libraries: The NDK builds these libraries, or .so files, from your native source code.

    NDK从你的本地源代码中构建函数库或者.so文件

  • Note: If two libraries implement respective methods with the same signature, a link error occurs. In C, "signature" means method name only. In C++, "signature" means not only method name, but also its argument names and types.

如果两个函数库用一样的签名实现各自的方法,会发生连接错误。在C中,签名只意味着方法名,在C++中签名不单单代表方法名,也代表参数名和类型。

  • Native static libraries: The NDK can also build static libraries, or .a files, which you can link against other libraries.

本地静态函数库:NDK也可以建造本地静态函数库或者a文件。你可以再次连接到其他的函数库

  • Java Native Interface (JNI): The JNI is the interface via which the Java and C++ components talk to one another. This guide assumes knowledge of the JNI; for information about it, consult the Java Native Interface Specification.

java 本地接口JNI:JNI是一种接口,是java 和C++组件交流的的一种方式,本指南假设的知识信息是关于JNI的,

  • Application Binary Interface (ABI): The ABI defines exactly how your app's machine code is expected to interact with the system at runtime. The NDK builds .so files against these definitions. Different ABIs correspond to different architectures: The NDK includes ABI support for ARMEABI (default), MIPS, and x86. For more information, see ABI Management.

应用程序库函数接口ABI定义了如何以运行的系统所期望的方式解析你应用程序机器代码并与之交互,NDK构造的so文件与这些定义相反,

  • Manifest: If you are writing an app with no Java component to it, you must declare the NativeActivityclass in the manifest. Native Activities and Applications provides more detail on how to do this, under “Using the native_activity.h interface.”
  • 清单:如果您正在编写一个应用程序没有Java组件,必须声明中的NativeActivityclass清单。本机的活动和应用程序提供了更多的细节关于如何做到这一点,在“使用native_activity。h接口。”

The following two items are only required for building using the ndk-build script, and for debugging using thendk-gdb script.

  • Android.mk: You must create an Android.mk configuration file inside your jni folder. The ndk-buildscript looks at this file, which defines the module and its name, the source files to be compiled, build flags and libraries to link.
  • 你需要创建一耳光android,mk配置文件在你的jni文件夹中,ndk-build脚本查看着文件夹,它定义了模块及其名称,编译源文件,建立旗帜和库链接。
  • Application.mk: This file enumerates and describes the modules that your app requires. This information includes:

    这文件描述和计算你应用程序需要的模块

    ABIs used to compile for specific platforms.

  • 本地库函数接通使用去编译指定的平台

    • Toolchains.
    • 工具链
    • Standard libraries to include (static and dynamic STLport or default system).
    • 加入标准的库函数
Flow

The general flow for developing a native app for Android is as follows:

  1. Design your app, deciding which parts to implement in Java, and which parts to implement as native code.

    Note: While it is possible to completely avoid Java, you are likely to find the Android Java framework useful for tasks including controlling the display and UI.

  2. Create an Android app Project in Eclipse as you would for any other Android project.
  3. If you are writing a native-only app, declare the NativeActivity class in AndroidManifest.xml. You can do so from the Eclipse/ADT Android Manifest Editor, or by hand-editing the file. For more information, see theNative Activities and Applications.
  4. Create an Android.mk file describing the native library, including name, flags, linked libraries, and source files to be compiled in the ‘JNI’ directory.
  5. Optionally, you can create an Application.mk file configuring the target ABIs, toolchain, release/debug mode, and STL. For any of these that you do not specify, the following default values are used, respectively:
    • ABI: armeabi
    • Toolchain: GCC 4.8
    • Mode: Release
    • STL: system
  6. Place your native source under the project's jni directory.
  7. Use ndk-build to compile the native (.so, .a) libraries.
  8. Build the Java component, producing the executable .dex file.
  9. Package everything into an APK file, containing .so, .dex, and other files needed for your app to run.

Note that Eclipse can perform steps 7. through 9. in a single operation.

Native Activities and Applications


The Android SDK provides a helper class, NativeActivity, that allows you to write a completely native activity.NativeActivity handles the communication between the Android framework and your native code, so you do not have to subclass it or call its methods. All you need to do is declare your application to be native in yourAndroidManifest.xml file, and begin creating your native application.

An Android application using NativeActivity still runs in its own virtual machine, sandboxed from other applications. You can therefore still access Android framework APIs through the JNI. In certain cases, however–such as for sensors, input events, and assets–the NDK provides native interfaces that you can use instead of having to call across the JNI. For more information about such support, see Android NDK Native APIs.

Regardless of whether or not you are developing a native activity, we recommend that you create your projects with the traditional Android build tools. Doing so helps ensure building and packaging of Android applications with the correct structure.

The Android NDK provides you with two choices to implement your native activity:

  • The native_activity.h header defines the native version of the NativeActivity class. It contains the callback interface and data structures that you need to create your native activity. Because the main thread of your application handles the callbacks, your callback implementations must not be blocking. If they block, you might receive ANR (Application Not Responding) errors because your main thread is unresponsive until the callback returns.
  • The android_native_app_glue.h file defines a static helper library built on top of the native_activity.hinterface. It spawns another thread, which handles things such as callbacks or input events in an event loop. Moving these events to a separate thread prevents any callbacks from blocking your main thread.

The /sources/android/native_app_glue/android_native_app_glue.c source is also available, allowing you to modify the implementation.

For more information on how to use this static library, examine the native-activity sample application and its documentation. Further reading is also available in the comments in the/sources/android/native_app_glue/android_native_app_glue.h file.

Using the native_activity.h interface

To implement a native activity with the native_activity.h interface:

  1. Create a jni/ directory in your project's root directory. This directory stores all of your native code.
  2. Declare your native activity in the AndroidManifest.xml file.

    Because your application has no Java code, set android:hasCode to false.

    You must set the android:name attribute of the activity tag to NativeActivity.

                android:label="@string/app_name">

    Note: You can subclass NativeActivity. If you do, use the name of the subclass instead ofNativeActivity.

    The android:value attribute of the meta-data tag specifies the name of the shared library containing the entry point to the application (such as C/C++ main), omitting the lib prefix and .so suffix from the library name.

                          android:value="native-activity" />
               
                 
                 
               

             
           
         

  3. Create a file for your native activity, and implement the function named in the ANativeActivity_onCreatevariable. The app calls this function when the native activity starts. This function, analogous to main in C/C++, receives a pointer to an ANativeActivity structure, which contains function pointers to the various callback implementations that you need to write. Set the applicable callback function pointers inANativeActivity->callbacks to the implementations of your callbacks.
  4. Set the ANativeActivity->instance field to the address of any instance of specific data that you want to use.
  5. Implement anything else that you want your activity to do upon starting.
  6. Implement the rest of the callbacks that you set in ANativeActivity->callbacks. For more information on when the callbacks are called, see Managing the Activity Lifecycle.
  7. Develop the rest of your application.
  8. Create an Android.mk file in the jni/ directory of your project to describe your native module to the build system. For more information, see Android.mk.
  9. Once you have an Android.mk file, compile your native code using the ndk-build command.

    $ cd //
    $ /ndk-build

  10. Build and install your Android project as usual, using Ant or Eclipse. If your native code is in the jni/directory, the build script automatically packages the .so file(s) built from it into the APK.

转:https://my.oschina.net/zaizaiangels/blog/533400



推荐阅读
  • Gradle复合构建详解
    自Gradle 3.3起,复合构建功能得以实现,这是一种能够整合其他独立构建的高级构建模式。本文将详细介绍复合构建与多项目构建的区别,以及如何在实际项目中应用复合构建。 ... [详细]
  • Java EE CDI:解决依赖关系冲突的实例
    在本教程中,我们将探讨如何在Java EE的CDI(上下文和依赖注入)框架中有效解决依赖关系的冲突问题。通过学习如何使用限定符,您将能够为应用程序的不同客户端提供多种接口实现,并确保每个客户端都能正确调用其所需的实现。 ... [详细]
  • 本文探讨了如何通过预处理器开关选择不同的类实现,并解决在特定情况下遇到的链接器错误。 ... [详细]
  • 云函数与数据库API实现增删查改的对比
    本文将深入探讨使用云函数和数据库API实现数据操作(增删查改)的不同方法,通过详细的代码示例帮助读者更好地理解和掌握这些技术。文章不仅提供代码实现,还解释了每种方法的特点和适用场景。 ... [详细]
  • 在高并发需求的C++项目中,我们最初选择了JsonCpp进行JSON解析和序列化。然而,在处理大数据量时,JsonCpp频繁抛出异常,尤其是在多线程环境下问题更为突出。通过分析发现,旧版本的JsonCpp存在多线程安全性和性能瓶颈。经过评估,我们最终选择了RapidJSON作为替代方案,并实现了显著的性能提升。 ... [详细]
  • 深入解析Spring启动过程
    本文详细介绍了Spring框架的启动流程,帮助开发者理解其内部机制。通过具体示例和代码片段,解释了Bean定义、工厂类、读取器以及条件评估等关键概念,使读者能够更全面地掌握Spring的初始化过程。 ... [详细]
  • 并发编程 12—— 任务取消与关闭 之 shutdownNow 的局限性
    Java并发编程实践目录并发编程01——ThreadLocal并发编程02——ConcurrentHashMap并发编程03——阻塞队列和生产者-消费者模式并发编程04——闭锁Co ... [详细]
  • 本文详细介绍如何使用 Python 集成微信支付的三种主要方式:Native 支付、APP 支付和 JSAPI 支付。每种方式适用于不同的应用场景,如 PC 网站、移动端应用和公众号内支付等。 ... [详细]
  • 在寻找轻量级Ruby Web框架的过程中,您可能会遇到Sinatra和Ramaze。两者都以简洁、轻便著称,但它们之间存在一些关键区别。本文将探讨这些差异,并提供详细的分析,帮助您做出最佳选择。 ... [详细]
  • 本文详细介绍了如何在现有的Android Studio项目中集成JNI(Java Native Interface),包括下载必要的NDK和构建工具,配置CMakeLists.txt文件,以及编写和调用JNI函数的具体步骤。 ... [详细]
  • Flowable 6.6.0 表单引擎在Web应用中的集成与使用
    本文档提供了Flowable 6.6.0版本中表单引擎在Web应用程序中的配置和使用指南,包括表单引擎的初始化、配置以及在Web环境下的具体实现方法。 ... [详细]
  • Eclipse 下 JavaFX 程序开发指南
    本文介绍了 JavaFX,这是一个用于创建富客户端应用程序的 Java 图形和媒体工具包,并详细说明了如何在 Eclipse 环境中配置和开发 JavaFX 应用。 ... [详细]
  • 本文详细解析了Java编程语言中的浅克隆和深克隆概念,通过实例代码演示了两者的区别与应用场景,帮助开发者更好地理解和使用对象克隆技术。 ... [详细]
  • 由二叉树到贪心算法
    二叉树很重要树是数据结构中的重中之重,尤其以各类二叉树为学习的难点。单就面试而言,在 ... [详细]
  • 如何使用Ionic3框架创建首个混合开发应用
    混合开发是指结合原生(Native)与网页(Web)技术进行移动应用开发的方法。本文将详细介绍如何利用Ionic3这一流行的混合开发框架,从环境搭建到创建并运行首个应用的全过程。 ... [详细]
author-avatar
版中凌菱
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有