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

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

问题描述

在 Android Studio 上开发时,有没有办法让应用程序直接安装在 system/app 文件夹中(设备已 root)?

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 中点击运行时自动运行它.

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 找不到设备.您可以简单地从 AStudio 重新播放,或在 adb root 成功后 sleep 重新播放一秒钟.

          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 变量和脚本的安装部分替换为以下内容:

          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天全站免登陆