你瞅啥呢

2025-04-18 flutter示例文件(AndroidManifest.xml)代码解析(deepseek)

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:label="test0408"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
    <!-- Required to query activities that can process text, see:
         https://developer.android.com/training/package-visibility?hl=en and
         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

这是 Flutter 项目的 Android 清单文件(AndroidManifest.xml),它定义了 Android 应用的基本配置、权限、入口 Activity 等信息。以下是详细解析:


1. 根节点 <manifest>

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  • 作用:声明 Android 应用的命名空间和基础配置。

  • 关键属性

    • xmlns:android:定义 Android 的 XML 命名空间。


2. <application> 节点

<application
    android:label="test0408"              // 应用名称(显示在桌面)
    android:name="${applicationName}"    // 应用的 Application 类(Flutter 自动生成)
    android:icon="@mipmap/ic_launcher">  // 应用图标
  • 作用:定义应用的全局配置。

  • 关键属性

    • android:label:应用名称(用户可见)。

    • android:name:自定义的 Application 类(Flutter 默认使用 io.flutter.app.FlutterApplication)。

    • android:icon:应用图标(@mipmap/ic_launcher 指向资源文件)。


3. <activity> 节点(主入口)

<activity
    android:name=".MainActivity"         // Activity 类名
    android:exported="true"              // 允许外部调用
    android:launchMode="singleTop"       // 启动模式(防止重复实例化)
    android:theme="@style/LaunchTheme"   // 启动页主题
    android:configChanges="..."          // 配置变更时自行处理(避免重启 Activity)
    android:hardwareAccelerated="true"   // 启用硬件加速
    android:windowSoftInputMode="adjustResize">  // 键盘弹出时调整布局
  • 作用:定义应用的入口 Activity(Flutter 的 UI 容器)。

  • 关键属性

    • android:exported="true":允许其他应用启动该 Activity(必需)。

    • android:launchMode="singleTop":如果 Activity 已在栈顶,则复用实例。

    • android:theme="@style/LaunchTheme":启动时的临时主题(避免白屏)。

    • android:configChanges:指定配置变更(如屏幕旋转)时由应用自行处理,而非重启 Activity。

    • android:windowSoftInputMode="adjustResize":键盘弹出时调整布局(避免遮挡输入框)。


子节点 <meta-data>(Flutter 主题)

<meta-data
    android:name="io.flutter.embedding.android.NormalTheme"
    android:resource="@style/NormalTheme" />
  • 作用:指定 Flutter UI 正常启动后的主题(覆盖 LaunchTheme)。


子节点 <intent-filter>(应用启动器)

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
  • 作用:声明该 Activity 是应用的主入口,并显示在桌面启动器中。


4. 其他 <meta-data>(Flutter 插件注册)

<meta-data
    android:name="flutterEmbedding"
    android:value="2" />
  • 作用:告诉 Flutter 使用 v2 版本的插件嵌入方式(新版本 Flutter 默认)。


5. <queries> 节点(权限声明)

<queries>
    <intent>
        <action android:name="android.intent.action.PROCESS_TEXT" />
        <data android:mimeType="text/plain" />
    </intent>
</queries>
  • 作用:声明应用需要查询或处理文本的权限(例如复制粘贴、文本选择)。

  • 背景:从 Android 11 开始,某些隐式 Intent 需要显式声明 <queries>


关键点总结

  1. 应用入口MainActivity 是 Flutter 的容器,通过 intent-filter 定义为启动器。

  2. 主题机制

    • LaunchTheme:启动时的临时主题(避免白屏)。

    • NormalTheme:Flutter UI 加载后的主题。

  3. Flutter 集成

    • flutterEmbedding=2 表示使用新版插件系统。

    • configChanges 确保 Flutter 正确处理配置变更(如旋转屏幕)。

  4. 兼容性<queries> 用于适配 Android 11+ 的包可见性限制。

posted @ 2025-04-18 10:29  叶乘风  阅读(95)  评论(0)    收藏  举报