Code Monkey home page Code Monkey logo

android-cmake-sample's Introduction

NDK version CMake version

Android & CMake sample

Put his hands in the native code (C/C++) with the Android NDK can be a hard task for an Android developer used to Java/Kotlin and Gradle.

This sample shows several use cases about how to use CMake:

  • to build native library without Android Studio
  • to use prebuilt library (shared (.so) or static (.a))
  • to depends on another native module from your native module
  • to build Android application with native code.

Project

The application allows you to do some computations on 2 numbers given in input. Computations are realized by native libraries, written in C/C++, embedded in the application. Each computation is realized by one of the library and each library is imported in a different way using CMake.

The project contains 4 modules:

  • app
  • subdirectory
  • shared_library
  • static_library

Modules

The app module contains the Android application code and depends on other modules to do computations.

Other modules contains C/C++ code and each one does one different computation:

  • subdirectory does a multiplication on 2 numbers.
  • shared_library does an addition on 2 numbers.
  • static_library does a subtraction on 2 numbers.

App module

This module contains the code of the Android application. It contains Kotlin code to manage a basic Android application but also C/C++ code to communicate with the native libraries this application depends on.

Dependencies on native libraries are defined in the CMakeList targeted by the build.gradle of the module:

externalNativeBuild {
    cmake {
        path "src/main/cpp/CMakeLists.txt"
    }
}

The C/C++ code uses JNI to create a bridge between Kotlin code and pure C/C++ code. It allows to write functions which are called by Kotlin code and from inside JNI function, to call any C/C++ function.

Subdirectory module

This module is imported by the app module with a CMake dependency. The CMakeList used by the app module targets the CMakeList of this module with the command add_subdirectory.

The library is built as a shared library. A .so file is generated and contains all the code.

The include directory contains all public headers required by the app module to be able to use it.

The library exposes its headers by using target_include_directories with PUBLIC visibility.

Shared_library module

This module allows to generate a shared library, a .so file, by executing the library_built.sh script.

This module is not built each time the app module is built. app module only targets the pre-generated .so file and its headers inside the include directory.

To import headers in the app module, we use the command include_directories

Static_library module

This module allows to generate a static library, a .a file, by executing the library_built.sh script.

This module is not built each time the app module is built. app module only targets the pre-generated .a file and its headers inside the include directory.

To import headers in the app module, we use the command include_directories

Resources:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.