📜 ⬆️ ⬇️

We write and collect Android applications in the linux console

image


In this article I will show how you can build an apk file in Ubuntu using only
command line utilities.


Usually, Android Studio is used to create applications for Adroid. But to build small programs, you can get away with the command line. For example, when computer resources are limited and your application is very simple.


As a permanent development environment, this may not be very convenient, but if you need to
sometimes collecting some small utilities is just right.


Introduction


Development for Android is not the main focus of my work, I sometimes make some small applications for my needs.


I used to use QPython, but it is rather heavy and inconvenient to use. Therefore, I turned to the development of native programs. Even with superficial knowledge of Java
This is not a big deal.


This guide is largely based on this document: Building an Android App
from the Command Line . Who are interested in the details, refer to the source.


A similar article: Writing, compiling and running HelloWorld for Android in a notebook has already been encountered on this resource, but it reviewed the development in Windows.


Here I will look at how to build an application in linux.


Iron


Testing was conducted on an old netbook with an Atom processor, 1GB of RAM
and 8GB SSD disk.


operating system


I tested the application on Ubuntu 17.04. Starting from Ubunu 16.04, you can install android-sdk through a package manager.


Manually Downloading the SDK

Basically, the same SDK can
download from the site .
Download file from the 'Get just the command line tools' section
In fact, this does not greatly change the process, but through the package manager everything is much simpler.
The difference will be only in the ways and installation of additional packages "android-platform".


Package installation


So, let's proceed to the installation.


sudo apt install android-sdk 

A large number of packages will be installed, including Java.


Further, depending on the required version of Android, you need to install the necessary
version of the packages. For lolipop 5.1, you must install:


 sudo apt install google-android-platform-22-installer sudo apt install google-android-build-tools-22-installer 

It is also necessary to install an additional package.


 sudo apt install apksigner 

If you plan to install apk-package via adb , then you need a few additional settings.


Adb setup


Use lsusb to find a connected device.


 # lsusb .... Bus 001 Device 004: ID 1782:75b0 MyDevice .... 

And create a file with the rule:


 sudo vi /etc/udev/rules.d/51-android.rules 

Add one line to the file:


 SUBSYSTEM=="usb", ATTR{idVendor}=="1782", MODE="0666", GROUP="plugdev" 

Here "1782" is taken from the lsusb output.


Restart service


 sudo systemctl restart udev 

After connecting via adb , the device must confirm the connection.


Now everything is ready for work.


Formulation of the problem


An application that we will build is a bit more complicated than 'Hello world'.



In general, everything is simple.


I have prepared an example which we take as a basis.


Create a signature


First, create a key to sign the file:


 keytool -genkeypair -keystore keystore.jks -alias androidkey \ -validity 10000 -keyalg RSA -keysize 2048 \ -storepass android -keypass android 

This will come in handy later.


Manifesto


AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ru.kx13.extractvidid" versionCode="1" versionName="0.1"> <uses-sdk android:minSdkVersion="22"/> <application android:label="EctractId" android:icon="@drawable/icon" > <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest> 

Here we indicate the name of the application in the "android: label" attribute. Also, the application will use its icon, it is listed in the "android: icon" attribute. The icon itself is in the "res / drawable-mdpi" directory file "icon.png". As an icon, you can take any small png file.


Layout


The file with the location of elements is in the directory "/ res / layout /".


activity_main.xml
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/my_text"/> <Button android:id="@+id/button_id" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="" /> </LinearLayout> 

You can add widgets to it if you want to expand the functionality.


Application source code


The source code of the application is here "java / ru / kx13 / extractvidid"


MainActivity.java
 package ru.kx13.extractvidid; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import android.widget.Button; import android.widget.Toast; import android.view.View; import android.content.ClipboardManager; import android.content.ClipData; public class MainActivity extends Activity { private static String extract(String s) { int start = s.indexOf("%3D"); int end = s.indexOf("%26"); if(start == -1 || end == -1) { return "error"; } return s.substring(start + 3, end); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView text = (TextView)findViewById(R.id.my_text); text.setText(" youtube video id"); Button button = (Button) findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ClipboardManager myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData abc = myClipboard.getPrimaryClip(); ClipData.Item item = abc.getItemAt(0); String text = item.getText().toString(); String video_id = MainActivity.extract(text); ClipData myClip = ClipData.newPlainText("text", video_id); myClipboard.setPrimaryClip(myClip); Toast toast = Toast.makeText(getApplicationContext(), video_id, Toast.LENGTH_SHORT); toast.show(); } }); } } 

The code is very simple and primitive, but this template can be used in other applications.


Build script


I did not use build tools like make or ant , since All code is in one file and it will not give special advantages. Therefore, this is a regular shell script:


build.sh
 #!/bin/sh SOURCE=ru/kx13/extractvidid BASE=/usr/lib SDK="${BASE}/android-sdk" BUILD_TOOLS="${SDK}/build-tools/22.0.1" PLATFORM="${SDK}/platforms/android-22" mkdir -p build/gen build/obj build/apk "${BUILD_TOOLS}/aapt" package -f -m -J build/gen/ -S res -M AndroidManifest.xml -I "${PLATFORM}/android.jar" javac -source 1.7 -target 1.7 -bootclasspath "${JAVA_HOME}/jre/lib/rt.jar" \ -classpath "${PLATFORM}/android.jar" -d build/obj \ build/gen/${SOURCE}/R.java java/${SOURCE}/MainActivity.java "${BUILD_TOOLS}/dx" --dex --output=build/apk/classes.dex build/obj/ "${BUILD_TOOLS}/aapt" package -f -M AndroidManifest.xml -S res/ -I "${PLATFORM}/android.jar" \ -F build/Extractor.unsigned.apk build/apk/ "${BUILD_TOOLS}/zipalign" -f 4 build/Extractor.unsigned.apk build/Extractor.aligned.apk apksigner sign --ks keystore.jks \ --ks-key-alias androidkey --ks-pass pass:android \ --key-pass pass:android --out build/Extractor.apk \ build/Extractor.aligned.apk 

Some comments on the paths.



Assembly and installation


To build, simply run


 ./build.sh 

If everything is configured correctly, no messages will be displayed, and the file "Extractor.apk" will appear in the "build" directory


Now we need to install our application.


 adb install -r build/Extractor.apk 

If everything went well, a new application will appear on the device. You can run and use.


In general, you can transfer the apk file to the device in any convenient way.


Conclusion


As you can see from the article to start development in the console is a snap.


Console utilities allow you to develop programs with very small resources.


Pleasant development!


')

Source: https://habr.com/ru/post/335074/


All Articles