ТОП просматриваемых книг сайта:
Expert Android Studio. Dundar Onur
Читать онлайн.Название Expert Android Studio
Год выпуска 0
isbn 9781119110736
Автор произведения Dundar Onur
Жанр Зарубежная образовательная литература
Издательство John Wiley & Sons Limited
If you prefer to list resources similar to the way they are hosted in the file system, click the Android list on the top left and a menu with different options will open. Choose Project, and Android Studio will group your project resources as they appear in the file system, as shown in Figure 2.9.
Figure 2.9 Traditional project view
Let's examine this view to gain a better understanding of Android Studio project structure. Every project has a few hidden folders, which you might not be able to navigate with your file manager.
Typically, an Android project has three hidden folders, as listed at the top left of Figure 2.9. The hidden folders are:
• .idea folder– This folder keeps IntelliJ-specific project metadata and settings not necessarily shared with source control systems (so not shared to someone else).
• .gradle folder– This folder keeps Gradle-related bin files. This folder's contents are not subject to change unless you change the project's Gradle version.
• .google folder– This folder includes sample packing files from Google.
NOTE
Typically, users should not directly edit any file in the hidden folders. Let the IDE deal with them.
Next is the application folder. The name of this folder can change depending on your application name and preferences. However, you can easily recognize the folder because it has a small device symbol on the lower right of the folder icon. This type of folder holds the source code, application files, and configuration.
There can be more than one Application folder in your project depending on its size and architecture. In this chapter we will assume there's one named “app,” but will dig into different combinations in later chapters.
Expand the app folder by clicking the triangle to the left of the folder name. If you have developed Android applications before, the contents should be familiar to you. If not, the following pages give detailed information.
The project folders are:
• build folder– This folder might be the least important of all because, as a developer, you won't need to deal with or edit anything inside it. The Gradle build system will be triggered to build your source files by the IDE and produce the output into this folder.
• src/main folder– This folder might be the most important folder because it hosts all your source code except for tests. If you expand the src folder, you may see package folders that group your source files. We will explain this later in this chapter.
• src/test and src/androidTest folders– These folders might be the most underestimated in the whole Android project. The basic convention with tests is to place Unit tests into src/test and instrumentation tests into src/androidTest folders. They hold your test files, which can be run during compilation, packaging, or even on a build server. Good test coverage for your source files is needed if you want to keep your code maintainable, open to change, and still bug free!
There are also several files in the root of the project folder. These are essential because they usually affect each module in the project. You may need to edit the following essential project files.
• build.gradle– Although each module in a project has its own build.gradle file, the top level build.gradle is inherited by each of them. Any global Gradle setting for a repository or a library can be added to this file.
• local.properties– Each user has an SDK and NDK file path in their computer. For example, say you work for a corporation where you need to have proxy settings, including your credentials. Adding that personal data to a Gradle file, which would be added to source control, may not be wise. Such info can be added to local.properties and kept out of source control.
• settings.gradle– Most Android projects have multiple modules, which may consist of libs or wear extensions. Once a build is executed, Gradle checks settings.gradle to figure out which projects need to be included in the build.
You may find additional files in the root project folder, which you don't need to edit or worry about for now. Although we covered all root level files and folders, we haven't covered the most important one, the src folder.
The src folder hosts all source, resource, and application manifests. Expand the source folder to list its contents, as shown in Figure 2.10.
Figure 2.10 Expanded view of project folders
Inside the src folder is only one folder, which is named main. The main folder contains the java and res folders, which have different icons than other folders to highlight their importance.
The java folder contains all the packages in the format of reverse URL and Java classes. In our example, we have only one package, com.example.android.camera2basic, which has three Java classes. Clicking a class file will open the editor and display the chosen Java file's contents, as shown in Figure 2.11.
Figure 2.11 Opened Java file on Android Studio
We cover the editor in detail but first let's move to the other folder inside main. The res folder holds all resource files, including images, layouts, localization files, and so on. Android projects have different folders for different screen sizes, pixel densities, and other parameters, as shown in Figure 2.12.
Figure 2.12 res folder in Android project view
Placing different sizes of the same image into a drawable folder will leverage the ability of Android to display the most appropriate image for the device your application is running on. The idea is the same for the layout and values folders. Different layouts can be added for landscape and portrait views, and different values can be added for different versions and pixel densities.
This approach has given Android the capability to run on different screen sizes and densities from the beginning, unlike most other mobile platforms, which used to offer only a fixed resolution.
TIP
When you are developing for Android always keep in mind that your application may target very different screen sizes – from phones to tablets, as well as watches, TVs, and even glasses.
Finally, we can focus on one final file that is very trivial for an Android application, the AndroidManifest.xml file. The Android manifest holds metadata from the list of activities, services, application name and version, target and minimum SDK requirements, and hardware requirements for the target devices, as well as the permissions that your app requires. Listing 2.1 shows the contents of AndroidManifest.xml.
LISTING 2.1 : AndroidManifest.xml Content
<?xml version="1.0" encoding="UTF-8"?><! - C
opyright 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0