我有一个带有Java代码的android项目,并向其中添加了Kotlin支持。当我尝试使用View或Context类中的某些方法或常量时,收到Kotlin编译器错误'Unresolved reference'。问题的最奇怪的部分是我可以成功使用 View.VISIBLE,但在尝试使用View.SYSTEM_UI_FLAG_FULLSCREEN时收到编译器错误。
Kotlin在项目级别的build.gradle中设置
buildscript {
ext.kotlin_version = "1.3.11"
repositories {
google()
maven { url 'https://maven.fabric.io/public' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'io.fabric.tools:gradle:1.+'
classpath 'org.slf4j:slf4j-api:1.7.21'
classpath 'com.github.tony19:logback-android-core:1.1.1-4'
classpath 'com.github.tony19:logback-android-classic:1.1.1-4'
classpath 'com.papertrailapp:logback-syslog4j:1.0.0'
classpath 'com.google.gms:google-services:4.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Kotlin在应用程序级别的build.gradle中设置。
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
apply plugin: 'com.google.gms.google-services'
/* other declarations */
dependencies {
/* other dependencies */
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version"
}
当我尝试编译下一个代码时
fun foo() {
View.SYSTEM_UI_FLAG_FULLSCREEN
}
我收到
Unresolved reference: SYSTEM_UI_FLAG_FULLSCREEN
但是我可以成功编译
fun foo() {
View.VISIBLE
}
而且,我无法编译
fun foo(context: Context) {
context.getExternalFilesDir("Dir")
}
未解决的参考:getExternalFilesDir
感谢帮助。