⬆️ ⬇️

AStA: we collect APK on the device itself

What is AStA? This is an acronym for Android Studio on Android. This is a method to build Android Studio projects on an Android device using chroot / Debian, JDK, Android SDK, and Gradle.



Why is it even needed? But you never know, why ... Sometimes, for example, I want to check out some idea, but there is no desktop at hand. In general, let the question "why" everyone will answer for himself.



What are the alternatives? Of the existing solutions, I know only AIDE , but it has its drawbacks. First, the window with a proposal to upgrade the version for 600 rubles constantly pops up, and if this is not done, then it is impossible to save projects consisting of more than 5 files. Secondly, AIDE does not support building Android Studio projects consisting of more than one module.



What do we need?



1. Device with Android kernel compatible with Debian Stretch.

2. The presence of root-rights on the device.

3. 4 GB of free space on a memory card for a Debian image.

4. Pre-installed on the Busybox device.

5. Desktop with Debian on board + adb (if installed via a script).

')

Installation via script



The following script creates a Debian image using debootstrap, puts it, as well as all the other necessary files, on the device's memory card, and then runs the Debian configuration script on the device itself:



#!/bin/sh #       : sudo echo 'installDebianOnAndroid script started' #   ARCH=armhf #  Debian DEBIAN_VERSION=stretch #      : STORAGE=/sdcard # ,      : DATA_DRIVE=/Data #      FAT32 #       (2^32 - 1)  BS=1M COUNT=4095 #   : dd if=/dev/zero of=$DATA_DRIVE/debianOnAndroid.img bs=$BS count=$COUNT #     : mkdir -p ~/debianOnAndroid #     : sudo mkfs.ext3 $DATA_DRIVE/debianOnAndroid.img #  : sudo mount -o user,loop,exec,dev $DATA_DRIVE/debianOnAndroid.img ~/debianOnAndroid/ #   debootstrap    debian: sudo debootstrap --verbose --arch $ARCH --foreign $DEBIAN_VERSION ~/debianOnAndroid/ http://ftp.se.debian.org/debian #  : sudo umount $DATA_DRIVE/debianOnAndroid.img #     : adb push $DATA_DRIVE/debianOnAndroid.img $STORAGE # Busybox     : # https://play.google.com/store/apps/details?id=stericson.busybox #adb push busybox/busybox-armv6l /sdcard/busybox #adb shell su -c cp /sdcard/busybox /data/local/busybox #adb shell su -c chmod 755 /data/local/busybox #   AStA  Android: adb shell mkdir -p $STORAGE/AStA adb shell mkdir -p $STORAGE/AStA/Projects adb shell mkdir -p $STORAGE/AStA/archives adb shell mkdir -p $STORAGE/AStA/scripts #       Debian adb push firstMountAndConfigureDebian.sh $STORAGE/AStA/scripts #      adb push mountDebian.sh $STORAGE/AStA/scripts adb push umountDebian.sh $STORAGE/AStA/scripts #    JDK, Android SDK  gradle ANDROID_SDK_TAR_GZ=android-sdk-linux.tar.gz JDK_8_TAR_GZ=jdk-8u144-linux-arm32-vfp-hflt.tar.gz GRADLE_ZIP=gradle-3.5-bin.zip #     AStA/archives   : adb push archives/$ANDROID_SDK_TAR_GZ $STORAGE/AStA/archives adb push archives/$JDK_8_TAR_GZ $STORAGE/AStA/archives adb push archives/$GRADLE_ZIP $STORAGE/AStA/archives #     Gradle: adb push gradleExec.sh $STORAGE/AStA/scripts #     Debian: adb shell su -c sh $STORAGE/AStA/scripts/firstMountAndConfigureDebian.sh #  : adb shell su -c sh $STORAGE/AStA/scripts/umountDebian.sh #   : ASTA_APP_APK=AStA-app.apk #     AStA  : adb push apk/$ASTA_APP_APK $STORAGE/AStA echo 'installing AStA-app' adb shell pm install -r $STORAGE/AStA/$ASTA_APP_APK echo 'installDebianOnAndroid script done' 


Debian configuration script on device:



 echo 'firstMountAndConfigureDebian script started' #     SSH: PASSWORD=1234567 #   Debian: MNTPT=/data/local/debianOnAndroid #    : STORAGE=/sdcard #  Debian: IMG_FILE=$STORAGE/debianOnAndroid.img #  Debian: DEBIAN_VERSION=stretch #   : mkdir -p $MNTPT #  : busybox mount -o loop $IMG_FILE $MNTPT #     chroot: export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/system/bin:/system/xbin:/su/bin:/su/xbin #   debootstrap: chroot $MNTPT /debootstrap/debootstrap --second-stage #  apt: echo "deb http://ftp.se.debian.org/debian $DEBIAN_VERSION main contrib non-free" > $MNTPT/etc/apt/sources.list # : busybox mount -t proc none $MNTPT/proc busybox mount -t sysfs none $MNTPT/sys busybox mount -o bind /dev $MNTPT/dev busybox mount -t devpts none $MNTPT/dev/pts export TMPDIR=/tmp #chroot $MNTPT /bin/bash #  apt    ,      Internet (3003) chroot $MNTPT sed -i 's#_apt:x:104:65534::/nonexistent:/bin/false#_apt:x:104:3003::/nonexistent:/bin/false#' /etc/passwd #  ,   : chroot $MNTPT apt-get update chroot $MNTPT apt-get --yes upgrade chroot $MNTPT apt-get --yes install ne openssh-server unzip \ android-sdk-build-tools android-sdk-platform-tools #      SSH: echo "echo 'root:$PASSWORD' | chpasswd" > $MNTPT/root/setpasswd.sh chroot $MNTPT /bin/sh /root/setpasswd.sh cat $MNTPT/root/setpasswd.sh #     SSH,     root': chroot $MNTPT sed -i '/PermitRootLogin without-password/c\PermitRootLogin yes' /etc/ssh/sshd_config chroot $MNTPT /etc/init.d/ssh restart #   AStA  Debian      Android: mkdir -p $MNTPT/AStA busybox mount -o bind $STORAGE/AStA $MNTPT/AStA #   : ANDROID_SDK_TAR_GZ=android-sdk-linux.tar.gz JDK_8_TAR_GZ=jdk-8u144-linux-arm32-vfp-hflt.tar.gz GRADLE_ZIP=gradle-3.5-bin.zip #  : chroot $MNTPT tar -xzvf /AStA/archives/$ANDROID_SDK_TAR_GZ -C /opt chroot $MNTPT tar -xzvf /AStA/archives/$JDK_8_TAR_GZ -C /opt chroot $MNTPT unzip /AStA/archives/$GRADLE_ZIP -d /opt # echo 'firstMountAndConfigureDebian script done' 


Running the installation through a script



To install a Debian image, application, and all the necessary scripts on a device, you need to perform the following sequence of actions:



 git clone https://github.com/tabatsky/AStA.git cd AStA/scripts #     start.sh sh start.sh 


You can also download a ready-made image of Debian (under armhf) here , or download via the application .



Enable and Disable Debian



The following script mounts Debian and starts ssh, and also mounts the AStA folder from the device to Debian:



 MNTPT=/data/local/debianOnAndroid STORAGE=/sdcard IMG_FILE=$STORAGE/debianOnAndroid.img mkdir -p $MNTPT busybox mount -o loop $IMG_FILE $MNTPT export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/system/bin:/system/xbin:/su/bin:/su/xbin busybox mount -t proc none $MNTPT/proc busybox mount -t sysfs none $MNTPT/sys busybox mount -o bind /dev $MNTPT/dev busybox mount -t devpts none $MNTPT/dev/pts export TMPDIR=/tmp #chroot $MNTPT bash chroot $MNTPT /etc/init.d/ssh start mkdir -p $STORAGE/AStA mkdir -p $MNTPT/AStA busybox mount -o bind $STORAGE/AStA $MNTPT/AStA 


And this one, accordingly, stops ssh and unmounts everything:



 MNTPT=/data/local/debianOnAndroid export PATH=/sbin:/usr/sbin:/bin:/usr/bin:/system/bin:/system/xbin:/su/bin:/su/xbin busybox umount $MNTPT/AStA chroot $MNTPT /etc/init.d/ssh stop busybox umount $MNTPT/dev/pts busybox umount $MNTPT/dev busybox umount $MNTPT/proc busybox umount $MNTPT/sys busybox umount $MNTPT 


You can run these scripts through the shell:



 su -c sh /sdcard/AStA/scripts/mountDebian.sh su -c sh /sdcard/AStA/scripts/umountDebian.sh 


Or you can simply click on the corresponding button in the application.



Running Gradle Commands



You can start the Gradle build by connecting via ssh and running the following command:



  sh /AStA/scripts/gradleExec.sh /AStA/Projects/MyProject/myModule/ myCmd # : sh /AStA/scripts/gradleExec.sh /AStA/Projects/AStA-app/app/ assembleDebug 


Similarly, you can simply select the desired command, project and module and click on the button in the application.



Restrictions



1. The project 'com.android.tools.build:gradle' version should be indicated in the project build.gradle file: 2.2. *;

2. In the build.gradle files of all modules, buildToolsVersion should be specified == 24.0.0;

3. CompileSdkVersion <= 24 must be specified in the build.gradle files of all modules.



I have it all



All scripts, as well as the source code of the application , can be found here:

github.com/tabatsky/AStA



As a conclusion, I want to say that with this method I managed to successfully assemble the AStA-app application itself.

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



All Articles