Add support library to Android Studio project

后端 未结 6 2026
天涯浪人
天涯浪人 2020-11-30 19:24

I just installed the new Android Studio and I\'m looking for a way to import the support library for Android.

Where is the option for that? In Eclipse that are just

相关标签:
6条回答
  • 2020-11-30 19:59

    I no longer work on Android project for a while. Although the below provides some clue to how an android studio project can be configured, but I can't guarantee it works flawlessly.

    In principle, IntelliJ respects the build file and will try to use it to configure the IDE project. It's not true in the other way round, IDE changes normally will not affect the build file.

    Since most Android projects are built by Gradle, it's always a good idea to understand this tool.

    I'd suggest referring to @skyfishjy's answer, as it seems to be more updated than this one.


    The below is not updated

    Although android studio is based on IntelliJ IDEA, at the same time it relies on gradle to build your apk. As of 0.2.3, these two doesn't play nicely in term of configuring from GUI. As a result, in addition to use the GUI to setup dependencies, it will also require you to edit the build.gradle file manually.

    Assuming you have a Test Project > Test structure. The build.gradle file you're looking for is located at TestProject/Test/build.gradle

    Look for the dependencies section, and make sure you have

    compile 'com.android.support:support-v4:13.0.+'

    Below is an example.

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.5.+'
        }
    }
    apply plugin: 'android'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile 'com.android.support:support-v4:13.0.+'
    }
    
    android {
        compileSdkVersion 18
        buildToolsVersion "18.0.1"
    
        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 16
        }
    }
    

    You can also add 3rd party libraries from the maven repository

    compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4'

    The above snippet will add gson 2.2.4 for you.

    In my experiment, it seems that adding the gradle will also setup correct IntelliJ dependencies for you.

    0 讨论(0)
  • 2020-11-30 20:02

    This is way more simpler with Maven dependency feature:

    1. Open File -> Project Structure... menu.
    2. Select Modules in the left pane, choose your project's main module in the middle pane and open Dependencies tab in the right pane.
    3. Click the plus sign in the right panel and select "Maven dependency" from the list. A Maven dependency dialog will pop up.
    4. Enter "support-v4" into the search field and click the icon with magnifying glass.
    5. Select "com.google.android:support-v4:r7@jar" from the drop-down list.
    6. Click "OK".
    7. Clean and rebuild your project.

    Hope this will help!

    0 讨论(0)
  • 2020-11-30 20:05

    You can simply download the library which you want to include and copy it to libs folder of your project. Then select that file (in my case it was android-support-v4 library) right click on it and select "Add as Library"

    0 讨论(0)
  • 2020-11-30 20:18

    Android no longer downloading the libraries from the SDK manager, it has to be accessed through Google's Maven repository.

    You will have to do something similar to this in your build.gradle file:

    allprojects {
        repositories {
            jcenter()
            maven {
                url "https://maven.google.com"
            }
        }
    }
    
    
    
    dependencies {
        ...
        compile "com.android.support:support-core-utils:27.0.2"
    }
    

    Find more details about the setting up process here and about the different support library revisions here.

    0 讨论(0)
  • 2020-11-30 20:20

    In Android Studio 1.0, this worked for me :-
    Open the build.gradle (Module : app) file and paste this (at the end) :-

    dependencies {
        compile "com.android.support:appcompat-v7:21.0.+"
    }
    

    Note that this dependencies is different from the dependencies inside buildscript in build.gradle (Project)
    When you edit the gradle file, a message shows that you must sync the file. Press "Sync now"

    Source : https://developer.android.com/tools/support-library/setup.html#add-library

    0 讨论(0)
  • 2020-11-30 20:23

    =============UPDATE=============

    Since Android Studio introduce a new build system: Gradle. Android developers can now use a simple, declarative DSL to have access to a single, authoritative build that powers both the Android Studio IDE and builds from the command-line.

    Edit your build.gradle like this:

    apply plugin: 'android'
    
        android {
            compileSdkVersion 19
            buildToolsVersion "19.0.3"
    
        defaultConfig {
            minSdkVersion 18
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
        }
    
        buildTypes {
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    
        dependencies {
            compile fileTree(dir: 'libs', include: ['*.jar'])
            compile 'com.android.support:support-v4:21.+'
        }
    

    NOTES: Use + in compile 'com.android.support:support-v4:21.+' so that gradle can always use the newest version.

    ==========DEPRECATED==========

    Because Android Studio is based on IntelliJ IDEA, so the procedure is just same like on IntelliJ IDEA 12 CE

    1.Open Project Structure (Press F4 on PC and Command+; on MAC) on your project).

    2.Select Modules on the left pane.

    3.Choose your project and you will see Dependencies TAB above the third Column.

    4.Click on the plus sign in the bottom. Then a tree-based directory chooser dialog will pop up, navigate to your folder containing android-support-v4.jar, press OK.

    5.Press OK.

    0 讨论(0)
提交回复
热议问题