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

前端 未结 5 773

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

相关标签:
5条回答
  • 2020-12-23 10:41

    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
    
    0 讨论(0)
  • 2020-12-23 10:43

    I think adb push *.apk /system/app/*.apk should do just fine.

    I don't know about Android Studio, but if you are on Linux you can get try to create an alias for

    adb install
    

    that points to that command, It should work!

    0 讨论(0)
  • 2020-12-23 10:44

    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
    
    0 讨论(0)
  • 2020-12-23 10:57

    (Android Q >> Windows)

    ::WIN BATCH SCRIPT
    ::setup emulator https://stackoverflow.com/a/64397712/13361987
    
    :: CHANGE THESE 
    set app_package=com.project.package
    set dir_app_name=NewApkName
    set MAIN_ACTIVITY=Package.MainActivity
    
    set ADB="adb"
    
    set path_sysapp=/system/priv-app
    set apk_host=.\app\build\outputs\apk\debug\app-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 0
    
    :: Install APK: using adb su
    %ADB_SH% mount -o remount,rw /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% rm -r /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
    
    
    
    
    :: from >> https://stackoverflow.com/questions/28302833/how-to-install-an-app-in-system-app-while-developing-from-android-studio
    
    0 讨论(0)
  • 2020-12-23 11:03

    To bypass reboot issue from the @paschalis answer reinstall application with a help of package manager before remounting system to read only:

    # Reinstall app
    $ADB_SH "pm install -r $apk_target_sys"
    
    # Unmount system
    $ADB_SH "mount -o remount,ro /"
    

    Package manager will invoke dex2oat by itself.

    0 讨论(0)
提交回复
热议问题