陳奕霖
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Android Native Development Kit (NDK) ###### tags: `android` `ndk` ## Android Studio * This guide assume you to have a basic understanding of using Android Studio and Android development. * Otherwise, please refer to the official tutorial (see [Meet Android Studio](https://developer.android.com/studio/intro/)) ## Introduction to NDK * NDK (Native Development Kit) : a toolset that lets you implement parts of your app in native code, using languages such as C and C++. * Java Native Interface (JNI): The JNI is the interface via which the Java and C++ components talk to one another. For information about it, consult the [Java Native Interface Specification](https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html). ![](https://i.imgur.com/lVObq5J.png) * ndk-build: The ndk-build script launches the build scripts at the heart of the NDK. These scripts: * 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 * 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. * Native shared libraries: The NDK builds these libraries, or .so files, from your native source code. * Native static libraries: The NDK can also build static libraries, or .a files, which you can link against other libraries. * 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 32-bit ARM, AArch64, x86, and x86-64. For more information, see [ABI Management](https://developer.android.com/ndk/guides/abis). * How a native project works ? ![](https://i.imgur.com/KJMVSAk.png) * Java Native Library: Our java file that import native libraries and declare native functions. * javah -jni : compile our java.class file to head file * CMake : using CMake to compile C++ source code and header file to dynamic library (.so) and interact with Java file by JNI. * Gradle : a custom build tool used to build android packages (apk files) by managing dependencies and providing custom build logic. * reference : https://developer.android.com/ndk/guides/concepts ## NDK Project Example : NativeDemo code download : http://git.jd.com/intern-svrdc10/SimpleAPP ## Install Dependencies * Open Android SDK Manager in Android Studio * in tool bar ![](https://i.imgur.com/dvml9lk.png =400x30) * or in tools ![](https://i.imgur.com/oJQPXfw.png =300x400) * Select CMake, LLDB, NDK ![](https://i.imgur.com/9528fWB.png =500x380) * Press OK to start installing * set system environment variable, add belows into PATH (optional) ``` > export ANDROID_SDK=/path/to/your/Android/sdk > export ANDROID_NDK=/path/to/your/Android/sdk/ndk-bundle > export PATH="$PATH:$ANDROID_SDK/tools:$ANDROID_SDK/platform-tools:$ANDROID_NDK" ``` ### Create a Native Android Project * Enable C++ support ![](https://i.imgur.com/PvdKTuh.png) * Enable Exception and RTTI Support ![](https://i.imgur.com/jEyXOqe.png) * Run the project ![](https://i.imgur.com/J8i7HWl.png =270x480) ### Native Source Code : native-lib.cpp * /app/src/main/cpp/native-lib.cpp ```cpp= #include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_jd_nativedemo_Hello_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } ``` * define your native function here. * function named rule : Java\_{package name}\_{class name}\_{function name} * JNIEnv : JNI interface pointer, it is related to caller's (in Java) thread, which helps us to us Java's function. ### CMakeList.txt * define how you build your native code. * basic setting ``` cmake_minimum_required(VERSION 3.4.1) ``` * build our target native code to libraray (SHARED $\to$ .so file, STATIC $\to$ .a file) ``` add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp ) ``` * using other libraries, like log-lib, the library provided by Android for printing log information. ``` find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log ) ``` * link our target native library to it dependencies. ``` target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} ) ``` * after re-build the project, we can find our native-lib becomes a .so library in app/build/intermediates/cmake/debug/obj/{ABI}/ ![](https://i.imgur.com/icimMmY.png) ### Add our Java Native Library class : Hello.java * /app/src/main/java/com/xxx/nativedemo/Hello.java * use our native function here. * include our native library * declare the native function. use the **native** keyword to indicate methods implemented as native code. ```java= public class Hello { // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ public static native String stringFromJNI(); } ``` ## Change NDK Version (optional) * In some situation, you may want to using different version of NDK. Therefore, you need to know how to manually change the version. * download the version you want in ([link](https://developer.android.com/ndk/downloads/)) * Unzip the folder. * move all the files in android-ndk-rxxx/ to /your/sdk/path/ndk-bundle. (replace the original files) * how to find your sdk path? open your sdk manager ![](https://i.imgur.com/2sKV4ab.png) * Clean the project and rebuild ![](https://i.imgur.com/9wICwqs.png =300x330) ## Reference * [Add C and C++ code to Your project](https://developer.android.com/studio/projects/add-native-code?hl=zh-us) * [android Ndk学习笔记01之Hello World!](https://www.jianshu.com/p/e16834a6b939) * [Android NDK开发(一) 使用CMake构建工具进行NDK开发](https://blog.csdn.net/kong_gu_you_lan/article/details/79091789)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully