Add support library to Android Studio project
Asked Answered
A

7

115

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 two clicks. I googled for it but found nothing. Surely it is too new.

Alonzo answered 16/5, 2013 at 6:39 Comment(1)
@AthiraReddy that looks a bit like seo spamAlonzo
A
55

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.

Acetabulum answered 4/8, 2013 at 5:1 Comment(3)
with gradle 0.9.+ this answer should be correct, but for some reason it doesn't work out. Doing the same via IDE as mentioned in the answer of @Pedicure everything seems to work out.Spondee
I just added the simple line compile 'com.android.support:support-v4:19.+ to the dependecies section, Android Studio 0.5.4 reported back that it's synchronizing the project with the gradle files, and shortly afterwards I found the folder for the support library in Project->External Libraries, ready to go.Ralph
In version 20.0.0 the "+" leads to an error, so you should update your answer and remove the "+" or leave a warning on this. Fore more information see: #24438670Tensity
P
86

=============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.

Pedicure answered 16/5, 2013 at 7:20 Comment(8)
Thanks for this, I spent hours trying to figure out, why my app crashes.Osseous
Make sure you add to Gradle too. I don't think this method works anymore on AS 0.3+Caputto
The key here for me was navigating to the folder rather than just the jar. Thanks man!Renae
This answer seems great, but for new users of Android studio is still confusing - I don't know which item in my folder is the Project. Is it the most root-level item in the tree? Is it the Application module within that item? Is it the .iml file? It would help to expand more here.Blockish
I changed the accepted state due the other answer is much simpler. However I upvoted your answer to compensate your rep lost.Alonzo
Since your answer still has more upvotes -> same as in accepted answer: You should leave a warning, that "+" will break the build as of support-lib-version 20 -> #24438670Tensity
Android Support Library rev 22 is out nowHomozygote
Thank You Sir, this got me what I needed!Billion
A
55

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.

Acetabulum answered 4/8, 2013 at 5:1 Comment(3)
with gradle 0.9.+ this answer should be correct, but for some reason it doesn't work out. Doing the same via IDE as mentioned in the answer of @Pedicure everything seems to work out.Spondee
I just added the simple line compile 'com.android.support:support-v4:19.+ to the dependecies section, Android Studio 0.5.4 reported back that it's synchronizing the project with the gradle files, and shortly afterwards I found the folder for the support library in Project->External Libraries, ready to go.Ralph
In version 20.0.0 the "+" leads to an error, so you should update your answer and remove the "+" or leave a warning on this. Fore more information see: #24438670Tensity
S
24

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!

Shamble answered 26/1, 2014 at 21:25 Comment(2)
Since I understood it I prefer the gradle way. But it's helpful to know that this includes uses the maven repository.Alonzo
when i click plus sign maven dependency is not displayed.only show library,file and module dependencies are there.Byrom
B
6

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"

Baedeker answered 28/9, 2013 at 20:53 Comment(0)
M
4

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

Marler answered 3/1, 2015 at 10:35 Comment(0)
B
0

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.

Bathesda answered 27/12, 2017 at 15:53 Comment(0)
A
0

AndroidX[About]

implementation 'androidx.appcompat:appcompat:1.0.2'  
Azriel answered 30/5, 2021 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.