import kotlinx.android.synthetic.main.activity_main.* 成为未使用的导入指令

问题描述 投票:0回答:2
import kotlinx.android.synthetic.main.activity_main.*

显示为未使用的导入指令 我的代码如下: `

package com.example.worst_calculator

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

`

几个小时以来,我一直遇到 import kotlinx.android.synthetic.main.activity_main.* 返回未解决的引用错误的问题。网上的解决方案似乎都不适用于我的具体问题。然后,我尝试从导入的项目中应用 gradle.build 模块代码(我也尝试导入 gradle.build 项目代码,但这引发了大量错误),我陷入了当前的困境。

gradle.build代码如下: `

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

plugins {
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}

`

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    namespace 'com.example.worst_calculator'
    compileSdk 32

    defaultConfig {
        applicationId "com.example.worst_calculator"
        minSdk 24
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

``

android xml kotlin gradle import
2个回答
0
投票

此 Gradle 插件已弃用 - 'kotlin-android-extensions'

第 1 步:从插件中删除“Kotlin-android-extensions”。在您的代码中删除所有导入,例如“import kotlinx.android.synthetic”。用于访问用户界面。 如果您的应用使用 Parcelable,您可以使用“kotlin-parcelize”而不是“kotlin-android-extensions”。

第 2 步:如果您的应用使用 Kotlin 合成进行视图绑定, 使用本指南迁移到 Jetpack ViewBinding 'https://developer.android.com/topic/libraries/view-binding'

在 build.gradle 中添加以下代码

   buildFeatures {
       viewBinding true
   }

0
投票

我毫无问题地删除了导入,因此可以安全地删除它

© www.soinside.com 2019 - 2024. All rights reserved.