【问题标题】:Kotlin extensions unresolved referencesKotlin 扩展未解析的引用
【发布时间】:2017-06-20 08:38:16
【问题描述】:

我说要学习 kotlin 并尝试通过 kotlin guides 创建一个项目。在其中一个指南中说我可以使用 View 的 ID 代替 findViewById。我已导入kotlin-android-extensions 插件,将其添加到 gradle 文件中^,但仍然无法仅使用 id 获取对象的引用。有人能帮我吗?我正在使用 Android Studio 2.3 和 Kotlin 版本 1.1.2-5。这是我的代码:

主 gradle 文件

buildscript {
ext.kotlin_version = "1.1.2-5"
repositories {
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.2'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"

    }
}

应用 gradle 文件

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

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId ".kotlintodoapp"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:recyclerview-v7:25.2.0'
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:support-v4:25.2.0'
    compile 'com.android.support:design:25.2.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

    compile 'com.pawegio.kandroid:kandroid:0.5.0@aar'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

repositories {
    mavenCentral()
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:a="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    a:id="@+id/parent_layout"
    a:layout_width="match_parent"
    a:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        a:id="@+id/simpleRecyclerView"
        a:layout_width="match_parent"
        a:layout_height="match_parent"/>


</RelativeLayout>

ActivityMain.kt

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        simpleRecyclerView.setFixedSize(true)
    }
}

Android 工作室说 kotlinx.android.synthetic.main.activity_main.* 是未使用的导入,并尝试将 simpleRecyclerView 作为 id 导入。

【问题讨论】:

  • @zsmb13 是的,我安装了 kotlin 插件,在我重新启动 android studio 之前它运行良好。可能是工作室错误吗?
  • 您是否尝试清理或重建项目?

标签: android kotlin


【解决方案1】:

无需在顶级 gradle 文件中添加 kotlin-android-extensions 依赖项。

项目分级配置:

dependencies {
   …
   classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

应用分级配置:

apply plugin: 'kotlin-android-extensions'

此配置应该使您的项目按预期工作。参考:https://kotlinlang.org/docs/tutorials/android-plugin.html

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多