Mobile Development 11 min read

New Features of Android Studio Native Development and How to Link Existing Native Code

Android Studio 2.2 introduces official native development support with CMake and ndk‑build integration, Gradle‑packaged .so libraries, LLDB hybrid debugging, enhanced C/C++ editing, easy linking of existing native code via IDE or build.gradle, new externalNativeBuild and abiFilters settings, and a concise CMake workflow.

Tencent Music Tech Team
Tencent Music Tech Team
Tencent Music Tech Team
New Features of Android Studio Native Development and How to Link Existing Native Code

1. Android Studio Native Development New Features

Support for both CMake (default) and ndk-build build systems. The experimental Gradle plugin is still available but migration to the official approach is recommended.

The generated .so libraries are packaged directly into the APK by Gradle.

LLDB is used as the native debugger, providing a hybrid Java‑Native debugging experience.

The IDE now offers native code editing support.

When searching for references to a native method, the IDE lists both Java and native (C/C++) symbols and their usages.

These features require Android Gradle Build Tools ≥ 2.2.0 and Gradle ≥ 2.14.1.

2. Linking Existing Native Code to an Android Studio Project

Google allows the use of ndk-build in Android Studio. Developers need to provide an Android.mk (and optionally an Application.mk ) and update the build.gradle file. During the build, Gradle invokes ndk-build and packages the resulting .so into the APK.

Method 1: Using the Android Studio IDE

Android Studio can automatically configure Gradle to link native sources:

Select the project in the Project view, open the File menu and choose Link C++ Project with Gradle .

In the dialog, select the build system ( ndk-build ) and locate the Android.mk file.

Confirm and wait for Gradle to finish. The native project is now linked.

For CMake‑based native code, provide the path to CMakeLists.txt> instead.

Method 2: Editing build.gradle Directly

If custom build parameters are needed, modify build.gradle . Without extra parameters, Gradle uses default settings, e.g., it builds .so for all ABIs unless APP_ABI is set in Application.mk .

3. New Members of build.gradle Related to Native Builds

3.1 android/externalNativeBuild Block

When linking native code via Method 1, Gradle adds an externalNativeBuild block under android :

externalNativeBuild {
    ndkBuild {
        path '[relative path to linked native code]/Android.mk'
    }
}

Replace ndkBuild with cmake for CMake projects, and set path to the location of CMakeLists.txt .

3.2 android/defaultConfig/ndkBuild (or cmake ) Block

Here you can add build arguments, C/C++ flags, etc. Example:

android {
    ...
    defaultConfig {
        ...
        ndkBuild {
            arguments "NDK_DEBUG=1"
            cFlags "-D_EXAMPLE_C_FLAG"
            cppFlags "-D__STDC_FORMAT_MACROS"
        }
    }
}

For CMake you might add arguments "-DANDROID_STL=gnustl_static" to select a static STL.

3.3 abiFilters Field

This field defines which ABIs are built and packaged into the APK. It can appear in android/defaultConfig/ndk (affects packaging) or in android/defaultConfig/ndkBuild (affects only building). It mirrors the APP_ABI setting in Application.mk . Example:

android {
    ...
    defaultConfig {
        ...
        ndk {
            abiFilters 'x86','armeabi','armeabi-v7a','arm64-v8a'
        }
    }
}

4. Introduction to CMake

From the latest Android Studio version, CMake is the default tool for building native libraries. A simple CMakeLists.txt example:

# CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_library(native-lib src/main/cpp/native-lib-src.cpp)
find_library(ndk_log log)
target_link_libraries(native-lib ${ndk_log})

Key CMake commands explained:

cmake_minimum_required(VERSION x.x.x) – specifies the minimum required CMake version (≥ 3.4.0 for Android).

add_library(<name> [STATIC|SHARED|MODULE] source...) – creates a native library; SHARED produces a .so file.

include_directories(dir...) – adds header search paths.

find_library(<VAR> lib [path...]) – locates a prebuilt NDK library (e.g., log ) and stores its path in <VAR> .

target_link_libraries(<target> <item>...) – links the target with other libraries, such as the NDK log library, static libraries created with add_library , or imported shared libraries.

Example of adding an imported library:

# Add an already built library named imported-lib
add_library(imported-lib SHARED IMPORTED)
set_target_properties(imported-lib PROPERTIES IMPORTED_LOCATION imported-lib/src/${ANDROID_ABI}/libimported-lib.so)
include_directories(imported-lib/include/)
target_link_libraries(native-lib imported-lib)

After modifying CMakeLists.txt , use Build → Refresh Linked C++ Projects to trigger Gradle → CMake rebuild. The cpp node in the Project view updates accordingly. When multiple native libraries exist, they appear as separate entries.

5. Summary and Outlook

Compared with the earlier experimental Gradle plugin, the official support for CMake and ndk-build in Android Studio 2.2 offers a more straightforward native development workflow. The new features encourage developers to adopt native development within Android Studio. Additional resources include the official Android documentation and a mirror provided by Tencent Bugly for environments with limited access to Google servers.

mobile developmentGradleNative Developmentandroid-studiocmakendk-build
Tencent Music Tech Team
Written by

Tencent Music Tech Team

Public account of Tencent Music's development team, focusing on technology sharing and communication.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.