Skip to content
FX. J. Adi Lima edited this page Feb 21, 2017 · 11 revisions

Build Android Application From Inside Android

I don't know how to write properly, but at least I have to write it down, so that somebody can react to my thoughts. I want to use Android OS as primary operating system for everyday use, not just curious, but seriously I think this can be done. And I guess I'm not the only one here, at least from a programmer's point of view, you can cut the build time (and also the time and effort for just testing small changes) if you write programs for android.

Let me give you some illustrations, let's say we have a small program for tutorial, nothing serious, just to be able to see your code running, what do you usually do?

  1. Open Eclipse or Android Studio, and create project.
  2. Wait for either one of them to finish preparing your new project, in this case you'll have to wait for a while, takes time, especially if you only have something like Core 2 Duo computer.
  3. Design your UI after your project ready.
  4. Start writing your codes.
  5. Build and make APK.
  6. Run an emulator of your choice to run the application and see what it looks like (takes time to do it). Alternatively, you can use adb to inject your codes into an appropriate device powered by Android (faster, but still annoying).

Now, usually, with common desktop projects targeting for the same hardware and OS as tour computer, after a successful build, you can simply press a button on your keyboard to run your application and test the result. With Android, it's not that simple, and I really doubt that it will be simple in near future. Android is not common Linux, though it uses Linux kernel, but they are different OS, not just different Linux distro, so you cannot chroot into Android's environment as well.

But this time you won't even need to chroot, I mean, after compiling your programs, you can just install run it as you usually do with any other desktop programs, but of course, you'll have to learn how to use it. But it's not hard, I can promise you this. It's gonna be a very short example, written in C++ (that's not a typo, I mean it, C++), and you won't even need to download NDK. It's already there.

First, you will need an app called termux (you can find it in the Play Store), install it, you can use your phone this time. After installation, before doing anything else, I suggest you upgrade, in case there's something important. Do the following:

apt update

And then follow the instructions on screen (if this is your first time using terminal in android, you'll be surprise).

After doing that, install clang (you'll be prompted to install any other prerequisites), and again, follow instructions on screen.

apt install clang

Now, you are ready for the test. By the way, in case you don't realize, your $HOME directory is actually termux app's data directory, usually something like: /data/data/com.termux/file/home

You can create a new file for the test here, use ViM, if it's not already present, then install it (or you can use nano if you want). Write a usual, classic HelloWorld with that, either C or C++. Just in case you're not familiar with C/C++:

      #include <stdio.h>
      int main(int argc, char** argv) {
          printf("Hello World\n");
          return 0;
      }
    

Compile the code as you usually do in your desktop computer:

      clang -o test1 test1.c      

And run it, again, as usual:

      ./test1       

If there's no typo, the hello world should be running by now.

But How Can This Happen?

First, what you get is not a regular executable, it's actually a shared library. In order to be run by the Dalvik VM, who control and execute native programs, the executable have to be loaded as a shared library, who have special entry the system recognize. This is also what happen if you use NDK provided by android as part of development kit.

And that's not the only nice thing you can do from inside termux, you can also do scripting (a real scripting) with the popular Node.js, Python, or Perl, and I'm quite sure other scripting engines are also available.

There's also another good news, you don't have to use that small device all the time to be able write programs for it, you can also use your PC, either laptop or desktop computer. You can install Android-x86 for this purpose.

We'll discuss Android-x86 in another page, for now, just enjoy playing and getting familiar to termux.

Clone this wiki locally