Here is How You Can Write and Run C, Java & Python Programs on Your Android Devices

The smartphone you carry in the pocket with you is not just a small gadget that you can use to send snaps to your friends or fling birds at pigs. In fact, it is a powerful piece of technology that can be used to do almost anything you would normally use a full-fledged PC for — albeit a little smaller and with a few caveats.

You can even write, run and compile code on your little computer and you don’t need to install any IDE or even root your phone for it. Since Android is based on the Linux kernel, it also has a shell like the one you would find on Linux — although there is no way to access it out-of-the-box.


ALSO READ

These are the Trends for Developers and Programming Languages in 2018


Fortunately, you can install a Linux terminal emulator and get access to tons of Linux packages that can be used to do a lot more with your phone than you normally would have access to. Here is a list of things you can do once you, with Linux’s terminal in your pocket,

Here is a short example of how you can write, compile, and run C, Java, and Python programs on Android,

1. Download Termux

Termux is a powerful terminal emulator that comes with an extensive list of over a hundred Linux packages. The first thing you need to do is head over to Google Play and download Termux.

2. Set up Storage

You need to give Termux access to set up storage. Just open Termux and write the following piece of code — It will create a folder for Termux on your Android device.

termux-setup-storage

The default path for Termux is “/data/data/com.termux/files/home”. One caveat of using this emulator is that Android doesn’t give write-access to it for anywhere outside its home directory — the only way to get around this is to root your phone.

However, you do have read access so you can simply copy any file you want to work with to your home directory and then do the rest from there.

3. Writing Code using an in-terminal Text Editor

There are a number of ways you can write code on Android.

1. Write code using a normal editor or transfer code from your PC to your phone and then transfer it to Termux’s home directory using the following command,

cp old-path/filename new-path/

//EXAMPLE
cp /data/data/com.termux/files/home/storage/downloads/code.c /data/data/com.termux/files/home

This command will copy a file named “code.c” from the downloads folder and place it in Termux’s home directory. You can change the path depending on where the file is.

2. You can also write inside the Termux terminal using an in-terminal text editor. You can install Micro, which is a lightweight easy-to-use code editor. You can install it using the following command,

pkg install micro

Once it is installed, you can open it by simply typing “micro” in the terminal. You can also use it to open and edit an existing file by typing “micro filename” (e.g micro hello.c).

Once inside the editor, the navigation is done using CTRL+keyboard keys (the volume down button of your phone acts as CTRL when inside the terminal). Here are a few useful keyboard shortcuts for Micro,

  • CTRL+Q – Exit Micro
  • CTRL+O – Open a file
  • CTRL+G – Help Menu
  • CTRL+S – Save File

4. Install the Compilers for C, Python, and Java

For C language, run the following code,

pkg install clang

For Python, run the following code,

pkg install python

For Java, run the following code,

pkg install ecj dx

5. Compile and Execute Code

For C language, run the following code,

gcc -o <ExecutableFileName> <CurrentFileName.c>
./<ExecutableFileName>

//EXAMPLE
gcc -o code code.c
./code

For Python, run the following code,

python <FileName.py>

//EXAMPLE
python code.py

For Java, run the following code,

ecj <filename.java>
dx --dex --output=<filename.dex> <filename.class>
dalvikvm -cp <filename.dex> <filename>

//EXAMPLE
ecj Hello.java
dx --dex --output=Hello.dex Hello.class
dalvikvm -cp Hello.dex Hello

Now you’re ready to get started. Happy coding!



  • Get Alerts

    Follow ProPakistani to get latest news and updates.


    ProPakistani Community

    Join the groups below to get latest news and updates.



    >