📜 ⬆️ ⬇️

Creating a console application using Android NDK

Hello! I have long been interested in the question of how Android is Linux and whether it is possible to run terminal (console) applications in it, bypassing the sandbox called Dalvik.

For this, I learned how to create C / C ++ applications for Android using the Android NDK. NDK allows you to create both libraries with native-methods (C / C ++) for Java, and executable applications to run from the command line.

Here I will tell you how to take the first steps quickly and easily. I will make a reservation in advance that the Android NDK for Windows is inconvenient, therefore OSX and Linux are further considered. Virtual C with Ubuntu to help!

Written for those who have already tried the following:
')
1. Created hello world in C / C ++;
2. Install and try ADB, SDK.
3. Turned on debug mode on Android-smartphone ( Debug mode ).

The next step is to install NDK on the machine and set up environment variables. I recommend to install not the latest version of ndk, since there are glitches with it. I myself put ndk-r10d. Read the instructions here: Ubuntu and SDK | NDK Getting Started | Tools, ADB, SDK, NDK for mac .

Now consider the creation of the simplest console adder of two integers. Create the directory simpleApp and put the jni subdirectory in it. In jni, you need to create a file with the source code of our application simpleApp.c with the following contents:

#include <stdio.h> int main() { int a,b; printf("Enter two numbers a and b: "); scanf("%d %d", &a, &b); printf("a+b = %d\n", a+b); return 0; } 

Then in the same directory you need to create a file with Android.mk compilation settings. A detailed description of this file is available here on Android.mk , but I will give only the minimum necessary content to compile:

 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := simpleApp LOCAL_SRC_FILES := simpleApp.c #   APP_ABI := armeabi #  ARM (   ) TARGET_ARCH_ABI := armeabi include $(BUILD_EXECUTABLE) #  -    

After preparing simppleApp.c and Android.mk, we enter the simpleApp directory through the terminal and run the ndk-build command:

 $ cd simpleApp/ simpleApp$ ndk-build [armeabi] Compile thumb : simpleApp <= simpleApp.c [armeabi] Executable : simpleApp [armeabi] Install : simpleApp => libs/armeabi/simpleApp 

Now the folder / libs / has appeared in the directory simpleApp. It should appear subdirectories with executable files for different platforms. For example, take the file for the arm platform from the armeabi directory (suitable for many common devices) and copy it to a real phone in the / data / local / tmp directory using adb and the push command:

 simpleApp$ cd libs/armeabi/ armeabi$ adb push simpleApp /data/local/tmp [100%] /data/local/tmp/simpleApp 

Now we go into the device through the terminal, go to the directory with our application simpleApp and run it:

 armeabi$ adb shell shell@m3:/ $ cd /data/local/tmp shell@m3:/data/local/tmp $ ./simpleApp Enter two numbers a and b: 6 3 a+b = 9 

Fine! The console application worked, requested the input of numbers and gave the correct result! I hope my notes will help someone in the first steps of mastering the Android NDK tool.

Source: https://habr.com/ru/post/313516/


All Articles