287 lines
5.9 KiB
Markdown
287 lines
5.9 KiB
Markdown
# 语音插件 Kotlin 保留字冲突修复
|
||
|
||
## ❌ **错误原因**
|
||
|
||
云端打包失败,报错:
|
||
```
|
||
Cannot find a parameter with this name: error
|
||
at index.kt:119:95, 123:95, 239:75, 261:71
|
||
```
|
||
|
||
**根本原因:** `error` 是 Kotlin 的保留字,不能用作对象属性名。
|
||
|
||
---
|
||
|
||
## 🔍 **问题分析**
|
||
|
||
### **插件代码中的问题**
|
||
|
||
在 `xwq-speech-to-text` 插件的 UTS 代码中使用了:
|
||
|
||
```typescript
|
||
data: {
|
||
text: '',
|
||
error: '识别错误' // ❌ error 是 Kotlin 保留字
|
||
}
|
||
```
|
||
|
||
UTS 编译为 Kotlin 时,`error` 作为对象属性名会导致编译错误。
|
||
|
||
---
|
||
|
||
## ✅ **解决方案**
|
||
|
||
将所有 `error` 属性改为 `errorMsg`。
|
||
|
||
---
|
||
|
||
## 📝 **修改内容**
|
||
|
||
### **1. 插件代码修改**
|
||
|
||
**文件:** `uni_modules/xwq-speech-to-text/utssdk/app-android/index.uts`
|
||
|
||
#### **修改1:onError 方法(第 73 行)**
|
||
|
||
```typescript
|
||
// 修改前
|
||
data: {
|
||
text: '',
|
||
error: '识别错误: ' + (e.message ?? '未知错误')
|
||
}
|
||
|
||
// 修改后
|
||
data: {
|
||
text: '',
|
||
errorMsg: '识别错误: ' + (e.message ?? '未知错误')
|
||
}
|
||
```
|
||
|
||
#### **修改2:onTimeout 方法(第 85 行)**
|
||
|
||
```typescript
|
||
// 修改前
|
||
data: {
|
||
text: '',
|
||
error: '识别超时'
|
||
}
|
||
|
||
// 修改后
|
||
data: {
|
||
text: '',
|
||
errorMsg: '识别超时'
|
||
}
|
||
```
|
||
|
||
#### **修改3:startSpeechVoice 方法 - 模型路径检查(第 290 行)**
|
||
|
||
```typescript
|
||
// 修改前
|
||
data: {
|
||
text: '',
|
||
error: '模型未初始化,请重新加载'
|
||
}
|
||
|
||
// 修改后
|
||
data: {
|
||
text: '',
|
||
errorMsg: '模型未初始化,请重新加载'
|
||
}
|
||
```
|
||
|
||
#### **修改4:startSpeechVoice 方法 - catch 块(第 322 行)**
|
||
|
||
```typescript
|
||
// 修改前
|
||
data: {
|
||
text: '',
|
||
error: '语音识别启动失败: ' + (e.message ?? '未知错误')
|
||
}
|
||
|
||
// 修改后
|
||
data: {
|
||
text: '',
|
||
errorMsg: '语音识别启动失败: ' + (e.message ?? '未知错误')
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
### **2. 前端代码修改**
|
||
|
||
**文件:** `fronted_uniapp/pages/speech/speech.vue`
|
||
|
||
#### **修改:handleStart 方法中的错误处理(第 517-522 行)**
|
||
|
||
```javascript
|
||
// 修改前
|
||
if (res && res.code === -1) {
|
||
console.error('[Speech] 识别出错:', res.data?.error)
|
||
self.isRecording = false
|
||
self.statusText = '识别出错'
|
||
self.debugInfo = '错误: ' + (res.data?.error || '未知错误')
|
||
self.stopAutoScroll()
|
||
uni.showToast({ title: res.data?.error || '语音识别出错', icon: 'none', duration: 2000 })
|
||
return
|
||
}
|
||
|
||
// 修改后
|
||
if (res && res.code === -1) {
|
||
console.error('[Speech] 识别出错:', res.data?.errorMsg)
|
||
self.isRecording = false
|
||
self.statusText = '识别出错'
|
||
self.debugInfo = '错误: ' + (res.data?.errorMsg || '未知错误')
|
||
self.stopAutoScroll()
|
||
uni.showToast({ title: res.data?.errorMsg || '语音识别出错', icon: 'none', duration: 2000 })
|
||
return
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 📋 **修改文件清单**
|
||
|
||
1. ✅ `uni_modules/xwq-speech-to-text/utssdk/app-android/index.uts`
|
||
- 修改了 4 处 `error` 为 `errorMsg`
|
||
|
||
2. ✅ `fronted_uniapp/pages/speech/speech.vue`
|
||
- 修改了 3 处 `res.data?.error` 为 `res.data?.errorMsg`
|
||
|
||
---
|
||
|
||
## 🚀 **重新打包步骤**
|
||
|
||
### **步骤1:清理缓存**
|
||
|
||
在 HBuilderX 中:
|
||
```
|
||
运行 → 清理项目缓存
|
||
```
|
||
|
||
### **步骤2:重新打包**
|
||
|
||
```
|
||
发行 → 原生 App-云打包
|
||
选择 Android
|
||
等待打包完成(约 5-10 分钟)
|
||
```
|
||
|
||
### **步骤3:验证**
|
||
|
||
1. 下载并安装 APK
|
||
2. 打开语音测评页面
|
||
3. 测试语音识别功能
|
||
4. 测试错误提示(故意触发错误)
|
||
|
||
---
|
||
|
||
## 📊 **Kotlin 保留字列表**
|
||
|
||
以下是常见的 Kotlin 保留字,不能用作属性名:
|
||
|
||
```
|
||
as, break, class, continue, do, else, false, for, fun,
|
||
if, in, interface, is, null, object, package, return,
|
||
super, this, throw, true, try, typealias, typeof, val,
|
||
var, when, while, error, suspend, sealed, etc.
|
||
```
|
||
|
||
**避免方法:**
|
||
- 使用描述性名称:`errorMsg`、`errorMessage`、`errorText`
|
||
- 使用下划线:`_error`(不推荐)
|
||
- 使用驼峰命名:`errorInfo`
|
||
|
||
---
|
||
|
||
## ⚠️ **注意事项**
|
||
|
||
### **1. 插件更新**
|
||
|
||
如果插件有更新:
|
||
- 检查新版本是否修复了此问题
|
||
- 如果没修复,需要重新应用本修改
|
||
|
||
### **2. 其他可能的保留字冲突**
|
||
|
||
检查代码中是否使用了其他 Kotlin 保留字:
|
||
```bash
|
||
# 搜索常见保留字
|
||
grep -r ":\s*val\s*:" uni_modules/
|
||
grep -r ":\s*var\s*:" uni_modules/
|
||
grep -r ":\s*fun\s*:" uni_modules/
|
||
```
|
||
|
||
### **3. 文档更新**
|
||
|
||
如果你的项目有 API 文档,记得更新:
|
||
- 回调参数从 `error` 改为 `errorMsg`
|
||
- 错误处理示例代码
|
||
|
||
---
|
||
|
||
## 🐛 **常见问题**
|
||
|
||
### **Q1:打包还是失败怎么办?**
|
||
|
||
**检查:**
|
||
1. 确认所有文件都已保存
|
||
2. 清理项目缓存
|
||
3. 查看完整的错误日志
|
||
4. 检查是否有其他编译错误
|
||
|
||
### **Q2:如何验证修改是否生效?**
|
||
|
||
```bash
|
||
# 搜索确认没有遗漏
|
||
grep -r "error:" uni_modules/xwq-speech-to-text/
|
||
grep -r "\.error" fronted_uniapp/pages/speech/
|
||
```
|
||
|
||
应该没有结果,或只有注释中的。
|
||
|
||
### **Q3:本地运行正常,打包失败?**
|
||
|
||
**原因:** 本地运行使用标准基座,不编译 UTS 插件。
|
||
云端打包会编译 UTS 为 Kotlin,才会发现此问题。
|
||
|
||
---
|
||
|
||
## ✅ **验收标准**
|
||
|
||
- [ ] 云端打包成功(无编译错误)
|
||
- [ ] APK 安装成功
|
||
- [ ] 语音识别功能正常
|
||
- [ ] 错误提示正确显示
|
||
- [ ] 无闪退问题
|
||
|
||
---
|
||
|
||
## 📚 **相关文档**
|
||
|
||
1. Kotlin 语言规范:https://kotlinlang.org/spec/
|
||
2. UTS 开发文档:https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html
|
||
3. 云端打包说明:https://uniapp.dcloud.net.cn/tutorial/run/
|
||
|
||
---
|
||
|
||
## ✅ **总结**
|
||
|
||
### **核心问题**
|
||
使用 Kotlin 保留字 `error` 作为对象属性名导致编译失败。
|
||
|
||
### **解决方法**
|
||
将所有 `error` 改为 `errorMsg`。
|
||
|
||
### **影响范围**
|
||
- 插件代码:4 处修改
|
||
- 前端代码:3 处修改
|
||
|
||
### **后续建议**
|
||
- 命名时避免使用保留字
|
||
- 使用更描述性的名称
|
||
- 考虑向插件作者反馈此问题
|
||
|
||
---
|
||
|
||
**现在重新打包应该可以成功了!** 🎯
|