NDK を使ってみる
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
NDK(Native Development Kit)は、Android で C や C++ のコードを使用できるようにするツールセットです。NDK のプラットフォーム ライブラリを活用することで、ネイティブ アクティビティを管理し、センサーやタップ入力など、実際のデバイスのコンポーネントにアクセスできるようになります。それほど経験のない Android プログラマーの場合、アプリを開発する際に必要となるのは Java コードとフレームワーク API に限られるため、通常、NDK はツールとして適していません。NDK が役に立つのは、以下のいずれかに該当する場合です。
- 低レイテンシを実現したり、ゲームや物理学シミュレーションなど、演算負荷の高いアプリを実行したりするために、デバイスからできる限り高いパフォーマンスを引き出す必要がある場合。
- 独自または他のデベロッパーの C / C++ ライブラリを再利用する場合。
Android Studio 2.2 以降を使用すると、NDK によって C や C++ のコードをネイティブ ライブラリにコンパイルし、Gradle(IDE の統合ビルドシステム)によってそのネイティブ ライブラリを APK にパッケージ化できます。これにより、Java コードから JNI(Java Native Interface)経由でネイティブ ライブラリ内の関数を呼び出せるようになります。Gradle と Android ビルドシステムの詳細については、ビルドを設定するをご覧ください。
Android Studio でネイティブ ライブラリをコンパイルするデフォルトのビルドツールは CMake です。また、多数の既存プロジェクトで使われている ndk-build もサポートしています。ただし、新しいネイティブ ライブラリを作成する場合は CMake を使用する必要があります。
このガイドでは、Android Studio 上で NDK の利用を開始するための手順について説明します。Android Studio のバージョンが最新でない場合は、今すぐダウンロードしてインストールしてください。
試験運用版の Gradle ユーザーの皆様へ: ネイティブ プロジェクトですでに CMake または ndk-build を使用している、Gradle ビルドシステムの安定版を使用したい、アドオンツール(CCache など)のサポートを必要としている、のいずれかに当てはまる場合は、プラグイン バージョン 2.2.0 以降に移行し、CMake または ndk-build を使用してネイティブ ライブラリをビルドすることをご検討ください。
こうした条件に当てはまらない場合は、引き続き試験運用版の Gradle と Android プラグインを使用できます。
NDK とツールをダウンロードする
アプリ用のネイティブ コードをコンパイル、デバッグするには、次のコンポーネントが必要です。
- Android Native Development Kit(NDK): Android で C / C++ コードを使用できるようにするツールのセット。
- CMake: Gradle と連携してネイティブ ライブラリをビルドする外部ビルドツール。ndk-build のみを使用する予定の場合は、このコンポーネントは不要です。
-
LLDB: Android Studio がネイティブ コードをデバッグする際に使用するデバッガ。
こうしたコンポーネントのインストールについては、NDK と CMake のインストールと設定をご覧ください。
ネイティブ プロジェクトを作成、インポートする
Android Studio のセットアップが完了したら、C / C++ のサポートを使用して新しいプロジェクトを作成できます。ただし、既存の Android Studio プロジェクトに対してネイティブ コードの追加やインポートを行う場合は、以下の基本プロセスに沿って進める必要があります。
-
新しいネイティブ ソースファイルを作成して、Android Studio プロジェクトに追加します。
- すでにネイティブ コードがある場合や、ビルド済みネイティブ ライブラリをインポートする場合は、この手順をスキップしてください。
-
CMake ビルド スクリプトを作成して、ネイティブ ソースをライブラリにビルドする方法を指定します。このビルド スクリプトは、ビルド済みライブラリやプラットフォーム ライブラリに対してインポートやリンクを行う際にも必要になります。
CMakeLists.txt
ビルド スクリプトが存在する既存のネイティブ ライブラリを使用する場合や、ndk-build を使用して Android.mk
ビルド スクリプトを組み込む場合は、この手順をスキップしてください。
-
CMake または ndk-build スクリプト ファイルへのパスを指定して、Gradle をネイティブ ライブラリにリンクします。Gradle はこのビルド スクリプトを使用してソースコードを Android Studio プロジェクトにインポートし、ネイティブ ライブラリ(SO ファイル)を APK にパッケージ化します。
注: サポートを終了した ndkCompile
ツールを使用している既存プロジェクトの場合は、build.properties
ファイルを開き、次のコード行を削除してから、CMake または ndk-build を使用するように Gradle を設定する必要があります。
// Remove this line
android.useDeprecatedNdk = true
-
[Run]
をクリックしてアプリをビルドして実行します。Gradle が、CMake または ndk-build プロセスを依存関係として追加し、ネイティブ ライブラリをコンパイル、ビルドして APK にパッケージ化します。
実際のデバイス上やエミュレータ上でアプリを実行したら、Android Studio を使用してアプリをデバッグできます。NDK とそのコンポーネントの詳細については、概念をご覧ください。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-26 UTC。
[null,null,["最終更新日 2025-07-26 UTC。"],[],[],null,["# Get started with the NDK\n\nThe Native Development Kit (NDK) is a set of tools that allows you to use C\nand C++ code with Android, and provides [platform libraries](/ndk/guides/stable_apis) you can use to manage\nnative activities and access physical device components, such as sensors and\ntouch input. The NDK may not be appropriate for most novice Android\nprogrammers who need to use only Java code and framework APIs to develop\ntheir apps. However, the NDK can be useful for cases in which you need to do\none or more of the following:\n\n- Squeeze extra performance out of a device to achieve low latency or run computationally intensive applications, such as games or physics simulations.\n- Reuse your own or other developers' C or C++ libraries.\n\n\nUsing [Android Studio 2.2 and higher](/studio), you can\nuse the NDK to compile C and C++ code into a native library and package it\ninto your APK using Gradle, the IDE's integrated build system. Your Java code\ncan then call functions in your native library through the [Java Native Interface (JNI)](http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html) framework. To learn\nmore about Gradle and the Android build system, read [Configure Your Build](/studio/build).\n\n\nAndroid Studio's default build tool to compile native libraries is [CMake](https://cmake.org/). Android Studio also\nsupports [ndk-build](/ndk/guides/ndk-build) due to the large\nnumber of existing projects that use the build toolkit. However, if you are\ncreating a new native library, you should use CMake.\n\n\nThis guide gives you the information you need to get up and running with the\nNDK on Android Studio. If you don't have the latest version of Android\nStudio, [download and install it now](/studio).\n\n\n**Attention experimental Gradle users:** Consider [migrating to plugin version 2.2.0 or higher](http://tools.android.com/tech-docs/new-build-system/gradle-experimental/migrate-to-stable), and using CMake or ndk-build\nto build your native libraries if any of the following apply to you: Your\nnative project already uses CMake or ndk-build; you would rather use a stable\nversion of the Gradle build system; or you want support for add-on tools,\nsuch as [CCache](https://ccache.samba.org/).\nOtherwise, you can continue to [use\nthe experimental version of Gradle and the Android plugin](http://tools.android.com/tech-docs/new-build-system/gradle-experimental).\n\nDownload the NDK and tools\n--------------------------\n\n\nTo compile and debug native code for your app, you need the following\ncomponents:\n\n- The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android.\n- CMake: an external build tool that works alongside Gradle to build your native library. You do not need this component if you only plan to use ndk-build.\n- *LLDB*: the debugger Android Studio uses to debug native code.\n\n\nFor information on installing these components, see [Install and configure the NDK and CMake](/studio/projects/install-ndk).\n\nCreate or import a native project\n---------------------------------\n\n\nOnce you set up Android Studio, you can simply [Create a New Project with\nC/C++ Support](/studio/projects/add-native-code#new-project). However, if you want to add or import native code to an\nexisting Android Studio project, you need to follow this basic process:\n\n1. [Create new\n native source files](/studio/projects/add-native-code#create-sources) and add them to your Android Studio project.\n - You can skip this step if you already have native code or want to import a prebuilt native library.\n2. [Create\n a CMake build script](/studio/projects/configure-cmake#create_script) to tell CMake how to build your native sources into a library. You also require this build script if you are importing and linking against prebuilt or platform libraries.\n - You can skip this step if your existing native library already has a `CMakeLists.txt` build script, or uses ndk-build and includes an [`Android.mk`](/ndk/guides/android_mk) build script.\n3. [Link Gradle to\n your native library](/studio/projects/gradle-external-native-builds) by providing a path to your CMake or ndk-build script file. Gradle uses the build script to import source code into your Android Studio project and package your native library (the SO file) into the APK.\n\n\n **Note:** If your existing project uses the deprecated\n `ndkCompile` tool, you should open your\n `build.properties` file and remove the following line of code\n before configuring Gradle to use CMake or ndk-build: \n\n ```\n // Remove this line\n android.useDeprecatedNdk = true\n ```\n4. [Build and run your app](/studio/run) by clicking **Run** . Gradle adds your CMake or ndk-build process as a dependency to compile, build, and package your native library with your APK.\n\n\nOnce your app is running on a physical device or the emulator, you can use\nAndroid Studio to [Debug your app](/studio/debug).\nOtherwise, to learn more about the NDK and its components, read the [Concepts](/ndk/guides/concepts) page."]]