[LINUX] I want to develop Android apps on Android

Introduction

I don't know how many people want to develop Android apps on Android, but this article aims to build an environment for self-development on Android. Self-development refers to developing an application for your device on your device. Windows, Mac, Linux, etc. can be self-developed, but at least Android has no official development tools, so I wanted to try it, but I thought of it, but I built it just as good as it was. (Although I'm using the official Linux version ...) Since it has just been built, there may be some deficiencies in the actual development, but please forgive me for that. If I find something, I would like to investigate and write a solution. In this article, we'll build Android Studio to the point where it does the bare minimum. → Described up to the point of launching Debugger.

It's better to have a stylus pen when using Android Studio, and it's better than not having a 100-yen shop. Since the menu of Android Studio is thin, it is hard to touch with the little finger, let alone the thumb. Please do not buy one for Nintendo 3DS with a plastic tip. The touch panel did not respond ...

Preparation (Ubuntu desktop environment)

Prepare the Ubuntu desktop environment with UserLAnd or Termux in advance. If you haven't built it, we will describe the procedure for building the minimum desktop environment later, so please refer to it if you like. Probably Android Studio runs faster in Termux, but the steps are tedious. If you have a fast device, UserLAnd may be a good idea.

I don't recommend Debian because it's a JDK version issue and Android Studio doesn't work, but anyone who can manage to get openjdk 8 may be able to get it working.

download

Download attp2 for Android Studio, SDK tools, Arm64. The download is written on the assumption that it will be operated on Windows, but I think the same thing can be done on Android.

Download Android Studio, SDK tools

Go to Android Studio downloads to download the Linux (64-bit) version of the Android Studio package and the Linux version of the SDK tools package. ..

At my time I had android-studio-ide-191.6010548-linux.tar.gz and sdk-tools-linux-4333796.zip.

Later, there is a place where the command is executed by specifying this file name, but if the file name has changed, replace it with your own downloaded file name and execute the command.

The Android SDK Tools (https://developer.android.com/studio/releases/sdk-tools?hl=ja) will include a set of tools for development and debugging. Android Studio will work in conjunction with various external tools, not just by itself. Later, I will download another command line tool etc. with a build tool called Gradle.

Download arm64 version attp2

aapt2 is a tool for generating APK files (package files of applications used for installation on Android). The Arm64 CPU version has not been officially released, but I am grateful that some people have released it on Github.

https://github.com/thejunkjon/android-tools/tree/master/build/android-9.0.0_r33/aapt2/arm64-v8a/bin

Go to the above site and press the Download button to download the aapt2 file.

Installation

It will be operated with some suitable file management tool of Android. I'm using an Android app called X-plore, but I'm happy with anything that can work with files. Place the Android Studio, SDK tools you downloaded earlier in a location visible to UserLAnd or Termux Ubuntu.

For UserLAnd

See the correspondence table below and install it in the desired position.

Ubuntu Android
/storage/internal/ (Main body side)/sdcard/Android/data/tech.ula/files/storage/
/storage/sdcard/ (SD Card side)/storage/(SD Card name)/Android/data/tech.ula/files/storage/

For Termux

See the correspondence table below and install it in the desired position.

Ubuntu Android
/sdcard/ (Main body side)/sdcard/
/storage/(SD Card name)/ (SD Card side)/storage/(SD Card name)/

JDK installation

It will be an operation on Ubuntu. It is not necessary to launch the desktop environment yet, so if it is easier to launch only the shell, please do so.

Install openjdk 8. Note that Termux is a root account, so you don't need the first sudo.


#Install openjdk 8
sudo apt install -y openjdk-8-jdk

#Check if openjdk 8 is installed
ls /usr/lib/jvm/java-8-openjdk-arm64

#If you don't have the text editor vim installed, install it
sudo apt install -y vim

#Edit configuration file
sudo vim /etc/enviroment

Save the following in the environment file.


JAVA_HOME="/usr/lib/jvm/java-8-openjdk-arm64"

If you don't know how to use vim, just remember the following.

Keyboard keys role
i Enter input mode from command mode
ESC Exit input mode and return to command mode
:wq Save and exit. Operate in command mode.

After saving, reflect the environment setting file in your environment.


source /etc/enviroment

Android Studio does not work with the Arm64 version of openjdk 11. If anyone has 11 in it, switch to 8 as described in the bonus section below.

Environment settings


# .Describe the setting of aapt2 in bashrc
cd
vim .bashrc

Even at the bottom of .bashrc, write the following and save it.

export ANDROID_DATA=/data

Only this time, manually execute the settings described. From the next time, it will be executed automatically, so the following operations will not be necessary.


source .bashrc

Install Android SDK tools

Next, install the Android SDK tools.


#For UserLAnd
mkdir -p /home/(My ID)/Android/Sdk
cd (sdk-tools-linux-4333796.Directory with zip)
cp sdk-tools-linux-4333796.zip /home/(My ID)/Android/Sdk
cd /home/(My ID)/Android/Sdk/
unzip sdk-tools-linux-4333796.zip

#For Termux
mkdir -p /root/Android/Sdk
cd (sdk-tools-linux-4333796.Directory with zip)
cp sdk-tools-linux-4333796.zip /root/Android/Sdk
cd /root/Android/Sdk
unzip sdk-tools-linux-4333796.zip

In the case of UserLAnd, the directory changes for each ID, but the ID will be the ID registered when Ubuntu is first selected. Probably it is written on the left side of the command prompt, so use that value.

Install Android Studio

It's finally time to install Android Studio.


# UserLAnd,Termux common

cd (android-studio-ide-191.6010548-linux.tar.Directory with gz)
cp android-studio-ide-191.6010548-linux.tar.gz /usr/local/
cd /usr/local/
tar -xvzf android-studio-ide-191.6010548-linux.tar.gz
cd /usr/local/android-studio/bin/
sudo chmod +x studio.sh

That's all for installing Android Studio itself, but you'll need to manually create and install some of the external tools later.

Android Studio settings

From here, the operation will be in the Ubuntu desktop environment.

Please note that if you are using XSDL, please set the font scale to a small value (I set it to 0.5). If you set it large, the Android Studio dialog will be larger than the screen, and the buttons will extend off the screen and cannot be pressed. Also, even if the resolution is low, the dialog will extend off the screen, so it seems that a certain size is required. In my case, it's 1280x720.

Run studio.sh from the command line to launch Android Studio.

Launch LXTerminal (bird mark at the bottom left of the screen → System Tools → LXTerminal) and execute the following command.


# UserLAnd,Termux common

/usr/local/android-studio/bin/studio.sh

A dialog for importing the setting file will appear. If there is a setting file, specify it and click the OK button. If not, just press the OK button.

After the splash window with android studio (lowercase for some reason) appears, I get a dialog asking me to send some data to Google. Please choose your favorite button.

The Welcome dialog will appear, so click the Next button.

The Install Type dialog will appear, but I've only chosen Standard, so I'll assume that I've chosen Satndard. Press the Next button.

The Select UI Theme dialog will appear. Select the design you like and press the Next button.

The Veryfy Settings dialog will appear, so press the Next button. If you get an error message here, the SDK Tools installation destination is incorrect, so press the Cancel button once to exit Android Studio, correct the installation destination, and then start Android Studio again. ..

The Emulator Settings dialog will appear, but there is nothing special to do, so press the Finish button.

The Downloading Components dialog will appear to download the components. When the download is complete, press the Finish button. In my environment it took about 10 minutes to download.

The Android Studio dialog will appear. Click Start a new Android Studio project.

The Create New Project dialog will appear. Select the appropriate panel and click the Next button.

The Configure your project dialog will appear, so set the API level according to the Android version of your smartphone (the menu on the Android setting screen is different for each smartphone, so please investigate how to check the Android version yourself). For others, set an appropriate value and press the Finish button. It's a little hardened here, but don't panic, let's wait. After a while, the IDE should start up.

When you see the word 2 processes running ... in the lower right corner, it will automatically download a build tool called gradle and build it.

When gradle stops working, select File → Project Structure from the menu to open the Project Structure dialog. Set JDK locations in SDK Location to JAVA_HOME and press the OK button.

The build will start again, so wait patiently for an error. If the build does not start, select the Android Stdio menu Build → Rebuild Project.

Here the error occurs in the external tool used by gradle. You should see Gradle Sync at the bottom of Android Studio and a bar to the right of it. Please wait until the bar stops moving. It took more than 10 minutes when I went with UserLAnd, depending on the environment.

Set gradle to work offline. Select File → Settings from the menu to open the Settings dialog. Select Build, Execution, Deployment → Gradle, check Offline work in Global Gradle settings, and click the OK button. I wondered if this process could prevent the jar file described below from being replaced, but after a little research I couldn't tell if the replacement would occur. .. .. ← Apparently, it doesn't seem to be replaced even if it stays online. But offline is usually faster, so let's go offline. Occasionally (such as when changing the environment from LXDE to Xfce), an error message may appear asking you to cancel offline at build time, so at that time you should cancel it once, build, and then go offline again.

Creating a jar file for aapt2

I got an error earlier, but the cause is a tool called appt2. The official version of this tool is for Intel CPUs, so it won't work on smartphones with Arm64 CPUs. Replace with the Arm64 CPU version of aapt2.

Since aapt2 is included in the jar file, first look for the jar file.

Select the bird mark at the bottom left of the screen → System Tools → File Manager PCManFM (hereinafter referred to as PCMan). When PCMan starts up, check the menu View → Show Hidden. Please open the following folder.

・ For UserLAnd

/home/(user ID)/.gradle/caches/modules-2/files-2.1/com.android.tools.build/aapt2

・ For Termux

/root/.gradle/caches/modules-2/files-2.1/com.android.tools.build/aapt2

Below this you should find aapt2-x.x.x-xxxxxx-linux.jar (where x is the version number), so look for it. Those with proto can be ignored this time.

Copy aapt2-x.x.x-xxxxxx-linux.jar somewhere. Rewrite the file name extension from jar to zip and unzip it. It should contain aapt2, so replace it with the first downloaded Arm64 CPU version of aapt2. Compress the META-INF folder, aapt2, and NOTICE into aapt2-x.x.x-xxxxxx-linux.zip. Rewrite the filename extension from zip to jar. Overwrites the jar file with the location of the first jar file you find.

Build again in Android Studio

Go back to Android Studio and select Build → Rebuild project from the menu and check that the rebuild does not cause any errors.

Then select Build → Build Bundle (s) / APK (s) → Build APK (s) from the menu to create an APK file (package for installing Android applications). The information Build APK (s) will appear in the lower right corner, and the word locate will appear in the string. Click on that string. It is successful if the folder is opened and a file with the extension apk is generated in it.

If you want to try it, pass the APK file to the Android side and try the installation. If you are told that the installation cannot analyze, the Android version of your smartphone may be older than the version specified in the APK file, so create a new project and create a new project for your smartphone. Please match the SDK (Minimum API level setting in the project generation dialog) that matches the Android version.

This is the end of building the development environment. It may be a good idea to develop lightly (?) Even in your spare time.

bonus

Solution for those who have installed openjdk 11

First, as mentioned above, install openjdk 8.

Then execute the following command and java-8-openjdk-arm64 will appear in the options. Select by number.


sudo update-alternatives --config java

Finally, as mentioned above, enter / etc / environment and set JAVA_HOME etc.

Put Android Studio in your desktop menu

After launching Android Studio, you can enter it in the desktop menu by selecting Tools → Create Desktop Entry ... from the menu. From the next time, click the bird mark on the lower left for LXDE, or click the mark on the upper left for XFCE to bring up the menu and select Programming> Android Studio to start it.

Reference site

How to introduce Java to Ubuntu 18.04 (explains the difference between JRE and JDK)

Recommended Posts

I want to develop Android apps on Android
I want to develop an Android application on Android (debugging)
I want to use Linux on mac
I want to log file I / O on Linux
I want to find a popular package on PyPi
I want to solve Sudoku (Sudoku)
I want to AWS Lambda with Python on Mac!
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
I want to restart CentOS 8 on time every day.
I want to do Wake On LAN fully automatically
I want to announce my graduation thesis on IPython Notebook
I want to scrape images to learn
I want to do ○○ with Pandas
I want to copy yolo annotations
I want to use the Ubuntu desktop environment on Android for the time being (Termux version)
I want to use Ubuntu's desktop environment on Android for the time being (UserLAnd version)
I want to use shortcut translation like DeepL app on Linux
I just wrote a script to build Android on another machine
I want to pin Spyder to the taskbar
I want to detect objects with OpenCV
I want to output to the console coolly
I want to print in a comprehension
I want to scrape them all together.
I want to handle the rhyme part1
I want to know how LINUX works!
I want to blog with Jupyter Notebook
I want to handle the rhyme part3
I want to use jar from python
I want to build a Python environment
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I was addicted to Flask on dotCloud
I want to play with aws with python
I want to use IPython Qt Console
I want to display the progress bar
I want to make an automation program!
I want to embed Matplotlib in PySimpleGUI
I want to handle the rhyme part2
I want CAPTCHA to say HIWAI words
I want to handle the rhyme part5
I want to handle the rhyme part4
I want to tweet on Twitter with Python, but I'm addicted to it
I want to display an image on Jupyter Notebook using OpenCV (mac)
I want to format and check Python code to my liking on VS Code
I want to use Ubuntu's desktop environment on Android for the time being (Termux version-Japanese input in desktop environment)
I want to make matplotlib a dark theme
I want to connect to PostgreSQL from various languages
I want to do Dunnett's test in Python
I want to have recursion come to my mind
I want to easily create a Noise Model
I want to use MATLAB feval with python
I want to analyze songs with Spotify API 2
I want to INSERT a DataFrame into MSSQL
I want to use Python in the environment of pyenv + pipenv on Windows 10
I want to create a window in Python
Anyway, I want to check JSON data easily
I want to email from Gmail using Python.
How to use Python Kivy ④ ~ Execution on Android ~
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I want to perform SageMaker inference from PHP