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>