📜 ⬆️ ⬇️

Android Development Tutorial. Part 1/?

Lars Vogel - Eclipse Evangelist.
Under the cat you will find the translation of his article, which describes the process of creating Android applications using Eclipse. Used by Eclipse 3.6, Java 1.6 and Android 2.3 (Gingerbread).
Part 2


1. Android Development


1.1. Android operating system

Android is a Linux-based operating system with a Java programming interface. It provides us with such tools as a compiler, debugger and device emulator, as well as its (Android) own Java virtual machine (Dalvik Virtual Machine - DVM). Android was created by the Open Handset Alliance, led by Google.

Android uses a special virtual machine, so called Dalvik Virtual Machine. Dalvik uses its own special bytecode. Therefore, you cannot run standard Java bytecode on Android. Android provides a “dx” tool that allows you to convert Java Class files into “dex” files (Dalvik Executable). Android applications are packaged in .apk (Android Package) files with the aapt program (Android Asset Packaging Tool). To simplify development, Google provides Android Development Tools (ADT) for Eclipse. ADT automatically converts from Java Class files to dex files, and creates apk during deployment.
')
Android supports 2D and 3D graphics using OpenGL libraries, as well as storing data in a SQLite database.

Each Android application runs in its own process and under its own userid, which is automatically generated by Android during deployment. Therefore, the application is isolated from other running applications, and a malfunctioning application cannot harm other Android applications unhindered.

1.2. The main components of Android

Android applications consist of the following parts:

Other parts of Android are widgets, or live folders (Live Folders), or live wallpapers (Live Wallpapers). Live folders display the source of any data on the “desktop” without running the corresponding applications.

1.3. Security and Permissions

Android determines specific permissions for specific tasks. For example, if an application wants to access the Internet, it must specify in its configuration file that it would like to obtain the appropriate permissions. During the installation of an Android application, the user is shown a screen on which he needs to give the application the required permissions.

1.4. AndroidManifest.xml

Android applications are described by the "AndroidManifest.xml" file. All the activites, services, receivers and content providers of the application must be declared in these files. It must also contain the permissions required by the application. For example, if an application needs network access, then it should be defined here. “AndroidManifest.xml” can be viewed as a description for deploying an Android application.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.vogella.android.temperature" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Convert" 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> <uses-sdk android:minSdkVersion="9" /> </manifest> 

The “package” attribute is the base package for the following Java elements. It must also be unique, because Android Marketplace only accepts an application for each “package” once. Therefore, it’s a good habit to use your reverse domain name as a “package” to avoid conflicts with other developers.

"Android: versionName" and "android: versionCode" determine the version of your application. "VersionName" is what the user sees and can be any string. The “versionCode” must be integer, and the Android Market uses this to determine if you have provided a new version in order to run updates on the devices on which your application is installed. Usually. starts with 1 and is incremented by one if you release a new version of the application.

The “activity” defines the activations, in this example it points to the class “de.vogella.android.temperature.Convert”. An intent filter has been registered for this class, which determines that this activation is launched when the application is launched (android action: name = "android.intent.action.MAIN"). The category definition (android category: name = "android.intent.category.LAUNCHER") determines that this application has been added to the application directory on an Android device. Values ​​with an @ sign refer to resource files that contain actual values. It simplifies working with different resources, such as strings, colors, icons, for different devices and simplifies the translation of applications.

The “uses-sdk” part of AndroidManifest.xml defines the minimum SDK version on which you can run your application. This prevents the installation of your application on devices with an older version of the SDK.

1.5. R.java, Resources and Assets

The “gen” directory in the Android project contains the generated values. "R.java" is a generated class that contains links to resources from the "res" folder of the project. These resources are contained in the “res” directory and can be values, menus, charts, icons or pictures, or animations. For example, a resource can be a drawing or XML files containing specific strings.

If you create new resources, the corresponding links will be automatically created in R.java. Links are static values ​​of type int (integer constants), the Android system provides methods for accessing the corresponding resources. For example, to access the string with the reference identifier "R.string.yourString" use the getString method (R.string.yourString)); Please do not try to change "R.java" manually.

While the “res” directory stores structured values ​​known to the Android platform, the “assets” directory can be used to store any data. In Java, you can access this data through AssetsManager and the getAssets () method.

1.6. Activity and Layouts

The user interface for the activity is defined using layouts. During execution, the layouts are instances of "android.view.ViewGroups." Layout defines the user interface elements, their properties and location. UI elements are based on the "android.view.View" class. ViewGroup is a subclass of View. Layouts can contain UI components (Views / Views) or other layouts (ViewGroups). You should not do more nesting of child elements in ViewGroups, as this affects performance.

Layout can be defined using Java code or using XML. As a rule, use Java code to generate a layout if you do not know about the content in advance. For example, if your layout depends on the content you are reading from the Internet.

XML-based layouts are defined using the resource file in the "/ res / layout" folder. This file defines a group of views (see clause 1.2), types, their relationships, and attributes for individual layouts. If the UI element requires access using Java code, give the UI element a unique identifier (id) using the "android: id" attribute. In order to assign a new identifier to the UI element, use the "@ + id / yourvalue" construction. Conditionally, this will create and assign a new id “yourvalue” to the corresponding UI element. Later in Java code, you can access these UI elements using the findViewById method (R.id.yourvalue).

Defining layouts via XML is usually preferable, since it separates software logic from layout definitions and makes it easier to define different layout resources for different devices. You can also mix both approaches.

1.7. Activation and life cycle

To be continued?
This is my first such great translation. I will be glad to constructive criticism.

Source: https://habr.com/ru/post/115022/


All Articles