技术小黑屋

Kotlin Kapt Error: Identifier Expected 问题记录

在工作中,小伙伴说他遇到了一个 kapt的问题,在使用 gradle 构建的时候出现了如下的错误。

1
2
3
4
5
6
7
Kapt worker classpath: []
/Users/androidyue/Documents/SecooComponentMaster/module-xxxx/build/tmp/kapt3/stubs/debug/com/xxxxx/xxxxx/util/PlayerCommandStateManager.java:7: error: <identifier> expected
    private static final java.util.Map<java.lang.String, com.xxxxxx.xxxxxxx.util.const.PlayerCommandState> recordMap = null;
                                                                                    ^/Users/androidyue/Documents/xxxxxxx/module-xxxxxx/build/tmp/kapt3/stubs/debug/com/xxxxx/xxxxxx/util/PlayerCommandStateManager.java:7: error: <identifier> expected
    private static final java.util.Map<java.lang.String, com.xxxxxx.xxxxxxxx.util.const.PlayerCommandState> recordMap = null;
                                                                                                            ^/Users/androidyue/Documents/xxxxxxx/module-xxxxxx/build/tmp/kapt3/stubs/debug/com/xxxxx/xxxxxxx/util/PlayerCommandStateManager.java:7: error: <identifier> expected
    private static final java.util.Map<java.lang.String, com.xxxxxx.xxxxxxx.util.const.PlayerCommandState> recordMap = null;

对应的实际代码为

1
2
3
object PlayerCommandStateManager {
    private val recordMap = mutableMapOf<String, PlayerCommandState>()
}
1
2
3
4
5
6
7
8
9
10
package com.secoo.gooddetails.util.const

enum class PlayerCommandState {
    PAUSE_BY_USER,
    PAUSE_NORMAL,
    PLAY,
    INITIAL,
    ENDED,
    IDLE
}

奇怪的解决方法

  • PlayerCommandState所在的包名由com.secoo.gooddetails.util.const,修改为其他不包含const的包名即可正常编译。

原因推测

  • 可能是kapt的识别包名的一些bug。可能的相关链接 https://youtrack.jetbrains.com/issue/KT-16153

环境信息

1
2
3
4
5
6
7
8
9
10
11
12
------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------

Build time:   2019-04-26 08:14:42 UTC
Revision:     261d171646b36a6a2xxxxxxx4c19d

Kotlin:       1.3.21
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_281 (Oracle Corporation 25.281-b09)
OS:           Mac OS X 10.16 x86_64




快来解锁最新版 Typora,新用户券后仅需 84 元!
如何便宜的购买 office 365 ?
新版赤友 NTFS 助手来袭,正版超低价