Android app components

Android apps typically consist of some, or all, the four different components listed following:

  • Activities
  • Services
  • Broadcast receivers
  • Content providers

Activities

An activity provides a screen with which users can interact in order to do something. Sometimes, it could include a few fragments inside. A fragment represents a behaviour or a portion of user interface in an activity. Users can perform operations such as making a call, sending an SMS, and so on. A good example of an activity could be the login screen of your Facebook app. The following screenshot shows the activity of the calculator application:

Activities

Services

A service can perform long-running operations in the background and does not provide a user interface. If you take the music application, you can close all of its screens after selecting the song of your choice. Still the music will be playing in the background. The following screenshot shows the services running on my device:

Services

Broadcast receivers

A broadcast receiver is a component that responds to system-wide broadcast announcements such as battery low, boot completed, headset plug, and so on. Though most of the broadcast receivers are originated by the system, applications can also announce broadcasts. From a developer's viewpoint, when the app needs to do some action only when there is a a specific event broadcast receiver is used.

Content providers

A content provider presents data to external applications as one or more tables. When applications want to share their data with other applications, a content provider is a way, which acts as an interface for sharing data among applications. Content providers use standard insert(), query(), update(), delete() methods to access application data. A special form of URI that starts with content:// is assigned to each content provider. Any app, which knows this URI, can insert, update, delete, and query data from the database of the provider app if it has proper permissions.

Example: Using content://sms/inbox content providers, any app can read SMS from the inbuilt SMS app's repository in our device. *READ_SMS permission must be declared in the app's AndroidManifest.xml file in order to access the SMS app's data.

Content providers

Android app build process

In all the previous sections, we have been dealing with APK files only. It is important to understand how these APK files are created behind the screens. When a developer builds an app using an IDE such as Android Studio, typically he performs the following at a high level.

As we have seen earlier, an Android project usually contains a Java source, which is compiled into classes.dex, a binary version of AndroidManifest.xml and other resources that are bundled together during the compilation and packaging process. Once it is done, the app has to be signed by the developer. Finally, it is ready to install and run on the device.

Though it looks very simple from a developer's point of view, it consists of complex processes behind the screens. Let's see how the whole build system works.

According to Google's official documentation, following is the complete build system process:

Android app build process
  1. The first step in the build process involves compiling the resource files such as AndroidManifest.xml and other XML files used for designing the UI for the activities. This process is done using a tool known as aapt (short for Android Asset Packaging Tool). This tool generates a file called R.java with a couple of constants inside it enabling us to reference them from our Java code:
    Android app build process
  2. If any .aidl (Android Interface Definition Language) files are used in the project, the aidl tool converts them to .java files. Usually AIDL files are used when we allow clients from different applications to access your service for IPC and want to handle multithreading in your service.
  3. Now we are ready with all the Java files that can be compiled by our java compiler. Javac is the compiler used to compile these java files and it generates .class files.
  4. All the .class files have to be converted into .dex files. This is done by the dx tool. This process generates a single DEX file with the name classes.dex.
  5. The classes.dex file generated in the previous step, resources that are not compiled such as images, and compiled resources are sent to the Apk Builder tool, which packages all these things into an APK file.
  6. To install this APK file on an Android device or emulator, it has to be signed with a debug or release key. During the development phase, IDE signs the app with a debug key for testing purposes. The signing process can be manually done from the command line using Java Keytool and jarsigner.
  7. When the application is ready for final release, it has to be signed with a release key. When an app is signed with a release key, it must be aligned using the zipalign tool for memory optimization while it runs on the device.

Reference: http://developer.android.com/sdk/installing/studio-build.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.234.28