You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何保留依赖包并移除其自动添加的不必要权限?

解决Flutter项目中多余权限问题

项目依赖

qr_code_scanner_plus: ^2.0.6
google_mobile_ads: ^5.2.0
provider: ^6.1.2
google_maps_flutter: ^2.9.0
wifi_iot: ^0.3.19+1
permission_handler: ^11.3.1

当前AndroidManifest.xml内容

<manifest xmlns:android="http://schemas.android.com/apk/res/android">    
    <application        
        android:label="QR Scanner"        
        android:name="${applicationName}"        
        android:icon="@mipmap/ic_launcher">        
        <meta-data android:name="com.google.android.geo.API_KEY"            
            android:value="xxxxxxxxxxx_xxxxxxxxxxxxxxx_xxxxxxxxxxx"/>        
        <activity            
            android:name="io.flutter.embedding.android.FlutterActivity"            
            android:exported="true"            
            android:launchMode="singleTop"            
            android:taskAffinity=""            
            android:theme="@style/LaunchTheme"            
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"            
            android:hardwareAccelerated="true"            
            android:windowSoftInputMode="adjustResize">            
            <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>        
        <meta-data            
            android:name="flutterEmbedding"            
            android:value="2" />        
        <meta-data            
            android:name="com.google.android.gms.ads.APPLICATION_ID"            
            android:value="ca-app-pub-XXXXXXXXXXXXXXXX~XXXXXXXXXX"/>        
        <service            
            android:name=".MyTileService"            
            android:icon="@drawable/flutter"            
            android:label="QR Scanner"            
            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"            
            android:exported="true">            
            <intent-filter>                
                <action android:name="android.service.quicksettings.action.QS_TILE"/>            
            </intent-filter>       
        </service>    
    </application>    
    <queries>        
        <intent>            
            <action android:name="android.intent.action.PROCESS_TEXT"/>            
            <data android:mimeType="text/plain"/>        
        </intent>    
    </queries>
</manifest>

问题分析

  • qr_code_scanner_plus 依赖自动引入相机权限(必要,保留)
  • wifi_iot 依赖自动引入Wi-Fi相关权限(必要,保留)
  • google_maps_flutter 默认引入定位权限(仅用作地点选择器,无需该权限,需移除)

解决方案

1. 添加tools命名空间

在manifest根标签中加入tools命名空间,用于控制权限合并规则:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

2. 移除多余定位权限

在application标签外添加权限移除声明,强制删除google_maps_flutter引入的定位权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove"/>

3. 完整修改后的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">    
    <application        
        android:label="QR Scanner"        
        android:name="${applicationName}"        
        android:icon="@mipmap/ic_launcher">        
        <meta-data android:name="com.google.android.geo.API_KEY"            
            android:value="xxxxxxxxxxx_xxxxxxxxxxxxxxx_xxxxxxxxxxx"/>        
        <activity            
            android:name="io.flutter.embedding.android.FlutterActivity"            
            android:exported="true"            
            android:launchMode="singleTop"            
            android:taskAffinity=""            
            android:theme="@style/LaunchTheme"            
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"            
            android:hardwareAccelerated="true"            
            android:windowSoftInputMode="adjustResize">            
            <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>        
        <meta-data            
            android:name="flutterEmbedding"            
            android:value="2" />        
        <meta-data            
            android:name="com.google.android.gms.ads.APPLICATION_ID"            
            android:value="ca-app-pub-XXXXXXXXXXXXXXXX~XXXXXXXXXX"/>        
        <service            
            android:name=".MyTileService"            
            android:icon="@drawable/flutter"            
            android:label="QR Scanner"            
            android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"            
            android:exported="true">            
            <intent-filter>                
                <action android:name="android.service.quicksettings.action.QS_TILE"/>            
            </intent-filter>       
        </service>    
    </application>    
    <!-- 移除不需要的定位权限 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove"/>
    
    <queries>        
        <intent>            
            <action android:name="android.intent.action.PROCESS_TEXT"/>            
            <data android:mimeType="text/plain"/>        
        </intent>    
    </queries>
</manifest>

原理说明

Android构建过程中会自动合并所有依赖库的Manifest文件,tools:node="remove" 会强制将指定权限从最终的Manifest中移除,覆盖依赖库添加的权限声明。

内容的提问来源于stack exchange,提问作者Clipboard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:55:20