无法导入 android.support.v7 (appcompat) - 未解决的库

Can't import android.support.v7 (appcompat) - Unresolved Library

我试图将此支持库导入到我的项目中,但无法正常工作。

首先我找不到它。当我点击 Dependencies 选项卡上的 + 按钮并搜索它时。我找到了 android.support.v4,但我也需要 v7。 我在某处读到该库可能已被弃用。

所以在我的代码中它无法导入 android.support。v7.widget.ShareActionProvider v7 是红色的,因为它无法解析。 我按照有关如何添加库的说明进行操作。但是还是不能解决问题。我执行了正常步骤,如清理、同步 gradle 文件、使链接无效等...

这是我在 mainactivity 中的一些代码

    import android.content.Intent
    import android.os.Bundle
    import android.support.v4.view.MenuItemCompat
    import android.view.Menu
    import android.view.MenuItem
    //import androidx.appcompat.widget.ShareActionProvider
    import androidx.appcompat.widget.Toolbar

    //import androidx.core.view.MenuItemCompat
    //import android.support.v4.view.MenuItemCompat
    //import androidx.core.view.MenuItemCompat.getActionProvider

    import android.support.v7.app.AppCompatActivity
    import android.support.v7.widget.ShareActionProvider
    //import android.widget.ShareActionProvider



    class MainActivity : AppCompatActivity() {
        private var shareActionProvider : ShareActionProvider? = null

    import android.content.Intent
    import android.os.Bundle
    import android.support.v4.view.MenuItemCompat
    import android.view.Menu
    import android.view.MenuItem
    //import androidx.appcompat.widget.ShareActionProvider
    import androidx.appcompat.widget.Toolbar

    //import androidx.core.view.MenuItemCompat
    //import android.support.v4.view.MenuItemCompat
    //import androidx.core.view.MenuItemCompat.getActionProvider

    import android.support.v7.app.AppCompatActivity
    import android.support.v7.widget.ShareActionProvider
    //import android.widget.ShareActionProvider


    class MainActivity : AppCompatActivity() {
        private var shareActionProvider : ShareActionProvider? = null

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            val toolbar = findViewById(R.id.toolbar) as Toolbar
            setSupportActionBar(toolbar)
        }

        override fun onCreateOptionsMenu(menu : Menu): Boolean {
            getMenuInflater().inflate(R.menu.menu_main, menu)
            val menuItem = menu.findItem(R.id.action_share) as MenuItem
            shareActionProvider = MenuItemCompat.getActionProvider(menuItem) as ShareActionProvider
            setShareActionIntent("Want to join me for pizza?")
            return super.onCreateOptionsMenu(menu)
        }

        private fun setShareActionIntent(text : String) {
            val intent = Intent(Intent.ACTION_SEND)
            intent.type = "text/plain"
            intent.putExtra(Intent.EXTRA_TEXT, text)
            shareActionProvider!!.setShareIntent(intent)
        }

        override fun onOptionsItemSelected(item : MenuItem) : Boolean {
            when(item.itemId) {
                R.id.action_create_order -> {
                    val intent = Intent(this, OrderActivity::class.java)
                    startActivity(intent)
                    return true
                }
                else -> return super.onOptionsItemSelected(item)
            }
}
}

这是我的 gradle.build 文件

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

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        jcenter()
        maven { url '../pluginrepo' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-beta05'
        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 {
        google()
        jcenter()
    }
}

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

这是我的应用 (build.gradle) 文件

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.gandalf.bitsandpizza"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
    //    implementation 'com.android.support:support-core-utils:27.0.0'
    implementation 'com.google.android:support-v4:r7'
    implementation 'com.android.support:appcompat-v7:28.0.+'
    //    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:support-core-utils:28.0.+'
}

这基于答案中建议的重构。

问题现已解决。这是更新后的 gradle.build 文件,来自 app

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.some.name"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

我正在使用这些依赖项

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

有进口

import android.support.v4.view.MenuItemCompat
import android.support.v7.widget.ShareActionProvider

而且没有红色。

也尝试清理代码

正如@Mohsen所说,你也可以尝试这样做

So in my code it can't import android.support.v7.widget.ShareActionProvider The v7 is in Red because, it can't be resolved

我希望这是您的代码中唯一的问题。您已添加:

implementation 'com.google.android:support-v4:r7'
implementation 'com.android.support:appcompat-v7:28.0.+' // Use specific version like 28.0.0 in future for this

指的是 v4v7 AppCompat 但你的 Toolbar 和测试 + ConstraintLayout 正在使用 AndroidX 依赖项。这对我来说真的很奇怪和困惑!

要使用 AppCompat 提供的 ShareActionProvider,您需要确保您的应用已迁移到 AndroidX 或不是第一!

我相信这是因为您的项目似乎已迁移到 AndroidX,这让 IDE 难以使用 AppCompatAndroidX

如果您尝试使用 V7 依赖项,请转到 Refactor、Select migrate to AppCompat

头痛了很久。我发现问题出在我使用的 Android Studio 版本上。我使用的是 Beta 版本,由于某种原因,库被搞砸了。当我切换到稳定版本时,一切都开始正常工作。

1) 只需按 ALT+ENT 和 select 将应用程序迁移到 AppCompat。

2) ImageView 重命名为 AppCompatImageView 。

3) 最后按 ALT+ENT 导入 class。 问题已解决!

或者导入 androidx.appcompat.widget.AppCompatImageView;

我在 gradle.properties

中使用这些
android.useAndroidX=true
android.enableJetifier=false