从Android Studio开发时如何在系统/应用程序中安装应用程序 [英] How to install an app in system/app while developing from android studio

查看:388
本文介绍了从Android Studio开发时如何在系统/应用程序中安装应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android Studio(设备已植根)上进行开发时,是否可以将应用直接安装在system/app文件夹中?

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

意思是,当我按下运行应用程序"按钮时,我希望将apk放置在系统/应用程序中.

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?

推荐答案

从AS自动部署系统应用

您可以创建一个可以完成此任务的脚本,并在每次在AS中运行run时自动运行该脚本.

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.

您可以根据自己的需要修改此脚本.将其放在:project_directory/installSystem.sh

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.将其与AS Run绑定

  1. 运行->编辑配置
  2. 在您模块的常规标签上进行以下更改

  • 安装选项->停用:没事
  • 启动选项->启动:什么都没有
  • 启动前:按+,然后按Run External Tool,选择您的脚本.
    • 在新对话框中:
      • 设置任何名称.
      • 在工具设置"->程序"上:导航到项目的目录,然后选择脚本
      • 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

          注意事项:

          首次安装

          在您的应用程序首次安装时,仅需一次重启设备(adb reboot) .之后,您只需按Run,一切都会自动发生.

          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.

          这是因为未自动调用主机编译器(dex2oat).某种程度上,尚未通知操作系统此新系统应用程序.手动调用dex2oat应该可以解决此问题,但是我没有运气.如果有人解决了,请分享.

          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的调用找不到该设备.成功完成adb root之后,您可以简单地从AStudiosleep播放一秒钟.

          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.

          adb push将无法工作.要使其正常工作,请使用以下命令替换ADB_SH变量和脚本的install部分:

          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
          

          这篇关于从Android Studio开发时如何在系统/应用程序中安装应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆