Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a project as a library in Android Studio?

How can I add a project like (RootTools) on another project as library in Android Studio?

I am using Android Studio 0.8.1 and I don't know how to add another project (folder) and I found a lot of information about importing jar files, but this is not the case.

Thanks for your help.

like image 967
Víctor Martín Avatar asked Jul 07 '14 12:07

Víctor Martín


2 Answers

I would suggest to import your library manually rather than using "Import Module" since it 1) will change directory layout for the library; 2) you can catch bugs (as I did) because Android Studio is still in beta.

To accomplish this:

1) Copy your library folder under /libraries

2) Create build.gradle file inside library folder you've just copied, with similar content:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}
apply plugin: 'android-library'

android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    sourceSets {
        main {
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']

            manifest.srcFile 'AndroidManifest.xml'
        }
    }
}

3) Add include ':libraries:RootTools' to your settings.gradle

4) Add dependency to the build.gradle under the app module:

dependencies {
    compile project(':libraries:RootTools')
    ...
}

5) Run ./gradlew assembleDebug to assemble your project, including the library.

like image 68
Alexey Dmitriev Avatar answered Oct 11 '22 09:10

Alexey Dmitriev


If you want to do it manual, you can follow the steps below.

  1. Create a folder as called "libraries" in top level. Not necessary but it will help you to have a good structure when you need to add more libraries.
  2. Copy the project folder under libraries.
  3. open settings.gradle and add the project.

include ':libraries:RootTools'

  1. Open build.gradle of the your project, not the top level build.gradle and add dependency
dependencies {
    compile 'com.android.support:support-v4:19.1.+'
    compile project(':libraries:RootTools')
}

then run or ./gradlew assemble

app 
- build.gradle //  add dependency
libraries
- RootTools
settings.gradle // add project
like image 31
Orhan Obut Avatar answered Oct 11 '22 10:10

Orhan Obut