How to use C/C ++ to develop a native program running on Android without using ADK and NDK

Source: Internet
Author: User

Familiar with Android and Linux
Developers will laugh at this weak article, but this article is intended for developers who do not know it. Of course, I will be very happy if you are familiar with it and intend to continue reading it. :)

As we all know, Android is based on Linux, so everyone can think that Android should be able to run programs in Linux, but this platform uses ARM
So where can I find an ARM Linux compiler? Here I recommend the Sourcery G ++ compiler, which is:

Http://www.codesourcery.com/sgpp/lite/arm/portal/release1803

Because my system is Windows, I downloadedIA32 Windows InstallerAfter the installation is completed step by step, we can use C/C ++
Write a program, compile it, and run it on AVD or your machine.

Below is a Hello World Program I wrote in C.

#include <stdio.h>int main(void){char s[20];scanf("%s",s);printf("Hello world, from %s.\n",s);return 0;}

SaveHello. cBecause
Android Linux kernel does not have standard I/O library functions, so we use static compilation.

Arm-none-linux-gnueabi-gcc-static
-O hello. c

Then we use adb to upload the hello program to AVD or your machine's/data
Directory. You may wonder why you want to upload the file to this directory? The reason is that the/system/bin and/sbin directories of Android are Read Only.
System, and all the file owner users under/mnt/sdcard are system, and the permission group is sdcard_r, even if it is root
The account cannot add executable permissions for files. Of course, you may also find other directories that can upload and change file permissions.

Adb push hello/data

Then we use adb
Shell Command to enter the shell of your Android device and change its permissions.


Adb shell


Cd/data


Chmod 777 hello

The execution result is as follows:


./Hello


Newcj


Hello world, from newcj.


How about it? Interesting. Please try it now. I believe you will also feel interesting!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.