用Groovy写一款Android Studio插件

版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎访问http://github.com/shenhuanet 转载请注明出处: https://blog.csdn.net/klxh2009/article/details/78983017

用 Groovy 写一款 Android Studio 插件

本插件由 Intellij-Idea 编写, 目的是快速获取当前手机正在运行的进程.

效果

运行中
过滤

功能介绍

  • 动态获取已连接的 Android 设备
  • 支持刷新进程信息
  • 如果 Android 设备已 Root, 则可 kill 该进程
  • 支持列表信息动态过滤

目录结构

目录结构

代码实现

plugin.xml
<extensions defaultExtensionNs="com.intellij">
    <!-- Add your extensions here -->
    <toolWindow canCloseContents="false" anchor="bottom"
        id="ProcessViewer" icon="/icons/icon_logo_13x13.png"
        factoryClass="com.shenhua.idea.plugin.processviewer.ui.ProcessViewer"/>
</extensions>

<project-components>
    <component>
        <implementation-class>com.shenhua.idea.plugin.processviewer.ui.ContentComponent</implementation-class>
    </component>
</project-components>

AdbHelper.groovy

import com.android.ddmlib.AndroidDebugBridge
import com.android.tools.idea.ddms.adb.AdbService
import com.google.common.util.concurrent.FutureCallback
import com.google.common.util.concurrent.Futures
import com.google.common.util.concurrent.ListenableFuture
import com.intellij.openapi.project.Project
import com.intellij.ui.components.JBLoadingPanel

import com.shenhua.idea.plugin.processviewer.bean.Process
import org.jetbrains.android.sdk.AndroidSdkUtils
import org.jetbrains.annotations.NotNull

/**
 * Created by shenhua on 2017/11/25.
 * Email [email protected]
 *
 * @author shenhua
 */
class AdbHelper {

    AdbHelper() {
    }

    synchronized void loadingAdb(@NotNull Project project) {
        File adb = AndroidSdkUtils.getAdb(project)
        if (adb == null) {
            println("Adb file is null")
        } else {
            println("Adb file is " + adb.getAbsolutePath())
            ListenableFuture<AndroidDebugBridge> future = AdbService.getInstance().getDebugBridge(adb)
            Futures.addCallback(future, new FutureCallback<AndroidDebugBridge>() {
                @Override
                void onSuccess(AndroidDebugBridge androidDebugBridge) {
                    println("Successfully obtained debug bridge")
                }

                @Override
                void onFailure(Throwable throwable) {
                }
            })
        }
    }

    synchronized void loadingAdb(@NotNull Project project, JBLoadingPanel loadingPanel) {
        File adb = AndroidSdkUtils.getAdb(project)
        if (adb == null) {
            println("Adb file is null")
        } else {
            loadingPanel.setLoadingText("Initializing ADB")
            loadingPanel.startLoading()
            println("Adb file is " + adb.getAbsolutePath())
            ListenableFuture<AndroidDebugBridge> future = AdbService.getInstance().getDebugBridge(adb)
            Futures.addCallback(future, new FutureCallback<AndroidDebugBridge>() {
                @Override
                void onSuccess(AndroidDebugBridge androidDebugBridge) {
                    println("Successfully obtained debug bridge")
                    loadingPanel.stopLoading()
                }

                @Override
                void onFailure(Throwable throwable) {
                    loadingPanel.stopLoading()
                }
            })
        }
    }

    static boolean isInstalled() {
        // TODO ClassCastException: com.android.tools.idea.sdk.AndroidSdks cannot be cast to com.android.tools.idea.sdk.AndroidSdks
        AndroidSdkUtils.isAndroidSdkAvailable()
    }

    void killProcess(Project project, String deviceId, String pid) {
        CommandLine commandLine = new CommandLine()
        DeviceAdbParser adbParser = new DeviceAdbParser()
        String adbOutput = commandLine.executeShellCommand(getAdbCommand(project) + " -s ${deviceId} shell kill ${pid}")
        println(adbOutput)
    }

    ArrayList<Process> getProcess(Project project, String deviceId) {
        CommandLine commandLine = new CommandLine()
        DeviceAdbParser adbParser = new DeviceAdbParser()
        String adbOutput = commandLine.executeShellCommand(getAdbCommand(project) + " -s ${deviceId} shell ps")
        adbParser.parseProcessList(adbOutput)
    }

    private synchronized String getAdbCommand(Project project) {
        File adbFile = AndroidSdkUtils.getAdb(project)
        if (adbFile != null)
            "${adbFile.getAbsolutePath()}"
        else
            "adb"
    }
}

第三方库

android.jar
common.jar
sdk-tools.jar
sdklib.jar

注意事项

  • 以上 jar 包存放在 android-studio 安装目录 plugins\android\lib 下.
  • 在使用 Intellij-Idea 导入项目时应该对以上 jar 正确引入.
  • 调试运行时会打开新的 Intellij-Idea 项目, 请确认该项目是 Android 项目.

有待继续开发

  • 查看进程详细信息和关联进程信息

欢迎star 或者 fork项目: github地址

猜你喜欢

转载自blog.csdn.net/klxh2009/article/details/78983017
今日推荐