Android Studio Tutorial

Android Studio Tutorial for Beginners : Create First App

A broad beginner’s guide to Android Studio, from installing Android Studio to coding and debugging your first Android app. Comprises troubleshooting tips for the Android device emulator and source code for the Android sample app.

Android Studio Tutorial for Beginners

Advertisements

Android Studio Tutorial: Your Complete Guide To Setting Up Your First Android Studio Project

In my previous tutorial, I showed you how to install Android Studio and how to create your first Hello World app. In this Android Studio Tutorial, we’ll be going a little bit further in-depth about the different components of an Android project and how to set them up.

Creating a new project in Android Studio is simple: start by opening the program and clicking on “Start a new Android Studio project” from the welcome screen or from the File menu.

Figure 1: The first step of creating a new project in Android Studio

In a few moments, you will be presented with a dialog window to configure some basic settings for your application. Here you can give it a name, choose if you want to set it as a “Blank App” or an “Android Sample“, choose if you want to import some code samples, and finally where to save your project.

Figure 2: The New Project configuration dialog

Once you are done with these settings, click on the Next button. This will take you to the main screen of Android Studio which you should be familiar with from the first article of this Android Studio Tutorial. Here, as before, you can choose a layout for your application or select an activity template to start from scratch. The following screenshot shows the layout selection screen for a new project.

Figure 3: The Layout selection screen for a new project

For our purposes, let’s choose the “Empty Activity” option and then click on Next again to proceed to the next step of this Android Studio Tutorial. Here you need to define some basic information about your application such as its title and menu label (the latter is used in combination with Action Bar Tabs). If this were an actual application that we intended to distribute, we would also have to specify a package name here; however, since we’re just getting started it isn’t necessary yet.

Advertisements

Figure 4: Basic settings of the first activity of the app

When you are done correctly configuring these options, click on Finish. Congratulations! You’ve successfully created your first Android project!

In the next section of this Android Studio Tutorial, we will jump a little bit deeper into how an Android Studio project is organized and what each of these files does.

Files in your Project

All your files for a given Android application are stored together inside a single directory that is called simply Android.

If you followed our instructions from the previous tutorial, that means that right now the directory containing your app’s files should be located under C:\Users\Your User Name\AndroidStudioProjects\FirstApp. In it, there should be several different directories and at least one file: FirstAppActivity/java, FirstAppActivity/res, settings. gradle.

Note: If want to find out where exactly your project files are, you can go to Android Studio’s Preferences > Appearance & Behaviour > System Settings > Folder and search options.

Figure 5: A general overview of the folders inside an Android project

Let’s have a look at what each of these directories is for.

Java files: Your application logic goes into one or more java files, which are compiled into .class files that contain bytecode that will be executed by the android virtual machine (the Dalvik VM).

If you’re not familiar with Java, don’t worry! You’ll probably not have to touch them at all if your app doesn’t require any special permissions or hardware access. In fact, in our FirstApp example, we won’t need to modify it at all since it’s just a basic template.

Layouts and Styles Layouts: Describe how an Activity is displayed to the user. You can use predefined layouts provided by Android Studio components like text fields, buttons, checkboxes, spinners, etc., or you can design your own custom layouts (don’t worry if this sounds complicated; we’ll get there in good time).

A layout’s directory will contain one or more XML files that specify what kind of View s (we’ll talk about those in another article) is used for this layout, as well as any other resources it needs.

For example, if you use images in your layout then they should end up in the same directory along with the XML file that describes how to use them. It should be noted that layouts should be used as sparingly as possible. Try to design your layouts such that they don’t interfere with the behavior of your Activity.

A .xml file included in a layout directory You may also see “.java” files inside the layout folders; these are called Layout Managers and can be used to define how child Views (more on them later) inside a layout make use of the available space.

Styles define what appearance an application’s UI has, such as its color scheme or font choices. You can then apply those styles throughout your app by using them in XML attributes like android:textStyle=”bold”, for example.

Note: Styles and Themes provide similar functionality, but you should generally use Styles when the visual customization is limited to a single Activity, and Themes when it should be applied throughout your whole app.

Resource files: Every visible UI component in Android has its own resource directory where you can store assets like images or colors that are used by that component. For example, if you put an image named my_icon.png into the drawable-mdpi directory then it will be available in code as R.drawable.my_icon.

There are a bunch of different directories for various types of assets, so look around them to find the ones most suitable for your needs.

Manifest file: This file contains all the metadata required for an Android application package (APK). In particular, it specifies what permissions are required (if any), what components are used (Activities, Services, Broadcast Receivers, etc.), and much more.

Note: The use of the manifest file is covered in much greater detail in another article. For now, don’t worry about it; we’ll only need to make a few minor changes for this simple app.

Gradle Scripts As I mentioned earlier, Gradle is an open-source build system that Android Studio uses to compile projects. It automates all sorts of tasks like compiling your code into bytecode that can be executed by the Dalvik VM, packaging it into an APK file, copying resources over…you name it! Every so often you’ll change something in one of your Gradle files and the project will recompile and run.

Gradle files and their purposes

There are a bunch of different Gradle scripts in an Android Studio project, but we only need to be concerned with two of them: the build. Gradle is located in the root directory and settings. Gradle, which is (usually) located right next to it.

The former applies general rules as well as those for our app, while the latter tells Android Studio where our code is located (otherwise it wouldn’t know about Java classes or Android layouts).

You’ll probably never touch these unless you want to do something advanced like having multiple projects sharing code (remember: each one has its own module and Gradle), but you want to make

Components of Android Studio

Android Studio is divided into six main components: an IDE, a Gradle plugin, a build system, a debugger, DDMS, and Android Virtual Device (AVD) Manager. Each component is briefly described below. For more details on some of these components please visit the Android Developer site.

1. The IDE: Android Studio uses IntelliJ IDEA as its IDE. The Android Studio IDE provides a code editor with support for direct code execution, code completion, refactoring, and quick help.

2. Gradle plugin: When you build an Android project from the command line using the included “gradlew” script, your project is actually built by a specialized version of Gradle. This plugin ensures that your project’s build files (such as the build. Gradle file) are complete and correct, and it also provides you with some useful tasks for building, running, testing, and deploying your project or its individual components.

3. Android Studio Build System: The Android Studio build system is based on Gradle which builds upon Apache Maven to support IDEs like Eclipse ADT (Android Developer Tools) and Android Studio.

This means that any tooling built for ADT can be used with Gradle via an import statement in the root build. Gradle file; this includes the Android SDK Build tools, emulator images, platform tools, etc every time you change a setting in the build.

Gradle file invokes another tool such as aapt or dx to process that configuration change and handle the relevant command line arguments for you. For example, when you change your targetSdkVersion in the build. Gradle file from “17” to “18,” Android Studio invokes aapt with “-v 18” behind the scenes so you can still run your APK in an emulator running version 17, but also compile against APIs available only in version 18.

Gradle is itself a very powerful system and there’s no need to understand it fully to use Android Studio.

4. The Debugger: Android Studio has a built-in debugger that lets you debug your Java or Kotlin code. It uses the popular JDB (Java Debugger) command-line interface, which users of Eclipse should be familiar with.

5. The DDMS: The Dalvik Debug Monitor Server (DDMS) provides port-forwarding services, screen capture on the device, thread and heap information on the device, logcat, process, radio, and packet logging. DDMS also allows you to install APKs onto devices connected over USB for debugging purposes.

6. AVD Manager: The AVD Manager provides an emulator UI for creating and managing Android Virtual Devices (AVDs). You can use it to create hardware configurations to test your application, launch an emulator based on those configurations, and manage the different AVDs you have available.

If you are an Android developer, then the IDE is where you spend most of your time. This tool will be used extensively to develop, debug and deploy your applications on both real devices as well as virtual emulators created using the AVD Manager.

The rest of the tools are command-line utilities that you can use from a terminal or Windows Command Prompt window to build projects or manage SDK components for use by other developers.

There are also specialized versions of these commands available in Gradle Wrapper scripts that simplify their usage during development by hiding some of their options, so this article does not go into great detail about them here.

Android Studio provides many useful code editing features to help you write better code faster. It also includes productivity tools to make you more efficient and productive when building Android applications.

There is a wide array of settings you can configure for Android Studio, too many to list here. In fact, you can even tell it how many lines of code or which files should be excluded from analysis if it would slow down the IDE too much when trying to manage them in real-time.

7. Android Debug Bridge: The Android Debug Bridge (ADB) is a client-server program used to communicate with an emulator or physical device. It consists of both a server written in Java and a client written in C++.

This command lets you manage the emulators including launching new ones as well as shutting them down or sending signals to them to simulate life-cycle events.

8. Ddms: The Dalvik Debug Monitor Server (DDMS) provides port-forwarding services, screen capture on the device, thread and heap information on the device, logcat, process, radio, and packet logging. DDMS also allows you to install APKs onto devices connected over USB for debugging purposes.

In addition to launching and managing emulators, Android Studio also provides a UI that lets you configure the emulator skin as well as providing options to perform common device actions such as taking screenshots or using media controls.

9. Android Asset Packaging Tool: The Android Asset Packaging Tool (AAPT) is used to create .apk files from .jar or.AAR packages generated by Android Studio. It is also used to manage the dependencies and splits the output into multiple ‘javabin‘ files for distribution and publication on Google Play.

10. Dex2jar: The dex2jar tool converts Dalvik Executable (DEX) bytecode to Java bytecode.

11. Dx: The dx tool lets you run Java bytecode through java.tools API and gives you access to java. lang. invoke package, which is how Android calls native code from within Dalvik code using .so files and JNI (Java Native Interface).

12. Aapt: The aapt tool lets you manage all aspects of building a .apk file, such as generating R.java and other resources as well as packaging Java bytecode into the application.

13. Zipalign: The zipalign tool is used to optimize and re-align Android package (APK) files to make them more efficient when running on a device. This is particularly important to fix any alignment-related issues that could cause incorrect behavior.

14. Android Resources: The android tool lets you manage the resources, like strings and user interface layouts, inside your project’s res/ folder as well as perform asset (images) manipulation such as converting them to different resolutions.

Conclusion

With these little tips, everyone should be able to Set Up Android Studio with all the Basic Tools to start using it.

As you could see, Android Studio offers many other tools and settings that can help increase your productivity or fine-tune the IDE to better suit your needs. It is definitely worth delving deeper into them!

Advertisements

Leave a Reply

Your email address will not be published.

Back to top button