• bin/yourapp.ap_
holds your application’s resources, packaged as a ZIP file (where yourapp
is the name of your application)
• bin/yourapp-debug.apk
or bin/yourapp-unsigned.apk
is the actual Android application (where yourapp
is the name of your application)
The .apk
file is a ZIP archive containing the .dex
file, the compiled edition of your resources ( resources.arsc
), any un-compiled resources (such as what you put in res/raw/
) and the AndroidManifest.xml
file. It is also digitally signed, with the -debug
portion of the filename indicating it has been signed using a debug key that works with the emulator, or -unsigned
indicating that you built your application for release (ant release), but the APK still needs to be signed using jarsigner
and an official key.
CHAPTER 3
Inside the Manifest
The foundation for any Android application is the manifest file: AndroidManifest.xml
in the root of your project. Here is where you declare what is inside your application — the activities, the services, and so on. You also indicate how these pieces attach themselves to the overall Android system; for example, you indicate which activity (or activities) should appear on the device’s main menu (aka the launcher).
When you create your application, you will get a starter manifest generated for you. For a simple application, offering a single activity and nothing else, the auto-generated manifest will probably work out fine, or perhaps require a few minor modifications. On the other end of the spectrum, the manifest file for the Android API demo suite is over 1,000 lines long. Your production Android applications will probably fall somewhere in the middle.
Most of the interesting bits of the manifest will be described in greater detail in the chapters on their associated Android features. For example, the service
element is described in greater detail in Chapter 30. For now, you just need to understand what the role of the manifest is and its general construction.
In the Beginning There Was the Root, and It Was Good
The root of all manifest files is, not surprisingly, a manifest
element:
package="com.commonsware.android.search">
...
Note the namespace declaration. Curiously, the generated manifests apply it only on the attributes, not the elements (e.g., it’s manifest
, not android:manifest
). However, that pattern works, so unless Android changes, stick with their pattern.
The biggest piece of information you need to supply on the manifest element is the package
attribute (also curiously not namespaced). Here you can provide the name of the Java package that will be considered the “base” of your application. Then, everywhere else in the manifest file that needs a class name, you can just substitute a leading dot as shorthand for the package. For example, if you needed to refer to com.commonsware.android.search.Snicklefritz
in our example manifest, you could just use .Snicklefritz
since com.commonsware.android.search
is defined as the application’s package.
Permissions, Instrumentations, and Applications (Oh, My!)
Underneath the manifest element, you will find the following:
• uses-permission
elements to indicate what permissions your application will need in order to function properly. See Chapter 29 for more details.
• permission
elements to declare permissions that activities or services might require other applications hold in order to use your application’s data or logic. Again, more details are forthcoming in Chapter 29.
• instrumentation
elements to indicate code that should be invoked on key system events, such as starting up activities, for the purposes of logging or monitoring.
• uses-library
elements to hook in optional Android components, such as mapping services
• Possibly a uses-sdk
element to indicate what version of the Android SDK the application was built for.
• An application
element defining the guts of the application that the manifest describes.
In the following example the manifest has uses-permission elements to indicate some device capabilities the application will need — in this case, permissions to allow the application to determine its current location. And there is the application element, whose contents will describe the activities, services, and whatnot that make up the bulk of the application itself.
package="com.commonsware.android">
android:name="android.permission.ACCESS_LOCATION" />
android:name="android.permission.ACCESS_GPS" />
android:name="android.permission.ACCESS_ASSISTED_GPS" />
android:name="android.permission.ACCESS_CELL_ID" />
...
Your Application Does Something, Right?
The real meat of the manifest file is the children of the application
element.
By default, when you create a new Android project, you get a single activity
element:
package="com.commonsware.android.skeleton">
This element supplies android:name
for the class implementing the activity, android:label
for the display name of the activity, and (frequently) an intent-filter
child element describing under what conditions this activity will be displayed. The stock activity
element sets up your activity to appear in the launcher, so users can choose to run it. As you’ll see later in this book, you can have several activities in one project if you so choose.
You may also have one or more receiver elements indicating non-activities that should be triggered under certain conditions, such as when an SMS message comes in. These are called intent receivers and are described in Chapter 23.
You may have one or more provider elements indicating content providers — components that supply data to your activities and, with your permission, other activities in other applications on the device. These wrap up databases or other data stores into a single API that any application can use. Later you’ll see how to create content providers and how to use content providers that you or others create.
Finally, you may have one or more service elements describing services — long-running pieces of code that can operate independent of any activity. The quintessential example is the MP3 player, where you want the music to keep playing even if the user pops open other activities and the MP3 player’s user interface is “misplaced.” Chapters 30 and 31 cover how to create and use services.
Android, like most operating systems, goes through various revisions, versions, and changes. Some of these affect the Android SDK, meaning there are new classes, methods, or parameters you can use that you could not in previous versions of the SDK.
If you want to ensure your application is run only on devices that have a certain version (or higher) of the Android environment, you will want to add a uses-sdk element as a child of the root manifest
element in your AndroidManifest.xml
file. The uses-sdk
element has one attribute, minSdkVersion
, indicating which SDK version your application requires:
package="com.commonsware.android.search">
Читать дальше