简体   繁体   中英

How to add dependencies?

I am using Android studio version 4.0

In Project level gradle:

def supportVersion = "27.1.0"
ext.deps = [
    supportAppCompat   : "com.android.support:appcompat-v7:$supportVersion",
    supportDesign      : "com.android.support:design:$supportVersion",
 
]

Module level gradle:

dependencies {    
implementation deps.supportAppCompat
implementation deps.supportDesign
}

When I import this it occurs an error

错误图片

when I import this

import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

Error in xml

error pic

when add this tags in xml an error occurs

<android.support.design.widget.CoordinatorLayout/>
<android.support.design.widget.AppBarLayout/>
<android.support.v7.widget.Toolbar />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton />

give a solution for this!!!

As Of Google Official Document ( https://developer.android.com/topic/libraries/support-library/packages )

Note: With the release of Support Library 28.0.0, the android.support-packaged libraries are deprecated and replaced by individually-versioned Jetpack libraries packaged as androidx. The initial 1.0.0 release of the Jetpack libraries provides parity with Support Library 28.0.0 and provides a starting point for migrating to the new androidx packaging.

To fix the error:

"com.android.support:appcompat-v7:$supportVersion"
"com.android.support:design:$supportVersion"

to

def supportVersion = "1.0.0"
ext.deps = [
    supportAppCompat   : "androidx.appcompat:appcompat:$supportVersion",
    supportDesign      : "com.google.android.material:material:$supportVersion",
]

After that either restart Android Studio or Refresh is cache data as- enter image description here

More information on this this thread: Cannot resolve symbol AppCompatActivity - Support v7 libraries aren't recognized?

For forcing android studio to use old library

I check you are using 27.1.0, so we can force android studio not to use androidx

  • Go to gradle.properties

  • make these flags false

    android.useAndroidX=false

    android.enableJetifier=false

as you using android studio 4.0

use androidX library instead of Support Library...as latest version uses androidx

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-alpha3', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
}

You can migrate your supportAppCompat to the AndroidX library.

In the menu go to Refactor -> Migrate to AndroidX. Then you should reimport androidx in your classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM