Book Image

Android Studio 4.2 Development Essentials - Kotlin Edition

By : Neil Smyth
Book Image

Android Studio 4.2 Development Essentials - Kotlin Edition

By: Neil Smyth

Overview of this book

Android Studio is an Integrated Development Environment that is based on the JetBrains IntelliJ IDEA. It provides developers with a unique platform to design and develop Android apps using various developer tools. The new Android Studio 4.2 has an upgraded IntelliJ platform and a variety of new features designed to improve the productivity of Android app developers. Fully updated for Android Studio 4.2, the objective of this book is to help you master the skills necessary to develop Android applications using Kotlin as the programming language. This book begins by outlining the steps necessary to set up an Android development and testing environment and introduces programming in Kotlin, addressing data types, flow control, functions, lambdas, and object-oriented programming. It includes an overview of Android Studio, covering areas such as tool windows, the code editor, and the Layout Editor tool. An introduction to Android architecture is followed by an in-depth explanation of the design of Android applications and user interfaces using the Android Studio environment. Early chapters detail Android Architecture components like view models, lifecycle management, Room database access, the Database Inspector, app navigation, live data, and data binding. Advanced topics such as intents are also covered, as are touch screen handling, gesture recognition, and the recording and playback of audio. You will also explore printing, transitions, cloud-based file storage, and foldable device support. Detailed descriptions of material design concepts are provided, including the use of floating action buttons, Snackbars, tabbed interfaces, card views, navigation drawers, and collapsing toolbars. Some key features of Android Studio 4.2 and Android discussed in-depth include the Layout Editor, the ConstraintLayout and ConstraintSet classes, MotionLayout Editor, view binding, constraint chains, barriers, and direct reply notifications. Later chapters cover advanced features of Android Studio such as App links, Dynamic Delivery, the Android Studio Profiler, Gradle build configuration, and submitting apps to the Google Play Developer Console.
Table of Contents (94 chapters)
94
Index

2.6 Making the Android SDK Tools Command-line Accessible

Most of the time, the underlying tools of the Android SDK will be accessed from within the Android Studio environment. That being said, however, there will also be instances where it will be useful to be able to invoke those tools from a command prompt or terminal window. In order for the operating system on which you are developing to be able to find these tools, it will be necessary to add them to the system’s PATH environment variable.

Regardless of operating system, the PATH variable needs to be configured to include the following paths (where <path_to_android_sdk_installation> represents the file system location into which the Android SDK was installed):

<path_to_android_sdk_installation>/sdk/tools

<path_to_android_sdk_installation>/sdk/tools/bin

<path_to_android_sdk_installation>/sdk/platform-tools

The location of the SDK on your system can be identified by launching the SDK Manager and referring to the Android SDK Location: field located at the top of the settings panel as highlighted in Figure 2-7:

Figure 2-7

Once the location of the SDK has been identified, the steps to add this to the PATH variable are operating system dependent:

2.6.1 Windows 7

1. Right-click on Computer in the desktop start menu and select Properties from the resulting menu.

2. In the properties panel, select the Advanced System Settings link and, in the resulting dialog, click on the Environment Variables… button.

3. In the Environment Variables dialog, locate the Path variable in the System variables list, select it and click on the Edit… button. Using the New button in the edit dialog, add three new entries to the path. For example, assuming the Android SDK was installed into C:\Users\demo\AppData\Local\Android\Sdk, the following entries would need to be added:

C:\Users\demo\AppData\Local\Android\Sdk\platform-tools

C:\Users\demo\AppData\Local\Android\Sdk\tools

C:\Users\demo\AppData\Local\Android\Sdk\tools\bin

4. Click on OK in each dialog box and close the system properties control panel.

Once the above steps are complete, verify that the path is correctly set by opening a Command Prompt window (Start -> All Programs -> Accessories -> Command Prompt) and at the prompt enter:

echo %Path%

The returned path variable value should include the paths to the Android SDK platform tools folders. Verify that the platform-tools value is correct by attempting to run the adb tool as follows:

adb

The tool should output a list of command line options when executed.

Similarly, check the tools path setting by attempting to launch the AVD Manager command line tool (don’t worry if the avdmanager tool reports a problem with Java - this will be addressed later):

avdmanager

In the event that a message similar to the following message appears for one or both of the commands, it is most likely that an incorrect path was appended to the Path environment variable:

'adb' is not recognized as an internal or external command,

operable program or batch file.

2.6.2 Windows 8.1

1. On the start screen, move the mouse to the bottom right-hand corner of the screen and select Search from the resulting menu. In the search box, enter Control Panel. When the Control Panel icon appears in the results area, click on it to launch the tool on the desktop.

2. Within the Control Panel, use the Category menu to change the display to Large Icons. From the list of icons select the one labeled System.

3. Follow the steps outlined for Windows 7 starting from step 2 through to step 4.

Open the command prompt window (move the mouse to the bottom right-hand corner of the screen, select the Search option and enter cmd into the search box). Select Command Prompt from the search results.

Within the Command Prompt window, enter:

echo %Path%

The returned path variable value should include the paths to the Android SDK platform tools folders. Verify that the platform-tools value is correct by attempting to run the adb tool as follows:

adb

The tool should output a list of command line options when executed.

Similarly, check the tools path setting by attempting to run the AVD Manager command line tool (don’t worry if the avdmanager tool reports a problem with Java - this will be addressed later):

avdmanager

In the event that a message similar to the following message appears for one or both of the commands, it is most likely that an incorrect path was appended to the Path environment variable:

'adb' is not recognized as an internal or external command,

operable program or batch file.

2.6.3 Windows 10

Right-click on the Start menu, select Settings from the resulting menu and enter “Edit the system environment variables” into the Find a setting text field. In the System Properties dialog, click the Environment Variables... button. Follow the steps outlined for Windows 7 starting from step 3.

2.6.4 Linux

On Linux, this configuration can typically be achieved by adding a command to the .bashrc file in your home directory (specifics may differ depending on the particular Linux distribution in use). Assuming that the Android SDK bundle package was installed into /home/demo/Android/sdk, the export line in the .bashrc file would read as follows:

export PATH=/home/demo/Android/sdk/platform-tools:/home/demo/Android/sdk/tools:/home/demo/Android/sdk/tools/bin:/home/demo/android-studio/bin:$PATH

Note also that the above command adds the android-studio/bin directory to the PATH variable. This will enable the studio.sh script to be executed regardless of the current directory within a terminal window.

2.6.5 macOS

A number of techniques may be employed to modify the $PATH environment variable on macOS. Arguably the cleanest method is to add a new file in the /etc/paths.d directory containing the paths to be added to $PATH. Assuming an Android SDK installation location of /Users/demo/Library/Android/sdk, the path may be configured by creating a new file named android-sdk in the /etc/paths.d directory containing the following lines:

/Users/demo/Library/Android/sdk/tools

/Users/demo/Library/Android/sdk/tools/bin

/Users/demo/Library/Android/sdk/platform-tools

Note that since this is a system directory it will be necessary to use the sudo command when creating the file. For example:

sudo vi /etc/paths.d/android-sdk