Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 104 ยท Getting Started

The Manifest and Resources

Learn what AndroidManifest.xml declares, how intent filters work, and how strings, colors, dimens and qualifiers such as night, locale and density pick the right resource.

Two parts of an Android project are not Kotlin code at all, yet they decide a great deal about how your app behaves: the manifest and the resources. The manifest tells the operating system what your app is made of and what it needs. The resources hold everything that is not logic: text, colors, sizes, images. Resources are also what let one app look right in dark mode, in Arabic or on a tablet.

Both are read-only here, since they need the Android build: the XML is real, accurate and explained line by line. A runnable stand-in at the end implements the rule the system uses to pick the best resource for a device configuration, so you can see why the result sometimes surprises people.

AndroidManifest.xml

The manifest is read by the system before any of your code runs. It has four main jobs:

  1. Name the app's components: activities, services, broadcast receivers and content providers.
  2. Declare the permissions the app needs, and any it defines itself.
  3. Say which Activity is the launcher entry point, and which components others may start.
  4. Declare required features and metadata, such as "this app needs a camera".
XML
<!-- read-only example: AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-feature android:name="android.hardware.camera" android:required="false" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Notes">

        <activity android:name=".MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ShareReceiverActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>

        <service android:name=".SyncService" android:exported="false" />
    </application>
</manifest>

Points to notice:

  • android:exported. Says whether components of other apps may start this one. It is required on any component that has an intent filter. Set it to true only for components meant to be reached from outside, such as the launcher Activity, and false otherwise. Exported components are part of your attack surface, a topic revisited in Securing Your App.
  • <uses-permission>. Asks for a permission. Dangerous permissions also need a runtime request (Runtime Permissions). INTERNET is a normal permission, granted at install.
  • <uses-feature required="false">. Declares that the app can use a camera without requiring one. With true, Google Play hides your app from devices without a camera.
  • Relative names. .MainActivity is relative to the module's namespace.

Intent filters

An intent is a message that asks the system to do something: open a screen, send some text, view a link. An intent filter is how a component says which intents it can handle. A filter has up to three parts:

  • action: what to do (VIEW, SEND, MAIN);
  • category: extra context (LAUNCHER, DEFAULT, BROWSABLE);
  • data: what kind of content (a MIME type, or a URI scheme and host).

For an implicit intent to match a filter, its action must be listed in the filter, every category it carries must be listed, and its data must match. The MAIN plus LAUNCHER filter is what places your icon on the home screen. Intents and Launching Other Screens covers intents in depth.

This stand-in resolves implicit intents against declared filters using exactly the three checks above. It is a model: the real resolver also handles priorities, URIs with several parts and the chooser dialog.

Kotlin Matching intents to filters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

The PDF share finds nothing because no filter declares that MIME type. In a real app that situation is a crash (ActivityNotFoundException) unless you catch it, which is a good reason to handle failures when launching implicit intents.

Resources

Resources live in res/. Anything that could vary by language, screen or theme should be a resource instead of a literal in code. You have already met the folder names. Here are the values files you will use most.

XML
<!-- read-only example: res/values/strings.xml -->
<resources>
    <string name="app_name">Notes</string>
    <string name="greeting">Welcome back, %1$s!</string>
    <plurals name="note_count">
        <item quantity="one">%d note</item>
        <item quantity="other">%d notes</item>
    </plurals>
</resources>
XML
<!-- read-only example: res/values/colors.xml and res/values/dimens.xml -->
<resources>
    <color name="brand_primary">#6750A4</color>
    <dimen name="screen_padding">16dp</dimen>
</resources>

%1$s is a positional placeholder, so translators may reorder arguments. Strings should never be concatenated in code. A dimen uses dp (density-independent pixels, a physical size that looks the same on every screen) for sizes and sp (scale-independent pixels) for text, which also follows the user's font-size setting. Plural forms are covered in Accessibility and Localization.

In Kotlin you use generated ids:

Kotlin
// read-only example: reading resources in Compose code
val title = stringResource(R.string.app_name)
val welcome = stringResource(R.string.greeting, userName)
val padding = dimensionResource(R.dimen.screen_padding)
val notes = pluralStringResource(R.plurals.note_count, count, count)

Qualifiers: one resource, many versions

Put a variant in a folder whose name has qualifiers and the system will use it when the configuration matches. Some common ones:

Qualifier Folder example Matches
Locale values-es, values-pt-rBR The user's language and region
Night mode values-night Dark theme is active
Smallest width values-sw600dp Devices with a shortest side of at least 600dp, typically tablets
Density drawable-xhdpi A screen density bucket
Orientation layout-land Landscape

Qualifiers can be combined and must appear in a fixed order: values-es-night, for example, not values-night-es. The order is part of the rule that picks a winner.

flowchart TD
    A["Collect all folders for the resource type"] --> B["Remove folders that contradict the device"]
    B --> C["Take the next qualifier in precedence order"]
    C --> D{"Does any remaining folder<br/>use this qualifier?"}
    D -- "no" --> C
    D -- "yes" --> E["Keep only folders that use it"]
    E --> F{"One folder left?"}
    F -- "no" --> C
    F -- "yes" --> G["Use that folder"]

The system removes mismatches, then narrows candidates qualifier by qualifier, from highest precedence to lowest.

The precedence order puts locale very high, then smallest width, then night mode, then density, among others. That has a consequence: if you have both values-es and values-night and the device is Spanish and in dark mode, the Spanish folder wins, because locale outranks night mode. That is why teams create the combination folder values-es-night when they truly need both.

A stand-in that picks the best resource

This program implements the elimination rule for four qualifier types: locale, smallest width, night mode and density. It is a simplified model: the real system supports many more qualifiers and has extra rules for density scaling, but the narrowing logic is the same.

Kotlin Choosing the best resource folder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Look at the last row. On an English tablet in dark mode, both values-sw600dp and values-night match. Because smallest width outranks night mode, the tablet variant wins and the dark variant is ignored. That is a real trap: if you add a -sw600dp folder for strings or dimensions, remember that dark-mode variants for the same resource will not apply on tablets unless you also make values-sw600dp-night.

Other manifest elements you will meet

Beyond <application> and its components, the manifest has a few top-level elements that describe what the app needs from the device. Google Play uses them to decide which devices can see and install your app.

  • <uses-sdk> declares API levels. In current projects minSdk, targetSdk and compileSdk live in build.gradle.kts, and the Gradle plugin writes <uses-sdk> into the merged manifest for you (Gradle for Android). Old projects set android:minSdkVersion and android:targetSdkVersion here by hand.
  • <uses-feature> declares hardware or software features: android.hardware.camera, android.hardware.nfc, android.hardware.sensor.stepcounter, android.hardware.touchscreen. With android:required="true" Play hides the app from devices without the feature; with required="false" the app installs everywhere and must check at run time (packageManager.hasSystemFeature(...)). Some permissions imply features (the camera permission implies a camera unless you add required="false").
  • <uses-configuration> limits the app to devices with a given input configuration: a keyboard type, a navigation method (trackball, D-pad), a touchscreen type. It is rare today, but it appears in code for TV and special-purpose hardware.
  • <uses-library> says the app needs a shared library that is not part of the core framework, such as an optional vendor library or org.apache.http.legacy. With android:required="false" the app still installs when the library is missing.
  • <supports-screens> and <compatible-screens> describe screen sizes. Prefer responsive layouts and <supports-screens> only for legacy cases.
  • <queries> (Android 11 and later) lists the other apps or intents your app needs to see, because package visibility is restricted by default.
XML
<!-- read-only example: a camera app that works without autofocus and optionally uses NFC -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-feature android:name="android.hardware.camera" android:required="true" />
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
    <uses-feature android:name="android.hardware.nfc" android:required="false" />
    <uses-permission android:name="android.permission.CAMERA" />
    <application ... />
</manifest>

Play Console shows how many devices your declarations exclude, so check it before each release.

Resource tips for real projects

  • Every resource needs a default in the unqualified folder. Without it, devices that match no qualifier fall into a gap, and the build or the app breaks.
  • Do not hard-code user-visible strings in code. Put them in strings.xml so they can be translated and found.
  • Use dp and sp, never raw pixels.
  • Prefer one resource per concept, such as R.color.brand_primary. Do not scatter hex codes.
  • Name resources in lowercase_snake_case. Android Studio's lint will warn about unused resources and missing translations.

Next you will meet the tools you use daily to find bugs.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does android:exported="true" on a component mean?

    exported controls whether other apps can start the component. It should be true only for components designed to be reached from outside, such as the launcher Activity.
  2. 2. Which unit should you use for text sizes so they follow the user's font-size setting?

    sp scales with the user's preferred font size, while dp is for other dimensions. Raw pixels do not adapt to different screens.
  3. 3. A device is Spanish and in dark mode. You have values-es and values-night folders but no values-es-night. Which wins?

    The qualifiers are checked in precedence order, and locale comes before night mode. Create values-es-night if you need both.
  4. 4. What happens to an intent filter that needs an implicit intent's category?

    To match, the intent's action, all of its categories and its data have to be accepted by the filter.

Summary

  • The manifest is read by the system first: it lists components, permissions, features and the launcher entry point.
  • Components with intent filters need android:exported; set it to true only when other apps should reach them.
  • Intent filters describe handled actions, categories and data.
  • Strings, colors and dimens live in res/values; use dp for sizes and sp for text.
  • Qualifiers add variants; the system removes mismatches and narrows by precedence, where locale outranks smallest width, night mode and density. Always provide a default.

Next, see how to run the app and read its output in Running, Debugging and Logcat.