AndroidManifest.xmlのフォーマット — パーミッション、Activity、Intent

Android AndroidManifest.xmlファイルをフォーマットし整理します。マニフェスト構造、パーミッション宣言、Activity/Service/Receiverコンポーネント、Intent Filterの設定を解説します。

Configuration

詳細な説明

AndroidManifest.xmlのフォーマット

AndroidManifest.xmlは、すべてのAndroidアプリケーションに不可欠な設定ファイルです。アプリのコンポーネント、パーミッション、ハードウェア要件などを宣言します。適切にフォーマットされたマニフェストは、アプリの機能を理解するために重要です。

マニフェスト構造

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.myapp">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.CAMERA" />

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

    <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>

    <service android:name=".MyService" />
    <receiver android:name=".MyReceiver" />

  </application>
</manifest>

パーミッション宣言

パーミッションは <application> 要素の前にカテゴリ別にグループ化してリストすべきです:

  • ネットワーク: INTERNETACCESS_NETWORK_STATE
  • ストレージ: READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE
  • ハードウェア: CAMERARECORD_AUDIOACCESS_FINE_LOCATION
  • システム: RECEIVE_BOOT_COMPLETEDVIBRATE

コンポーネントの構成

<application> 内では、一貫性のためにこの順序でコンポーネントを整理します:

  1. Activity — UIスクリーン
  2. Service — バックグラウンド操作
  3. Receiver — ブロードキャストレシーバー
  4. Provider — コンテンツプロバイダー
  5. Meta-data — 追加設定

Intent Filter

Intent Filterは、コンポーネントが処理できる暗黙的Intentを宣言します。適切なインデントにより、action、category、data要素が視覚的に明確になります:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:scheme="https"
        android:host="example.com"
        android:pathPrefix="/link" />
</intent-filter>

Android名前空間

すべてのAndroid固有属性は android: 名前空間プレフィックスを使用します。フォーマットにより、これらの名前空間プレフィックス付き属性が明確に表示され、一貫して整列されます。

ユースケース

Androidマニフェストフォーマットは、コードレビューでパーミッション変更をレビューするモバイル開発者、Intent解決の問題をデバッグする場合、Google Play提出のためのマニフェスト構造の準備(Google Playはマニフェスト構造を検証)、マニフェストマージにより追加されたサードパーティSDKパーミッションの監査に不可欠です。

試してみる — XML Formatter

フルツールを開く