Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import a new module (android library project) using Android studio 0.3.0

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.

like image 458
micnoy Avatar asked Oct 19 '13 09:10

micnoy


People also ask

Can we import module from source in Android Studio?

Select the source directory of the Module you want to import and click Finish. Open Project Structure Dialog (You can open the PSD by selecting File > Project Structure) and from the left panel click on Dependencies. Select the module from the Module(Middle) section In which you want to add module dependency.


3 Answers

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"

like image 106
JtotheR Avatar answered Sep 27 '22 21:09

JtotheR


In android studio 0.3.1 they fixed it.

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

Import module screen

like image 27
micnoy Avatar answered Sep 27 '22 22:09

micnoy


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'
like image 36
Victorio Monlleó Avatar answered Sep 27 '22 20:09

Victorio Monlleó