Skip to content

Android NDK with VSCode configuration

VScode is a cross-platform code editor launched by Microsoft with powerful functions and rich plug-ins. Using VSCode can greatly improve our development efficiency.This article introduces how to develop Android native programs through VSCode and NDK on Ubuntu.

Prerequisites

  • CMake3.13+
  • VSCode1.62+
  • NDK r23b+
  • ADB

Building

Create directory for your project

In this example, I named the project perf_test.

mkdir perf_test

Open the folder created in VSCode

File->OpenFolder, select the folder you just created.

Create CMakeList.txt

Add following codes.

cmake_minimum_required(VERSION 3.0.0)
project(perf_test VERSION 0.1.0)
​
include_directories(".")
aux_source_directory(. DIR_SRCS)
​
add_compile_options(-std=c++17)
include_directories("/your_path/your_ndk/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.9/include")
set(CMAKE_C_COMPILER /your_path/your_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang)
set(CMAKE_CXX_COMPILER /your_path/your_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android28-clang++)
​
add_executable(perf_test ${DIR_SRCS})
target_link_libraries(perf_test "log")
target_link_libraries(perf_test "z")
target_link_libraries(perf_test "dl")
target_link_libraries(perf_test "m")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O2-mcpu=cortex-a8 -mfpu=neon -mfloat-abi=hard -ffast-math")
add_compile_options(-Wl,--as-needed)
​
message(STATUS "build type: ${CMAKE_BUILD_TYPE}")

Create source code file

Create main.cpp, add following codes.

#include <iostream>

int main(int argc, char ** argv)
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Create build script

Create build.sh, add following codes.

#!/bin/bash

check_error()
{
  if [ $1 == 0 ]; then
      return
  fi
  echo -e "\033[1;31merror occurs in $2 with code $1. \033[0m"
  exit $1
}

echo "building for anfroid..."
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make; ret=$?
cd ..
check_error $ret "building on android"

Grant execution rights and build

chmox +x build.sh
./build.sh

Then outputs:

building for anfroid...
-- build type: Release
-- Configuring done
-- Generating done
-- Build files have been written to: /data/src/perf_test/build
[ 50%] Building CXX object CMakeFiles/perf_test.dir/main.cpp.o
[100%] Linking CXX executable hpcTest
[100%] Built target perf_test

Built successfully.

Show the executable file built

file ./build/perf_test

Then outputs:

./build/perf_test: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, not stripped

Runing

Create the folder to run the program

adb shell mkdir -p /data/local/tmp/test/

Push executable file to the folder

adb push ./build/perf_test /data/local/tmp/test/

Outputs:

./build/perf_test: 1 file pushed, 0 skipped. 22.5 MB/s (120664 bytes in 0.005s)

Execute the program

adb shell /data/local/tmp/test/perf_test

Outputs :

Hello World!

The output is as expected.

Create a script for run the program automatically

Create a file named run.sh, add following commands to the file.

#!/bin/bash
adb shell mkdir -p /data/local/tmp/test/
adb push ./build/perf_test /data/local/tmp/test/
adb shell /data/local/tmp/test/perf_test

Then grant execution rights to the file.

The next time we run the program, we can directly use the following command.

./run.sh
Leave a Reply