Android Studio bug - attribute 'android:versionCode' not found

本文提供两种解决Android Studio Canary版中出现的AndroidManifest.xml编辑bug的方法。第一种为清理并重建项目;第二种则是通过调整设置避免bug再次发生,但可能增加编译时间。

在Android Studio Canary出现了编辑bug - AndroidManifest.xml:2 attribute ‘android:versionCode’ not found

解决方案 -:

  1. Clean Project
  2. Build Project
  3. Reload Android Studio

解决方案二 : 关闭掉Install Run

  1. 打开settings
  2. 搜索run
  3. 将右边复选框取消

这里写图片描述
4. 点击 Apply
5. 点击 Ok

对于,方案一虽然会解决,但是以后还会复发。方案二最有效,可以保证以后不复发,但是每次都会编译项目,会导致运行时间过长。

希望Google尽快解决这个bug,好烦~~~~

你遇到的错误: ``` Android resource linking failed com.example.bus.app-main-51:/drawable/ic_subway.xml:9: error: attribute android:cx not found. com.example.bus.app-main-51:/drawable/ic_subway.xml:9: error: attribute android:cy not found. com.example.bus.app-main-51:/drawable/ic_subway.xml:9: error: attribute android:r not found. ... error: failed linking file resources. ``` --- ## ❌ 问题原因:**你在 `<vector>` 中误用了圆形(Circle)标签属性** 这些属性 `android:cx`, `android:cy`, `android:r` 是用于 **`<shape>` 类型为 `oval` 的 drawable XML**,比如: ```xml <!-- ✅ 这是 shape drawable(.xml 放在 res/drawable/ 下) --> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FF0000" /> <size android:width="48dp" android:height="48dp" /> </shape> ``` 但你现在是放在 **`res/drawable/ic_subway.xml`** 并且用的是 **VectorDrawable(即 `<vector>` 标签)**,而 `<vector>` 不支持 `cx/cy/r`! 所以编译器报错:“找不到这些属性” —— 因为它们根本不在这个命名空间中。 --- ## ✅ 正确做法:使用合法的 VectorDrawable 结构 ### 🚫 错误示例(你现在的 `ic_subway.xml` 可能长这样): ```xml <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24"> <!-- ❌ 错误!cx/cy/r 不属于 <vector> 的子元素 --> <circle android:cx="12" android:cy="12" android:r="10" android:fillColor="#0000FF" /> <path android:pathData="M12,6 L12,18 M8,10 L12,6 L16,10" android:strokeColor="#FFFFFF" android:strokeWidth="2"/> </vector> ``` ⚠️ `<circle>` 标签 **不是 VectorDrawable 的合法标签!** --- ## ✅ 正确写法:用 `<path>` 来画圆或图形 你需要将圆形转换成一个等效的 `<path>` 数据。 ### ✅ 正确的 `ic_subway.xml` 示例(地铁图标) ```xml <!-- res/drawable/ic_subway.xml --> <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" android:tint="?attr/colorOnPrimary"> <!-- 背景圆圈:通过 path 绘制一个圆形 --> <path android:fillColor="#0000FF" android:pathData="M12,2C7.03,2 3,6.03 3,11c0,4.97 4.03,9 9,9s9,-4.03 9,-9c0,-4.97 -4.03,-9 -9,-9zM12,20c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7s7,3.13 7,7s-3.13,7 -7,7z" /> <!-- 地铁图形 --> <path android:fillColor="#FFFFFF" android:pathData="M8,10 L16,10 L14,14 L10,14 L8,10 Z" /> <!-- 底部连接线 --> <path android:strokeColor="#0000FF" android:strokeWidth="2" android:pathData="M12,18 L12,20" /> </vector> ``` --- ### 🔍 解释一下关键点: | 部分 | 说明 | |------|------| | 第一个 `<path>` | 画一个实心蓝圈(类似地铁标志背景),使用 `pathData` 描述圆形轮廓 | | 第二个 `<path>` | 白色车厢形状(矩形+三角) | | 第三个 `<path>` | 向下延伸的轨道线 | > 💡 提示:你可以用工具生成矢量图路径,比如 [Android Asset Studio](https://romannurik.github.io/AndroidAssetStudio/icons-launcher.html) 或设计软件导出 SVG 再转 Android Vector。 --- ## ✅ 如何避免这类错误? ### ✅ 方法一:只使用支持的标签 在 `<vector>` 中只能包含以下标签: - `<path>` - `<group>` - `<clip-path>` 👉 完整文档参考:[VectorDrawable 官方文档](https://developer.android.com/guide/topics/graphics/vector-drawable-resources) --- ### ✅ 方法二:使用 Android Studio 创建 Vector Asset 1. 右键 `res/drawable` → **New → Vector Asset** 2. 选择内置图标(如 `subway`)或导入 SVG 3. 自动生成合法的 `<vector>` XML 这样就不会写错语法。 --- ### ✅ 方法三:检查文件扩展名和根标签匹配 | 文件名 | 应该使用的根标签 | |--------|----------------| | `ic_subway.xml` → `<vector>` | ✅ 必须是 VectorDrawable 结构 | | `bg_circle.xml` → `<shape>` | ✅ 只能在普通 Drawable 中使用 `cx/cy/r` | ⚠️ 切记: > **`<circle android:cx=...>` 只能在 `<shape android:shape="oval">` 中使用,不能出现在 `<vector>` 里!** --- ## ✅ 其他建议:给你的两个图标统一风格 你也应该检查 `ic_location.xml` 是否也用了非法标签。推荐一个标准定位图标: ### ✅ `res/drawable/ic_location.xml` ```xml <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24"> <path android:fillColor="#FF0000" android:pathData="M12,2 C8,2 4,6 6,12c2,6 6,12 6,12s4,-6 6,-12c2,-6 -4,-10 -8,-10z"/> <circle android:fillColor="#FFFFFF" android:cx="12" android:cy="12" android:r="3"/> </vector> ``` ❌ 等等!这里也有 `<circle>`?是不是错了? ✅ **不!这是对的!** 因为这个 `<circle>` 是 **作为 `<path>` 的简写形式存在的**,Android 支持在 `<vector>` 中使用: ```xml <group> <path ... /> <circle android:cx="" android:cy="" android:r="" android:fillColor="" /> </group> ``` 但是注意:这其实是 **AAPT 编译时自动转换为 pathData 的语法糖**,必须满足格式要求。 不过为了安全起见,建议还是全部用 `pathData`。 --- ## ✅ 最终修复步骤总结 ### 🔧 操作流程: 1. 打开 `res/drawable/ic_subway.xml` 2. 删除所有含 `cx`, `cy`, `r` 的 `<circle>` 标签 3. 替换为合法的 `<path>` 描述图形(可用上面提供的代码) 4. 保存文件 5. 清理项目: - **Build → Clean Project** - **Build → Rebuild Project** ✅ 错误应消失,资源链接成功。 --- ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值