Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install an app in system/app while developing from android studio

Is there a way to make an app install directly in the system/app folder while developing on Android Studio (the device is rooted)?

Meaning, when I press on the 'Run app' button, I want the apk to be placed in system/app.

If this is not possible, what is the recommended most convenient way to work on building and testing a system app?

like image 857
dors Avatar asked Feb 03 '15 15:02

dors


People also ask

How do I install a third party app as a system app?

Inside the “/“ aka the device folder, head to the “data” and then the “app” folder to access the all the installed apps. Here, you will find the data folders of all the third-party apps listed together. Now, long press on the folder of the app that you want to convert as system app and select the “cut” option.

How do I make an Android app a system app?

First create a folder for your app ( Let say MyTestApp) inside packages/apps/ of your android AOSP downloaded source code. Then create a Android.mk file inside the folder(MyTestApp). Step 2 open Android.mk file and add folowing code Snippet and save this mk file. PRODUCT_PACKAGES tag at the bottom MyTestApp.

Can I install apps on Android Studio?

In Android Studio, create an Android Virtual Device (AVD) that the emulator can use to install and run your app. In the toolbar, select your app from the run/debug configurations drop-down menu. From the target device drop-down menu, select the AVD that you want to run your app on. Click Run .

How do I run an Android app in Android Studio?

To build and run your app, select Run > Run in the menu bar (or click Run in the toolbar). If it's the first time running the app, Android Studio asks you to select a deployment target as shown in figure 1. Select a device to install and run your app.

How to install apps on Android devices?

Part of the benefits of having an Android device is being able to install an app on it. Installing is as simple as one, two, three; just search for your desired app on the Google Play Store and hit the Install button. Though installing apps is easy, they can be installed as either user apps or system apps.

How to run an app as a system process in Android?

If you are intent to run your app as a system process, then it is important to include android:sharedUserId attribute in the AndroidManifest.xml Once you add this attribute, please note that this application can only be installed as a system app. If you try to install this as a normal app, it will fail.

What is Android Studio in Android Studio?

Android Studio sets up new projects to deploy to the Android Emulator or a connected device with just a few clicks. Once your app is installed, you can use Apply Changes to deploy certain code and resource changes without building a new APK.


2 Answers

Deploy automatically system app from AS

You can create a script that will do the job, and run it automatically each time you hit run in AS.

1. Create the script

You can adapt this script that I've created from my needs. Place it in: project_directory/installSystem.sh

#!/bin/bash  # CHANGE THESE FOR YOUR APP app_package="com.example" dir_app_name="MySysApp" MAIN_ACTIVITY="SysAppMainActivity"  ADB="adb" # how you execute adb ADB_SH="$ADB shell" # this script assumes using `adb root`. for `adb su` see `Caveats`  path_sysapp="/system/priv-app" # assuming the app is priviledged apk_host="./app/build/outputs/apk/app-debug.apk" apk_name=$dir_app_name".apk" apk_target_dir="$path_sysapp/$dir_app_name" apk_target_sys="$apk_target_dir/$apk_name"  # Delete previous APK rm -f $apk_host  # Compile the APK: you can adapt this for production build, flavors, etc. ./gradlew assembleDebug || exit -1 # exit on failure  # Install APK: using adb root $ADB root 2> /dev/null $ADB remount # mount system $ADB push $apk_host $apk_target_sys  # Give permissions $ADB_SH "chmod 755 $apk_target_dir" $ADB_SH "chmod 644 $apk_target_sys"  #Unmount system $ADB_SH "mount -o remount,ro /"  # Stop the app $ADB shell "am force-stop $app_package"  # Re execute the app $ADB shell "am start -n \"$app_package/$app_package.$MAIN_ACTIVITY\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER" 

2. Bind it with AS Run

  1. Go to Run -> Edit Configurations
  2. Do the following changes on General tab (of your module)

    • Installation Options->Deplay: Nothing
    • Launch Options->Launch: Nothing
    • Before launch: press +, then Run External Tool, to select your script.
      • In the new dialog:
        • set any name.
        • On 'Tool Settings'->Program: navigate to the project's dir, and select your script

Caveats :

First installation

The device needs to be restarted (adb reboot) only once, on the very first installation of your app. Afterwards, you can simply press Run and everything will happen automatically.

This is because the host compiler (dex2oat) is not invoked automatically. Somehow the OS is not yet informed for this new system app. Calling dex2oat manually should solve this, but I had no luck. If anyone solves it please share.

adb root issues

Sometimes (usually the initial execution after the restart) the call to adb root does not find the device. You can simply re-play from AStudio, or sleep for a second after a successful adb root.

using su instead of adb root

adb push won't be working despite mounting system and giving permissions. To make it work replace the ADB_SH variable and the install section of the script with the following:

.. ADB_SH="$ADB shell su -c" .. # Install APK: using adb su $ADB_SH "mount -o rw,remount /system" $ADB_SH "chmod 777 /system/lib/" $ADB_SH "mkdir -p /sdcard/tmp" 2> /dev/null $ADB_SH "mkdir -p $apk_target_dir" 2> /dev/null $ADB push $apk_host /sdcard/tmp/$apk_name 2> /dev/null $ADB_SH "mv /sdcard/tmp/$apk_name $apk_target_sys" $ADB_SH "rmdir /sdcard/tmp" 2> /dev/null 
like image 156
Paschalis Avatar answered Oct 10 '22 17:10

Paschalis


Windows script for those interested:

Store this file the same way: in the root of your project directory (installSysPrivApp.bat)

::WIN BATCH SCRIPT  :: CHANGE THESE set app_package=com.example.package set dir_app_name=app set MAIN_ACTIVITY=MainActivity  set ADB="adb" ::ADB_SH="%ADB% shell" # this script assumes using `adb root`. for `adb su`  see `Caveats`  set path_sysapp=/system/priv-app set apk_host=.\Application\build\outputs\apk\Application-debug.apk set apk_name=%dir_app_name%.apk set apk_target_dir=%path_sysapp%/%dir_app_name% set apk_target_sys=%apk_target_dir%/%apk_name%  :: Delete previous APK del %apk_host%  :: Compile the APK: you can adapt this for production build, flavors, etc. call gradlew assembleDebug  set ADB_SH=%ADB% shell su -c  :: Install APK: using adb su %ADB_SH% mount -o rw,remount /system %ADB_SH% chmod 777 /system/lib/ %ADB_SH% mkdir -p /sdcard/tmp %ADB_SH% mkdir -p %apk_target_dir% %ADB% push %apk_host% /sdcard/tmp/%apk_name%  %ADB_SH% mv /sdcard/tmp/%apk_name% %apk_target_sys% %ADB_SH% rmdir /sdcard/tmp  :: Give permissions %ADB_SH% chmod 755 %apk_target_dir% %ADB_SH% chmod 644 %apk_target_sys%  ::Unmount system %ADB_SH% mount -o remount,ro /  :: Stop the app %ADB% shell am force-stop %app_package%  :: Re execute the app %ADB% shell am start -n \"%app_package%/%app_package%.%MAIN_ACTIVITY%\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER 
like image 39
Birdnado Avatar answered Oct 10 '22 17:10

Birdnado