How to code on Android?

Introduction

If you're interested in knowing how to program in C, Java directly on your phone, this is the article for you.

Indeed, many people don't know that they can program on their phone. However, like someone said, your Android device is like a computer. So, you can totally code on your phone!

Read on for a list of the different languages you can code with and a list of the tools used to code on your phone!

Termux

Some time ago, while I was still in college, I had an assignment to do in C, but my computer was broken! So, that is why I turned to my phone. This is how I discovered how to program in C on my phone.

So, Android is based on Linux, which means that all the commands that work on Linux work on Android too! But, for that, you need access to a terminal.

What's the app for that? Termux!


A screen capture of Termux.

Screenshot of Termux


Termux is an Android app created by Fredrik Fornwall. It is a "free and open-source terminal emulator for Android which allows for running a Linux environment on an Android device".

How to use Termux

Termux works just as a Linux environment. If you don't have any experience with that, check out my article about the top Linux commands here.

Tip: On Termux, the package installer is pkg. Just type pkg install [command].

How to edit the code?

You can use a command line code editor, directly within Termux itself, or you can choose to resort to an external code editor.

Using command line editors

There are three main command line text editors:

- Vim
- Nano
- Emacs

I often use nano for its simplicity. The only keyboard shortcuts you need to know are this:
ctrl+o then enter to save the file.
ctrl+x to exit.

I've actually never used Vim.

I've used Emacs in the past. It is full of keyboard shortcuts that you need to know. Here is a list [under construction].

Alternative solution: use an external code editor.

You can navigate with Termux to a location outside of the Termux home directory, so that you can access your files with the external code editor. You can then use the same methods listed below to compile your code.

[Under construction]

What keyboard to use?

When writing code, there are certain key you want to use that are not necessarily on your keyboard. Termux does provide a top bar with useful buttons such as Ctrl, Alt, and Up Arrow and Down Arrow. However it is missing the Right Arrow and Left Arrow which allow for optimal code editing. So, here's a list of programming keyboard:

How to compile a C program using Termux on Android

It's very easy. Just like in Linux, you can use gcc to compile c programs.

gcc file.c -o exec

or

gcc -c file.c
gcc file.o -o exec

You can also use gdb, which is the debugger. For how to use gdb see [under construction].

How to compile a Java program using Termux on Android

You need to have the JDK installed which you can do so using pkg. Just type javac, and it will automatically tell you which package you should be installing.

Java Code:

public class HelloWorld {
public static void main(String args[]) {
        System.out.println("Hello World :)";
   }
}

Compiling is done using javac:

javac HelloWorld.java

And then you run the code like so:

java HelloWorld


And many more...

You can technically use any language as you would like.

For example, python will also work.


Using an IDE

There are some Android apps which allow you to directly code inside of them, without using Termux.

[Under construction]


Android Programming on Android: Best Apps to Program on Android

Android logo

Do you want to code anywhere, in the subway or the bus? There are apps that allow you to code Android applications directly on your phone or tablet! Here are three apps that do just that:

- AIDE

Download AIDE

Review: AIDE is an IDE meant for developing apps directly on your Android device.
AIDE contains two modes: learning and coding. The learning mode is almost all paid. AIDE offers a subscription model. You need to pay to access advanced android programming courses. Among the paid courses, you can find a course on how to program games on Android with libgdx, and a course on the Android NDK, which allows you to program apps in C or C++.
The developer mode is accessed from the For Experts pannel. To create a new project, a popup appears after the For Experts button is pressed, but it is a little unreliable and there is no way to create a project from anywhere else. The file explorer is at the bottom half of the screen and is less practical.
You include the header files by clicking on the class in the auto-complete menu while typing.
AIDE supports external dependenciesby adding them from the Add To Project button that gives you a list of dependencies. However, when trying to make the FAB (FLoating Action Button) work, I ran accross strange errors. This appears to be a bug in recent versions of AIDE.
Also, AIDE appears at times slower than Code Assist. The code doesn't wrap so you have to scroll. Also, the app has a dark theme but it is not as pretty in my opinion.
AIDE stores the files in an external directory of your choosing so you can copy the files easily. Also AIDE provides options to sign your app by creating a keystore.

- Java N-IDE

Download Java N-IDE

Review: This app was apparently discontinued from the Play Store and I don't know why. I used it and loved it. I made one of my first published apps with this app. The only problem was that the AppBar was in an old style and it was impossible to change. Also, the gradle files were impossible to change, which means that things like the FAB button won't work. Also, the includes aren't done automatically. Despite that, it was a pleasant experience developing on this app. It is in dark mode and the UI is pleasant, with a file browser on the side. One positive about this app is that the text wraps to fit the screen size.

- Code Assist Android IDE

Download Code Assist

Review: This app is in early access mode / alpha. It looks dope though.
The UI is very nice, with a practical side pannel for the file explorer like Java N-IDE. The app supports adding dependencies through its library manager. To use it, you may find it easier to add the dependency in the app build.gradle file, and then click "add librariries from build.gradle". This means that the FAB (Floating Action Button) WILL work! This was the easiest to setup the FAB as compared with AIDE where strange errors appeared. To learn how to include the FAB in Code Assist, please refer to the following link: [under construction]
A slightly annoying feature is all the noisy notifications during build time. Another thing is that the copy and paste menu is filed under a text action menu, which is less efficient.
The auto complete is one of the best I've seen, and unlike AIDE works good in the XML as well. You include the header files by clicking on the class in the auto-complete menu while typing.
Code Assist also has a dark theme, although the colors for the code are slightly ugly in my opinion (weird shades of orange).
There is an option to wrap the code to fit the screen and an option for the text size.
At first I thought there was no way of exporting your project, however I realised that when creating the project you can submit a path for the location of the project< However, starting Android 11, you can only save to the Code Assist Android/data folder, which is publically accessible. However, when trying to access that folder on my phone, a message said it is only accessible on computer. That's unfortunate if your computer is broken. You can then access the apk in the file directory under bin or output in older versions.
I am unsure about how app signing is managed.