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?
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.
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.
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 .
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.
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.
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.
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.
You can create a script that will do the job, and run it automatically each time you hit run in AS.
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"
Do the following changes on General tab (of your module)
+
, then Run External Tool
, to select your script. 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
issuesSometimes (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
.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With