Add Support Library to Android Studio Project

Add support library to Android Studio project

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.

Adding external library in Android studio

Try this:

File > Project Structure > Dependencies Tab > Add module dependency (scope = compile)

Where the module dependency is the project library Android folder.

How do I add a library project to Android Studio?

Update for Android Studio 1.0

Since Android Studio 1.0 was released (and a lot of versions between v1.0 and one of the firsts from the time of my previous answer) some things has changed.

My description is focused on adding external library project by hand via Gradle files (for better understanding the process). If you want to add a library via Android Studio creator just check the answer below with visual guide (there are some differences between Android Studio 1.0 and those from screenshots, but the process is very similar).

Before you start adding a library to your project by hand, consider adding the external dependency. It won’t mess in your project structure. Almost every well-known Android library is available in a Maven repository and its installation takes only one line of code in the app/build.gradle file:

dependencies {
implementation 'com.jakewharton:butterknife:6.0.0'
}

Adding the library

Here is the full process of adding external Android library to our project:

  1. Create a new project via Android Studio creator. I named it HelloWorld.
  2. Here is the original project structure created by Android Studio:
HelloWorld/
app/
- build.gradle // local Gradle configuration (for app only)
...
- build.gradle // Global Gradle configuration (for whole project)
- settings.gradle
- gradle.properties
...

  1. In the root directory (HelloWorld/), create new folder: /libs in which we’ll place our external libraries (this step is not required - only for keeping a cleaner project structure).
  2. Paste your library in the newly created /libs folder. In this example I used PagerSlidingTabStrip library (just download ZIP from GitHub, rename library directory to „PagerSlidingTabStrip" and copy it). Here is the new structure of our project:
HelloWorld/
app/
- build.gradle // Local Gradle configuration (for app only)
...
libs/
PagerSlidingTabStrip/
- build.gradle // Local Gradle configuration (for library only)
- build.gradle // Global Gradle configuration (for whole project)
- settings.gradle
- gradle.properties
...

  1. Edit settings.gradle by adding your library to include. If you use a custom path like I did, you have also to define the project directory for our library. A whole settings.gradle should look like below:

     include ':app', ':PagerSlidingTabStrip'
    project(':PagerSlidingTabStrip').projectDir = new File('libs/PagerSlidingTabStrip')

5.1 If you face "Default Configuration" error, then try this instead of step 5,

    include ':app'
include ':libs:PagerSlidingTabStrip'

  1. In app/build.gradle add our library project as an dependency:

     dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:21.0.3'
    implementation project(":PagerSlidingTabStrip")
    }

6.1. If you followed step 5.1, then follow this instead of 6,

    dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:21.0.3'

implementation project(":libs:PagerSlidingTabStrip")
}

  1. If your library project doesn’t have build.gradle file you have to create it manually. Here is example of that file:

         apply plugin: 'com.android.library'

    dependencies {
    implementation 'com.android.support:support-v4:21.0.3'
    }

    android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
    minSdkVersion 14
    targetSdkVersion 21
    }

    sourceSets {
    main {
    manifest.srcFile 'AndroidManifest.xml'
    java.srcDirs = ['src']
    res.srcDirs = ['res']
    }
    }
    }
  2. Additionally you can create a global configuration for your project which will contain SDK versions and build tools version for every module to keep consistency. Just edit gradle.properties file and add lines:

     ANDROID_BUILD_MIN_SDK_VERSION=14
    ANDROID_BUILD_TARGET_SDK_VERSION=21
    ANDROID_BUILD_TOOLS_VERSION=21.1.3
    ANDROID_BUILD_SDK_VERSION=21

    Now you can use it in your build.gradle files (in app and libraries modules) like below:

     //...
    android {
    compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION

    defaultConfig {
    minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
    targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
    }
    }
    //...
  3. That’s all. Just click‚ synchronise the project with the Gradle’ icon synchronise with Gradle. Your library should be available in your project.

Google I/O 2013 - The New Android SDK Build System is a great presentation about building Android apps with Gradle Build System: As Xavier Ducrohet said:

Android Studio is all about editing, and debugging and profiling.
It's not about building any more.

At the beginning it may be little bit confusing (especially for those, who works with Eclipse and have never seen the ant - like me ;) ), but at the end Gradle gives us some great opportunities and it worth to learn this build system.

Can I use library that used android support with Androidx projects.

You can enable Jetifier on your project, which will basically exchange the Android Support Library dependencies in your project dependencies with AndroidX-ones. (e.g. Your Lottie dependencies will be changed from Support to AnroidX)

From the Android Studio Documentation (https://developer.android.com/studio/preview/features/):

The Android Gradle plugin provides the following global flags that you
can set in your gradle.properties file:

  • android.useAndroidX: When set to true, this flag indicates that you want to start using AndroidX from now on. If the flag is absent,
    Android Studio behaves as if the flag were set to false.
  • android.enableJetifier: When set to true, this flag indicates that you want to have tool support (from the Android Gradle plugin) to
    automatically convert existing third-party libraries as if they were
    written for AndroidX. If the flag is absent, Android Studio behaves as
    if the flag were set to false.

Precondition for Jetifier:

  • you have to use at least Android Studio 3.2

To enable jetifier, add those two lines to your gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true

Finally, please check the release notes of AndroidX, because jetifier has still some problems with some libraries (e.g. Dagger Android): https://developer.android.com/topic/libraries/support-library/androidx-rn

How to use support libraries in android studio?

Navigate to File -> Project Structure -> app -> Dependencies

Click the plus icon on the right hand side, select Library Dependency and add the required support libraries from there.

Sample Image

How can i install support library android?

You're asking the wrong question.

Right-click on your project.

Select Android Tools > Add Support Libraries...

(a progress bar will appear, and voila it should download what you need)

By the way, you should really install the latest android-sdk update. From your screenshot, it doesn't look like you have yet.

Adding support library to android studio project

I fixed it.... All I needed to do was run

./gradlew clean build

in the root of my project. Android studio wasn't updating the gradle so it didn't include the external libraries.

How to import Android Support Library to project?

If you download it from Android Studio (Tools -> Android -> SDK manager)

You can obtain path to SDK from menu. Just launch SDK manager
And get the SDK Path (Line over the packages)

So now you can find support library by following way:

"SDK Path"\extras\android\support

Android gradle build and the support library

Until we have the support library has a repository artifact you cannot include it in more than one library project.
You could create a library project that only contains the support library, and have all other libraries depend on it.

Update: this is now possible.

Android Studio: Gradle Sync fail with v4 support library

There are two build.gradle files in your project, one rootlevel, and one for your application. You will also see a dependencies section in the other build.gradle. You need to place the compile "com.android.support:support-v4:24.1.1" in there.



Related Topics



Leave a reply



Submit