Getting Started - adelphes/android-dev-ext GitHub Wiki

Getting started

Welcome to Android development with VSCode.
The Android extension allows you to edit, build and debug Android applications using VSCode.

Usage

The Android extension supports developing applications coded in Java and Kotlin. If your application is coded in another language (like Dart or JS), this extension can be used to debug any Java setup code or Java interfaces that link between your application and the underlying Android framework.

Compatibility with Android Studio

In general, applications created and developed in Android Studio are compatible with Android development in VSCode. However, there are some important notes to be aware of:

  • VSCode is designed as an edit-build-debug development environment and makes very few assumptions about your code and development setup. This flexibility makes VSCode a powerful IDE across a wide range of languages, tools and frameworks, but does require the developer to configure various settings that might be assumed by Android Studio.
  • When active, Android Studio attempts to create exclusive connections to Android devices, denying access to other debuggers and tools. If you are using VSCode to develop an Android application, having Android Studio open results in dropped connections during debugging. You should close any instances of Android Studio while developing Android applications in VSCode.
  • Gradle (the build system used with Android) is heavily integrated with Android Studio, allowing developers to easily build their app after making code changes. In VSCode, Gradle can be integrated by configuring a default build task which builds the app using a terminal command.

Setting up Android development

  • From the Extensions section, install the Android extension
  • Open your app folder in VSCode. For apps created with Android Studio (and built with gradle), open the top-level folder containing build.gradle.
  • Configure the default build task to build your app. For gradle apps:
    • Open the VSCode Command Palette and select Configure Default Build Task. Select Create tasks.json file from template and Others from the list of task choices.
    • A .vscode/tasks.json file is automatically created with a sample echo terminal command. Replace the contents with the values below and save the file.
    • Once tasks.json is saved, pressing ctrl/cmd-shift-B will execute the build task.
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "run gradle",
            "type": "shell",
            "command": "${workspaceFolder}/gradlew",
            "args": ["assembleDebug"],
            "windows": {
                "command": "${workspaceFolder}/gradlew.bat",
                "args": ["assembleDebug"]
            },
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "dedicated",
                "showReuseMessage": false,
                "clear": true
            }
        }
    ]
}

Debugging Android applications

The Android extension support debugging Android apps allowing developers to debug directly in VSCode, using breakpoints, stepping, watches and repl evaluation.
To setup debugging in VSCode:

  • From the Run view in VSCode, click create a launch.json file.
  • Choose Android from the list of environments. A .vscode/launch.json file is pre-populated with configurations for android launch and android attach.
  • In the android launch configuration, validate the appSrcRoot and apkFile values:
    • appSrcRoot must point to the folder at the base of your app files. This folder typically contains AndroidManifest.xml as well as the assets, res and source folder. This value must be configured correctly in order for the debugger to locate source files used to build your app.
    • apkFile must point to your built (debug) APK. This is needed to deploy your app to any connected devices or emulators.
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "android",
            "request": "launch",
            "name": "Android launch",
            "appSrcRoot": "${workspaceRoot}/app/src/main",
            "apkFile": "${workspaceRoot}/app/build/outputs/apk/debug/app-debug.apk",
            "adbPort": 5037
        },
        {
            "type": "android",
            "request": "attach",
            "name": "Android attach",
            "appSrcRoot": "${workspaceRoot}/app/src/main",
            "adbPort": 5037,
            "processId": "${command:PickAndroidProcess}"
        }
    ]
}

Once launch.json is configured and saved, connect an Android device (with developer debugging enabled) or start an Android emulator.

  • In VSCode, select "Android launch" in the debug configuration menu and press the run button.
  • The debugger will start and validate the launch configuration. Once a connected device is found, your APK will be transferred to the device and launched.

Remember: Any open instances Android Studio must be closed to successfully debug Android applications with VSCode. Ignoring this requirement results in a socket closed error when the debugger is launched.

More information about debugging in VSCode can be found on the Visual Studio Code website.