8

The android tools team made a huge step when introducing android studio 0.3.0 with the new user interface for modifying the build.grade file using the project structure.

But how can I import an android library project into my general project? When I press the '+' button in the Project structore -> Modules section I can only create NEW module.

4 Answers 4

16

The way I just did it was by copy pasting the library project in the root of your project (not using android studio, this gives a 'cannot create class-file error', but just using the file manager of your operating system. When using android studio for this it didn't copy some of the files).

Then mark the java folder of the library project as 'source root' (right mouse on the folder).

Then to settings.gradle add your lib project like this:

include ':youApp', ':yourLibrary'

then to build.gradle of yourApp add dependancy:

dependencies {
    compile project(':yourLibrary')
}

Now rebuild your project.

Now add a function of the library project (showing red) when you click on it and press Alt-Enter it should say "add dependancy on module yourAppProject"

8

In android studio 0.3.1 they fixed it.

Project structure -> Modules -> "+" -> Import module.

Import module screen

2
3

It's still broken 0.3.6...

The manual way can be a good solution. For example, to import a library like Android-Validator (https://github.com/throrin19/Android-Validator) in your project "Test":

The structure will be:

Test
    build.gradle
Android-Validator (the library sources)
    build.gradle
    src
    res
settings.gradle

And now, let's gonna edit the files...

In Test/build.gradle add:

dependencies {
    compile project(':Android-Validator')  
  }

Android-Validator/build.gradle (remember to change xx and yy):

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

android {

   compileSdkVersion XX
   buildToolsVersion "XX.0.0"

   defaultConfig {
       minSdkVersion YY
       targetSdkVersion XX

   }    

   sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']            
                res.srcDirs = ['res']            
            }
        }
    }

settings.gradle:

include ':Test', ':Android-Validator'
2

I had a very similar issues with gradle builds / adding .jar library. I got it working by a combination of :

  1. Moving the libs folder up to the root of the project (same directory as 'src'), and adding the library to this folder in finder (using Mac OS X)
  2. In Android Studio, Right-clicking on the folder to add as library
  3. Editing the dependencies in the build.gradle file, adding: compile fileTree(dir: 'libs', include: '*.jar')}

But annoyingly, HOURS after I get it working, Android Studio have just released 0.3.7, which claims to have solved a lot of gradle issues such as adding .jar libraries

http://tools.android.com/recent

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.