原配Android Studio

要说Android开发者最常用的开发工具那非Android Studio莫属了,基于IntelliJ IDEA,提供了很多强大的功能,是Android开发者的首选,但是Android Studio也有自己的一些问题,就是用的时候感觉有些臃肿,多打开几个页面内存就起来了,电脑风扇嗡嗡作响,而且每次要等他同步完项目才能进行编译,当然这些其实还好,主要是最近开始看一些C++代码,Android Studio看起来多少有些不方便,由于之前写游戏的时候都是用vscode来写C++的,vscode跨平台,颜值高,而且对C++支持很好,所以自然而然就想起能不能用它来开发android程序呢?

理论支持

首先评估一下就日常开发来看,我主要用的点就是代码跳转、项目编译和调试、ui预览,但是大家都知道vscode其实就是一个轻量的代码编辑器,功能上介于ide和文本编辑器之间,让他支持Android开发,最主要的就是看插件了。

插件装起来

首先看一文档,但是只找到了对Java的支持,没有找到对Android和Kotlin的支持,对Java的支持可以看文档
https://code.visualstudio/docs/java/java-tutorial

Android插件

我主要装了这么几个插件

上图这个插件可以看到就是用来Android开发和调试用的,看看它支持什么

代码调试、变量查询,logcat、源码查询,看着还不错,但是怎么编译呢?首先要了解的就是vscode中的task,vscode中的task可以配置为运行脚本和启动进程,这样许多现有的工具就可以在VS Code中使用,而不需要输入命令行或编写新代码。那么我们来配置vscode来让它支持对android工程的编译。在项目根目录下的.vscode中添加一个tasks.json,在里面添加代码如下。

    "version": "2.0.0",
    "tasks": [
        {
            "label": "run gradle",
            "type": "shell",
            "command": "${workspaceFolder}/gradlew",
            "args": [
                "assembleDebug"
            ],
            "problemMatcher": [
                "$eslint-stylish"
            ]
        },
        {
            "label": "gradle debug",
            "type": "shell",
            "command": "adb",
            "args": [
                "install",
                "-t",
                "${workspaceFolder}/xt-application/build/outputs/apk/debug/xt-application-debug.apk"
            ],
            "dependsOn": [
                "run gradle"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$eslint-stylish"
            ]
        },
        {
            "label": "kuafu debug",
            "type": "shell",
            "command": "${workspaceFolder}/kuafuconfig.sh",
            "args": [
                "/Users/skateboard/AndroidStudioProjects/aquaman_android",
                "Debug",
                "1.4.0",
                "0"
            ],
            "problemMatcher": [
                "$eslint-stylish"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean project",
            "type": "shell",
            "command": "${workspaceFolder}/gradlew",
            "args": [
                ":xt-application:clean"
            ],
            "problemMatcher": [
                "$eslint-stylish"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

这个json文件中的tasks json数组中的每个单元代表了一个task,以gradle debug为例,这个task其实就是将编译好的的apk通过adb安装到手机,其中label代表的是task的名称,type代表的是task的类型,这里是shell脚本,命令代表的是这个task执行的shell命令是什么,这里执行的命令是adb,args代表的是adb命令后面跟随的参数,可以看到它是一个json数组,每个参数选项就是一个单元,dependsOn代表的是这个task执行前需要执行的前置task,这里的gradle debug这个task执行的时候需要先执行"run gradle"task,它代表的就是我们在命令行使用的./gradlew assembleDebug编译。当然掌握了这些你就可以自定义task了,可以看到我还定义了clean project的task,这个是用来clean project的,名为kuafu debug的task,这个是我们用来进行增量代码编译的,使用的是我们自己的插件。其实vscode的task看上去和gradle的task其实差别不大,就是功能弱了一些,但是配置更简单了一些。之后在vscode的Terminal-Run Task中打开选项栏,输入相应的task名称,就能执行了,配合之前安装的插件,目前使用Vscode开发Android基本就够用了,当然你可能需要调试代码或者使用到查看logcat等的需要,这个时候你既可以选择编写自定义的task来实现也可以使用刚才安装的插件提供的功能,通过配置launch.json,在其中添加配置来实现。

    "version": "0.2.0",
    "configurations": [
        {
            "type": "android",
            "request": "attach",
            "name": "Android Attach",
            "appSrcRoot": "${workspaceRoot}/xt-application/src/main",
            "adbPort": 5037,
            "processId": "${command:PickAndroidProcess}"
        },
        {
            // configuration type, request  and name. "launch" is used to deploy the app
            // to your device and start a debugging session.
            "type": "android",
            "request": "launch",
            "name": "App Build & Launch",
            "preLaunchTask": "run gradle",
            "targetDevice": "GDBNW20309000947",
            // Location of the App source files. This value must point to the root of
            // your App source tree (containing AndroidManifest.xml).
            "appSrcRoot": "${workspaceRoot}/xt-application/src/main",
            // Fully qualified path to the built APK (Android Application Package).
            "apkFile": "${workspaceFolder}/xt-application/build/outputs/apk/debug/xt-application-debug.apk",
            // `host:port` configuration for connecting to the ADB (Android Debug Bridge) server instance.
            // Default: localhost:5037
            "adbSocket": "localhost:5037",
            // Automatically launch 'adb start-server' if not already started.
            // Default: true
            "autoStartADB": true,
            // Launch behaviour if source files have been saved after the APK was built.
            // One of: [ ignore warn stop ]. Default: warn
            "staleBuild": "warn",
            // Fully qualified path to the AndroidManifest.xml file compiled into the APK.
            // Default: "${appSrcRoot}/AndroidManifest.xml"
            "manifestFile": "${workspaceRoot}/xt-application/src/main/AndroidManifest.xml",
            // Custom arguments passed to the Android package manager to install the app.
            // Run 'adb shell pm' to show valid arguments. Default: ["-r"]
            "pmInstallArgs": [
                "-r"
            ],
            // Custom arguments passed to the Android application manager to start the app.
            // Run `adb shell am` to show valid arguments.
            // Note that `-D` is required to enable debugging.
            "amStartArgs": [
                "-D",
                "--activity-brought-to-front",
                "-a android.intent.action.MAIN",
                "-c android.intent.category.LAUNCHER",
                "-n com.***.**t/com.**.**.launch.LaunchActivity"
            ],
            // Manually specify the activity to run when the app is started. This option is
            // mutually exclusive with "amStartArgs".
            // "launchActivity": "com.kwai.aquaman.launch.LaunchActivity",
            // Time in milliseconds to wait after launching an app before attempting to attach
            // the debugger. Default: 1000ms
            "postLaunchPause": 1000,
            // Set to true to output debugging logs for diagnostics.
            "trace": true
        }
    ]
}

下面就是我配置的demo,由于注释写的很清晰,我就不一一解释了,使用这个配置就可以实现打包安装重启一条龙,还支持断点调试。

Kotlin插件

项目中由于大量使用了Kotlin代码,所以我需要Kotlin代码的语法高亮以及代码不全和跳转等功能,但是上面说的Android插件并不能提供Kotlin支持,为此,我又安装了下面的插件。


虽然按照它自己的介绍,它支持定义的跳转,代码不全等等,但是很遗憾,我在实际使用的时候并不能实现定义的跳转以及代码不全。

总结

几经尝试,未能解决kotlin的代码不全已经定义跳转问题,为此,我不得不放弃使用vscode开发的想法,可能是我的使用姿势不对吧,欢迎大佬指导,只是时间紧,任务重,还是老老实实用Android Studio开发吧。

关注我的公众号:“滑板上的老砒霜”

更多推荐

使用vscode开发Android从开始到放弃