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

添加'tools:replace="android:name"'后Gradle仍构建失败,求解决

问题:Chaquopy集成Flutter时Gradle构建失败(Manifest合并冲突)

背景

在Flutter应用中使用Chaquopy运行Python文件,已解决Python路径/版本、按钮类型等前期问题,现遭遇Gradle构建失败,核心为Manifest合并冲突。

报错详情

C:\Users\user\StudioProjects\untitled\android\app\src\debug\AndroidManifest.xml:5:9-42 Error:
    Attribute application@name value=(android.app.Application) from AndroidManifest.xml:5:9-42
    is also present at [:chaquopy] AndroidManifest.xml:9:18-72 value=(com.chaquo.python.android.PyApplication).
    Suggestion: add 'tools:replace="android:name"' to <application> element at AndroidManifest.xml:3:5-35:19 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : Attribute application@name value=(android.app.Application) from AndroidManifest.xml:5:9-42
    is also present at [:chaquopy] AndroidManifest.xml:9:18-72 value=(com.chaquo.python.android.PyApplication).
    Suggestion: add 'tools:replace="android:name"' to <application> element at AndroidManifest.xml:3:5-35:19 to override.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1m 25s
Exception: Gradle task assembleDebug failed with exit code 1

当前AndroidManifest.xml代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.untitled">
    <application
        android:label="untitled"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        android:name="com.chaquo.python.android.PyApplication".
        <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" />
        tools:replace="android:name"
    </application>
</manifest>

修复方案

1. 修正Manifest语法与结构错误

当前代码存在两处关键错误:

  • android:name="com.chaquo.python.android.PyApplication". 末尾多了英文句号,且该属性写在了<application>标签的外部(标签闭合前的内容区域),属于无效配置。
  • tools:replace="android:name" 未放置在<application>标签的属性列表中,而是放在了标签内部,无法生效。

2. 正确配置Manifest文件

修正后的完整代码如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" <!-- 添加tools命名空间 -->
    package="com.example.untitled">
    <application
        android:label="untitled"
        android:name="com.chaquo.python.android.PyApplication" <!-- 直接指定Chaquopy的Application类 -->
        android:icon="@mipmap/ic_launcher"
        tools:replace="android:name"> <!-- 将replace属性放在application标签内 -->
        <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">
            <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" />
    </application>
</manifest>

3. 额外验证步骤

  • 确认本地Python版本符合Chaquopy要求,安装Python时务必勾选Add Python to PATH选项。
  • 若弹出Python修改安装窗口,完成修复后重启Android Studio/VS Code,确保环境变量生效。
  • 执行flutter clean后重新构建项目,清除缓存文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 00:05:40