Expert Android Studio. Dundar Onur

Чтение книги онлайн.

Читать онлайн книгу Expert Android Studio - Dundar Onur страница 8

Expert Android Studio - Dundar Onur

Скачать книгу

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.

Expanded view of project folders window.

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.

Display of Opened Java file on Android Studio screen.

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.

res folder in Android project view screen.

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 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. – >

      <manifest xmlns: android="http://schemas.android.com/apk/res/android"

      package="com.example.android.camera2basic"> <uses-permission android: name="android.permission.CAMERA"/> <uses-feature android: name="android.hardware.camera"/> <uses-feature android: name="android.hardware.camera.autofocus"/>

      <application android: allowBackup="true"

      android: label="@string/app_name"

      android: icon="@drawable/ic_launcher"

      android: theme="@style/MaterialTheme"> <activity

      android: name=".CameraActivity"

      android: label="@string/app_name"> <intent-filter>

      <action android: name="android.intent.action.MAIN"/>

      <category android: name="android.intent.category.LAUNCHER"/>

      </intent-filter>

      </activity>

      </application>

      </manifest>

      Our manifest file starts with the manifest declaration, which also declares the main package. This declaration enables us to refer to subpackages and classes by using only the suffix after the root package.

      Next, the manifest declares the permissions, followed by the uses-feature tag to declare the hardware requirements of the application.

      Every activity and service component that resides in an Android project must be listed under the application tag. The sample project consists of only one activity, which is used as the entry point of the sample app, so the activity is listed as .CameraActivity, only with the full path and name after the root package and with the LAUNCHER intent. This activity will be used for launching the application presented in the Android manifest.

      Building and Running a Project

The sample project is a complete and ready-to-run application,

Скачать книгу