Error:Attribute application@icon value=(@drawable/ic_launcher)

本文详细解析了在Android开发中遇到的ManifestMerger错误,特别是当Library项目与主项目存在相同属性定义时引发的问题。通过具体的代码示例,展示了如何在AndroidManifest.xml文件中使用tools:replace属性来正确配置,避免编译错误。

今天在导入一个emoji的控件的时候提示Error:(14, 9) Attribute application@icon value=(@drawable/ic_launcher) from AndroidManifest.xml:14:9

百度了一下,这是一个编译的常见错误,主要是AS的Gradle插件默认会启用Manifest Merger Tool,若Library项目中也定义了与主项目相同的属性(例如默认生成的android:icon和android:theme),则此时会合并失败,并报上面的错误。

可惜看的那篇文章的解决方法讲的不清不楚,依然没有搞懂要怎么弄,于是找到另一篇文章中有完整的代码,一目了然。

注意修改的地方在于第2行的

xmlns:tools="http://schemas.android.com/tools"

和application最后一行的

tools:replace="android:icon, android:theme"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.leo.kang.demo"
    android:versionCode="1"
    android:versionName="1.0">
 
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        tools:replace="android:icon, android:theme">

AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\work\\WebView\\app\\src\\main\\res\\mipmap-anydpi-v26\\ic_launcher.xml","position":{"startLine":3}}],"original":"ERROR:C:\\work\\WebView\\app\\src\\main\\res\\mipmap-anydpi-v26\\ic_launcher.xml:4: AAPT: error: resource mipmap/ic_launcher_foreground (aka com.example.webview:mipmap/ic_launcher_foreground) not found.\n ","tool":"AAPT"} AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"C:\\work\\WebView\\app\\src\\main\\res\\mipmap-anydpi-v26\\ic_launcher_round.xml","position":{"startLine":3}}],"original":"ERROR:C:\\work\\WebView\\app\\src\\main\\res\\mipmap-anydpi-v26\\ic_launcher_round.xml:4: AAPT: error: resource mipmap/ic_launcher_foreground (aka com.example.webview:mipmap/ic_launcher_foreground) not found.\n ","tool":"AAPT"} FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processDebugResources'. > A failure occurred while executing com.android.build.gradle.internal.res.LinkApplicationAndroidResourcesTask$TaskAction > Android resource linking failed ERROR:C:\work\WebView\app\src\main\res\mipmap-anydpi-v26\ic_launcher.xml:4: AAPT: error: resource mipmap/ic_launcher_foreground (aka com.example.webview:mipmap/ic_launcher_foreground) not found. ERROR:C:\work\WebView\app\src\main\res\mipmap-anydpi-v26\ic_launcher_round.xml:4: AAPT: error: resource mipmap/ic_launcher_foreground (aka com.example.webview:mipmap/ic_launcher_foreground) not found. * 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 1s 23 actionable tasks: 8 executed, 15 up-to-date
最新发布
11-12
<think>好的,我现在要回答用户关于Android开发中android:icon="@drawable/ic_launcher"的作用的问题。首先,我需要回忆一下AndroidManifest.xml中application标签的属性。记得android:icon是用来指定应用图标的,这个图标会在桌面、应用列表和任务管理器中显示。用户提供的引用里提到了一些错误,比如找不到资源,这可能和图标路径有关。 接下来,我需要确认ic_launcher的位置。通常,这个图标放在res/drawable或res/mipmap目录下。不同版本的Android Studio可能默认路径不同,比如用户引用中提到的mipmap和drawable的冲突。需要解释清楚这两个目录的区别,以及如何正确引用。 然后,用户提到的Manifest合并错误,可能是因为多个模块或依赖库中定义了不同的图标,导致冲突。这时候需要用tools:replace属性来解决,如引用[1]中的例子。需要说明tools命名空间的作用和如何正确使用replace属性。 另外,用户的问题可能还涉及如何更换应用图标,这时候可以引用[5]中的方法,指导如何在Android Studio中生成或替换ic_launcher图片,并确保资源正确放置。 还要注意语法正确,比如在提到路径时使用$...$格式,例如res/$mipmap$或res/$drawable$。同时,独立的问题需要单独成段,使用$$...$$格式,不过这里可能用不上。需要确保回答结构清晰,分点说明用途、资源管理、常见错误及解决方法,最后生成相关问题。 最后,检查是否有引用遗漏,确保每个引用标识都正确对应到用户提供的参考内容。比如引用[1]提到tools:replace的使用,引用[2][3][4]涉及资源找不到的错误,引用[5]关于如何更换图标。这些都需要在回答中自然地引用,帮助用户理解问题来源和解决方法。</think>### android:icon="@drawable/ic_launcher" 的作用解析 在 Android 开发中,`android:icon="@drawable/ic_launcher"` 是 `AndroidManifest.xml` 文件中 `<application>` 标签的重要属性,其作用如下: #### 1. **定义应用图标** - **用途**:该属性指定应用程序的默认图标,图标会显示在设备桌面、应用列表、设置界面及任务管理器中[^3][^4]。 - **资源路径**:`@drawable/ic_launcher` 表示图标文件存储在 `res/drawable` 或 `res/mipmap` 目录下。现代 Android 项目默认使用 `mipmap` 目录存储不同分辨率的图标(如 `mipmap-hdpi`、`mipmap-xhdpi`),以适配不同屏幕密度[^5]。 #### 2. **资源冲突与路径管理** - **常见错误**:若图标文件未正确放置或命名,会导致编译错误,例如: ```Manifest merger failed: Attribute application@icon value=(@mipmap/ic_launcher)```[^2] 或 ```No resource found that matches the given name (at 'icon' with value '@drawable/ic_launcher')```[^3]。 - **解决方法**: 1. 确认图标文件存在且路径正确(如 `res/mipmap/ic_launcher.png`)。 2. 使用 `tools:replace="android:icon"` 覆盖依赖库中的图标定义[^1][^4]。 #### 3. **多模块与依赖库的图标合并** - 当项目包含多个模块或第三方库时,若多个 `AndroidManifest.xml` 文件定义了 `android:icon` 属性,需通过合并规则解决冲突。例如: ```xml <application android:icon="@mipmap/ic_launcher" tools:replace="android:icon"/> ``` 通过 `tools:replace` 显式指定使用当前模块的图标[^1][^2]。 #### 4. **图标生成与替换** - 在 Android Studio 中,可通过右键 `res` 目录选择 **New → Image Asset** 生成适配不同分辨率的 `ic_launcher` 图标,系统会自动更新 `mipmap` 资源[^5]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值