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

10,067

Solution 1

I'm using these dependencies

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'
}

There imports

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

And there's no red.

Try to clean the code as well

As @Mohsen said, you can also try to do this

enter image description here

Solution 2

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

I wish that was the only issue in your codes. You've added:

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

Which refers to v4 and v7 AppCompat but your Toolbar and the tests + ConstraintLayout are using AndroidX dependencies. This is actually weird and confusing even to me!

For using ShareActionProvider which is available with AppCompat, you'll need to make sure your app is migrated to AndroidX or not first!

I believe this is happening because it seems like your project is migrated to AndroidX and this is confusing the IDE to use AppCompat or AndroidX.

Simply, go to Refactor, Select migrate to AppCompat if you're trying to use V7 dependencies.

Share:
10,067

Related videos on Youtube

ConnectionistSystems
Author by

ConnectionistSystems

Updated on June 04, 2022

Comments

  • ConnectionistSystems
    ConnectionistSystems about 2 years

    I tried to import this support library into my project but can't get it to work.

    First of all I can't find it. When I hit the + button on the Dependencies tab and search for it. I have found android.support.v4 but I need v7 as well. I read somewhere that the library might be deprecated.

    So in my code it can't import android.support.v7.widget.ShareActionProvider The v7 is in Red because, it can't be resolved. I followed the direction on how to add a library. But still can't solve the issue. I did normal steps as clean, sync gradle files, invalidate links, etc...

    Here is some of my code from 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)
                }
    }
    }
    

    This my gradle.build file

     // 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
    }
    

    This is my app (build.gradle) file

    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.+'
    }
    

    This is based on Refactoring suggested in answer.

    The issue is now resolved. This is the updated gradle.build file from 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'
    }
    
    • Skizo-ozᴉʞS
      Skizo-ozᴉʞS over 5 years
      Why you don't use this instead: import android.widget.ShareActionProvider; or this one : import android.support.v7.widget.ShareActionProvider;
    • ConnectionistSystems
      ConnectionistSystems over 5 years
      So if I use the aforementioned import instead of v7. Then this line of code won't work, because it can't find getActionProvider. shareActionProvider = MenuItemCompat.getActionProvider(menuItem) as ShareActionProvider
    • Skizo-ozᴉʞS
      Skizo-ozᴉʞS over 5 years
      What is in red?
    • ConnectionistSystems
      ConnectionistSystems over 5 years
      getActionProvider
    • Skizo-ozᴉʞS
      Skizo-ozᴉʞS over 5 years
      See my update on my answer
    • ʍѳђઽ૯ท
      ʍѳђઽ૯ท over 5 years
      @ConnectionistSystems Now you have duplicate imports. Remove them all and import them again with the appcompat. It should work then
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    For the first option v7 can't be resolved. For the second option getActionProvider is unresolved reference. shareActionProvider = MenuItemCompat.getActionProvider(menuItem) as ShareActionProvider
  • ʍѳђઽ૯ท
    ʍѳђઽ૯ท over 5 years
    Yes, because you didn't migrated to AndroidX that's why. Check the OP's Activity imports of Toolbar and AppCompat. I've tried to explain them all in my answer, please check that too.
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    Then @ConnectionistSystems make sure you have Android Studio 3.2 and then Refactor to AppCompat
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    Okay. I did migrate to AppCompat as suggested. I still have red on v7 which is why I tired to use AndroidX instead. I also did clean and sync gradle file.
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    What is red? Can you post a screen shot?
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    I uploaded an image in original question. I don't know how to do it here.
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    Remove your import of Androidx AppCompatActivity it should be only this one :import android.support.v7.app.AppCompatActivity And make sure your gradle is like my answer the same implementations
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    Tried it @ConnectionistSystems?
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    I want to thank everyone for their assistance. I tired what was suggested. I changed my gradle file as was suggested and cleaned the project. It is still the same and the biggest issue is v7 is still red. I tried to refactor to AppCompat again, still nothing changed.
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    Did you remove the androidx import?
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    I uploaded two more images at the bottom of the question
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    Change this line testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" aswell... can you upload your code to github or something so I can take a look deeply? or maybe TeamViewer
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    I changed the gradle.build file. I also uploaded to github. github.com/ConnectionistSystems/BitsandPizza Thank You!
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    Ok, I did some changes, it's working now, you can see in the commit what I've changed... it was lot of stuff hahaha :D
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    Oh I'm not able to push, can you invite me ?
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    nvm I'll create a new project and upload it and then you can download and see what's different from yours
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    Hello @Skizo-ozᴉʞS Thanks for looking at the code. I wanted to add to the repository but I don't know your email address. I tried implementing the changes you did. I see how you made changes mainly to MainActivity. I still however have the same issue. The V7 is red. Please see the latest image attached with the question. I can't find appcompatv7 to download. Perhaps you have it already downloaded in Android Studio. Thanks again for your time.
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    @ConnectionistSystems Give me your email and I'll update your code, I did not change only the MainActivity....
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    The issue is now resolved. This is the updated build.gradle file. Thanks @Skizo-ozᴉʞS
  • ConnectionistSystems
    ConnectionistSystems over 5 years
    I have less than 15 reputation, therefore I can't up/like any of the posted answers. Thanks!
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 5 years
    But you do not need reputation to mark my answer as a correct. Feel free to upvote and mark my answer as a correct if it worked!