RePlugin调试时执行Gradle任务rpInstallAndRunPluginDebug失败原因分析

Android Studio版本:Android Studio Electric Eel | 2022.1.1

gradle插件版本:7.4.2

gradle文件:gradle-7.5-all.zip

用Android Studio打开plugin工程,在界面右侧执行gradle任务replugin-plugin执行rpInstallAndRunPluginDebug任务,

预期结果:编译插件并运行插件

实际结果:编译插件但并未运行插件

task执行日志如下:

java 复制代码
> Task :app:rpInstallPluginDebug
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell am start -n "com.qihoo360.replugin.sample.host/com.qihoo360.replugin.sample.host.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
< replugin-plugin-v3.1.0 > - Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.qihoo360.replugin.sample.host/.MainActivity }
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell am broadcast -a com.qihoo360.replugin.sample.host.replugin.uninstall -e plugin webview
< replugin-plugin-v3.1.0 > - Broadcasting: Intent { act=com.qihoo360.replugin.sample.host.replugin.uninstall flg=0x400000 (has extras) }
< replugin-plugin-v3.1.0 > - Broadcast completed: result=0
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell am force-stop com.qihoo360.replugin.sample.host
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell am start -n "com.qihoo360.replugin.sample.host/com.qihoo360.replugin.sample.host.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
< replugin-plugin-v3.1.0 > - Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.qihoo360.replugin.sample.host/.MainActivity }
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe push D:\WorkProject2\qihoo\RePlugin-dev\RePlugin-dev\replugin-sample\plugin\plugin-webview\app\build\outputs\apk\debug\app-debug.apk /sdcard/
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell pm grant com.qihoo360.replugin.sample.host android.permission.WRITE_EXTERNAL_STORAGE 
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell pm grant com.qihoo360.replugin.sample.host android.permission.READ_EXTERNAL_STORAGE 
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell sleep 2 
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell am broadcast -a com.qihoo360.replugin.sample.host.replugin.install -e path /sdcard/app-debug.apk -e immediately true 
< replugin-plugin-v3.1.0 > - Broadcasting: Intent { act=com.qihoo360.replugin.sample.host.replugin.install flg=0x400000 (has extras) }
< replugin-plugin-v3.1.0 > - Broadcast completed: result=0

> Task :app:rpInstallAndRunPluginDebug
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe shell am broadcast -a com.qihoo360.replugin.sample.host.replugin.start_activity -e plugin webview
< replugin-plugin-v3.1.0 > - Broadcasting: Intent { act=com.qihoo360.replugin.sample.host.replugin.start_activity flg=0x400000 (has extras) }
< replugin-plugin-v3.1.0 > - Broadcast completed: result=0

BUILD SUCCESSFUL in 3s
31 actionable tasks: 2 executed, 29 up-to-date

Build Analyzer results available
18:07:39: Execution finished 'rpInstallAndRunPluginDebug'.

发现一条日志

java 复制代码
< replugin-plugin-v3.1.0 > $ D:\.env\Android\hw-android-sdk\platform-tools\adb.exe push D:\WorkProject2\qihoo\RePlugin-dev\RePlugin-dev\replugin-sample\plugin\plugin-webview\app\build\outputs\apk\debug\app-debug.apk /sdcard/

gradle将生成的新插件push到了设备里面

并且logcat过滤sdcard有如下日志:

java 复制代码
2024-12-03 18:15:25.011  8724-8724  ziparchive              com.qihoo360.replugin.sample.host    W  Unable to open '/sdcard/app-debug.apk': Permission denied
2024-12-03 18:15:25.011  8724-8724  st:GuardService         com.qihoo360.replugin.sample.host    E  Failed to open APK '/sdcard/app-debug.apk': I/O error
2024-12-03 18:15:25.012  8724-8724  PackageManager          com.qihoo360.replugin.sample.host    W  Failure to parse package archive apkFile= /sdcard/app-debug.apk
2024-12-03 18:15:25.012  8724-8724  HostEventCallbacks      com.qihoo360.replugin.sample.host    D  onInstallPluginFailed: Failed! path=/sdcard/app-debug.apk; r=READ_PKG_INFO_FAIL

host宿主apk读取sdcard的新插件失败了,

在host宿主apk添加权限请求

java 复制代码
manifest
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
	if (!Environment.isExternalStorageManager()) {
		Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
		intent.setData(Uri.parse("package:" + getPackageName()));
		startActivity(intent);
		return;
	}
}

手动授权android.permission.MANAGE_EXTERNAL_STORAGE权限后,执行rpInstallAndRunPluginDebug任务,达到预期结果,成功编译插件并运行插件

注意1:

android.permission.MANAGE_EXTERNAL_STORAGE权限无法通过如下命令获得

adb shell pm grant com.qihoo360.replugin.sample.host android.permission.MANAGE_EXTERNAL_STORAGE

注意2:

plugin项目中至少要有一个activity在manifest中声明了下面这个action才会在执行完rpInstallAndRunPluginDebug任务后打开插件

java 复制代码
<intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
相关推荐
ThetaarSofVenice1 分钟前
【Java从入门到放弃 之 final 关键字】
java·开发语言·python
赔罪10 分钟前
Java 内部类与异常类
java·开发语言·intellij-idea·myeclipse
凌寒ᨐ舞15 分钟前
idea 运行 docker-compose 文件问题
java·docker·intellij-idea
静心观复22 分钟前
Java NIO、AIO分析
java·开发语言·nio
静心观复22 分钟前
java IO 与 BIO、NIO、AIO
java·nio
袁庭新24 分钟前
什么是Lua协同程序?和线程有什么区别?
java·开发语言·lua·脚本语言·袁庭新·什么是lua协同程序·lua协同程序
程序员小杰@26 分钟前
Java的 BIO、NIO、AIO?分别的作用和用法
java·python·nio
一丝晨光26 分钟前
如何很快将文件转换成另外一种编码格式?编码?按指定编码格式编译?如何检测文件编码格式?Java .class文件编码和JVM运行期内存编码?
java·c++·python·visual studio·unicode·ansi·utf8
喜欢小苹果的码农1 小时前
javaCV音频剪切
java·音视频
北京_宏哥1 小时前
《手把手教你》系列技巧篇(十七)-java+ selenium自动化测试-元素定位大法之By css上卷(详细教程)
java·selenium·前端框架