简体   繁体   English

如何让 Android Studio 的 Java 编译器识别 Kotlin class?

[英]How to get Android Studio's Java compiler to recognize a Kotlin class?

I added a Kotlin file that contains three classes to an Android Studio project originally written in Java. The file compiles fine within Android Studio, and the project builds until I try to get a Java class to interact with the Kotlin class. Android Studio gives me the following error: I added a Kotlin file that contains three classes to an Android Studio project originally written in Java. The file compiles fine within Android Studio, and the project builds until I try to get a Java class to interact with the Kotlin class. Android Studio gives me以下错误:

Java compiler:
   [path]/[project]
      app/src/main/java
         [project].jav
            error: cannot find symbol class Quiz_abTime

When I added the file, Android Studio complained about gradle configuration, but I ran the configuration and it says that all modules that use Kotlin are configured.当我添加文件时,Android Studio 抱怨 gradle 配置,但我运行了配置,它说所有使用 Kotlin 的模块都已配置。

Note that I have already tried invalidating the cache and restarting Android Studio, so that isn't the problem.请注意,我已经尝试使缓存无效并重新启动 Android Studio,所以这不是问题所在。

Here are my build.gradle files.这是我的build.gradle文件。 The module first:首先是模块:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.61'
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

Now the app's build.gradle :现在应用程序的build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 26

    defaultConfig {
        applicationId [application name deleted for SO]
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 6
        versionName "2.0.5"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:support-vector-drawable:26.1.0'
    implementation 'com.android.support:appcompat-v7:26.1.0' // where X.X.X version
    implementation 'com.github.yukuku:ambilwarna:2.0.1'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
    mavenCentral()
}

Here's another issue that I include only because it may be related.这是我包含的另一个问题,只是因为它可能相关。 In my main application, I have the following:在我的主要应用程序中,我有以下内容:

import android.support.v7.app.ActionBar;

Curiously, ActionBar is highlighted in red, and it says it Cannot resolve symbol ActionBar`.奇怪的是, ActionBar以红色突出显示,并表示Cannot resolve symbol ActionBar`。 There are several others highligted like this;还有其他几个像这样突出显示; this started after Android Studio asked for an upgrade.这是在 Android Studio 要求升级后开始的。 However, at least this builds;但是,至少可以构建; building fails only when I try to access the Kotlin class from a Java class.只有当我尝试从 Java class 访问 Kotlin class 时,构建才会失败。

I've done Kotlin with Java before in Android Studio and this wasn't a problem, but it Can anyone tell me where to look for issues that are preventing the Java compiler from seeing the Kotlin class?我之前在 Android Studio 中完成了 Kotlin 和 Java,这不是问题,但是谁能告诉我在哪里可以找到阻止 Java 编译器看到 Kotlin class 的问题?

In my case, I forgot to add a dependency to my Java project.就我而言,我忘记向我的 Java 项目添加依赖项。 The compiler was complaining about Kotlin files.编译器抱怨 Kotlin 个文件。

implementation 'androidx.core:core-ktx:1.9.0'

In this case it turns out that Android Studio didn't prepend the Kotlin file with an important line: 在这种情况下,事实证明Android Studio并没有在Kotlin文件中添加重要的一行:

package [packagename]

The moment I added that, it compiled and ran beautifully. 我添加的那一刻,它编译并运行精美。 I thought this was something AS would do automatically, but apparently not -- or at least not in this case, for whatever reason. 我认为这是AS会自动执行的操作,但显然不会-或至少在这种情况下,无论出于何种原因。 The issues with other symbols it can't find are apparently unrelated. 找不到其他符号的问题显然无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM