【问题标题】:error: cannot find symbol class for add kotlin class into java class in android studio 3.0 stable错误:在 android studio 3.0 stable 中找不到符号类以将 kotlin 类添加到 java 类中
【发布时间】:2017-11-07 19:33:10
【问题描述】:

我使用 android studio 3.0 和一些旧的 java 类将 Java 转换为 Kotlin。之后 Kotlin 类无法导入 java 类! 在下面你可以看到我的 gradle 和我的错误图片。

模块构建等级

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.google.gms:google-services:3.1.0'

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

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

在应用程序级别 build.gradle 中,我通常使用 gradle 代码,而在 android 3.0 中,我认为我们需要任何东西

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.idehnavazan.beautifierclient"
        minSdkVersion 14
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    dataBinding {
        enabled = true
    }


}
repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://maven.google.com' }
    maven { url 'https://jitpack.io' }


}
dependencies {
    compile 'com.android.support:multidex:1.0.1'  
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.github.rey5137:material:1.2.2'
    compile 'com.android.support:cardview-v7:25.3.1'

}
apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • 这不是问题...
  • 我添加了一些细节。希望对你有帮助
  • 无关:这不是您创建片段的方式。只需调用构造函数CategoryFragment() 而不是instantiate

标签: gradle kotlin android-studio-3.0


【解决方案1】:

要使用 Kotlin 文件,您需要将 Kotlin 添加到您的项目中。

project/build.gradle

buildscript {
    ext.kotlinVersion = '1.1.51'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

项目/模块/build.gradle

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

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}

接下来是什么

由于您使用的是支持库 25.3.1 和 Android 插件 3.0.0,您的下一个问题可能是:style attribute '@android:attr/windowEnterAnimation' not found

【讨论】:

    【解决方案2】:

    我已将所有 kotlin 类添加到单独的文件夹中,它对我有用! 将您的 java 类与 kotlin 分开。我有

    app:compileDebugJavaWithJavac Kotlin class calling from java:  error: cannot find symbol 
    

    编译时错误。

    【讨论】:

      【解决方案3】:

      我的问题是我使用导入了一个子模块 testImplementation 而不是 implementation 在模块 gradle 文件中,它将您的模块声明为依赖项。

      更改依赖声明为我修复了它。

      【讨论】:

        【解决方案4】:

        我也是 kotlin/android 的新手,并且正在使用 ViewBinding。发生此类错误时,我正在重命名 xml 文件。我刚刚补充了:

        dataBinding {
                enabled = true
            }
        

        到 build.gradle(module) 文件,它现在可以工作了。老实说,我从来不明白视图绑定和数据绑定的区别

        【讨论】:

          猜你喜欢
          • 2023-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-14
          • 2016-03-02
          • 2017-03-01
          • 2017-11-17
          相关资源
          最近更新 更多