Android Studio: add project as library

2019-03-01 14:33发布

问题:

I want to add this project as library to my project in android studio. this is what I tried,

I have my project directory as f:/my project/my app/src and my library in f:/my project/my library/src

I import the module (the library) by going to file > import module > selecting the library then I got to file > project structure > modules > dependencies tab > select my project > add module dependency apply ok and then done

however when I use the code from the library I get the usual syntax error (the class ... could not be found)

also I noticed this popup (see image)

I am new to android studio or intelliJ, how do I fix this. Thanks!

回答1:

Edit the settings.gradle file (in directory f:/my project), it must contains something like this:

include 'my app','my library'

If this file don't exists: create it manually. The settings.gradle contains the list of gradle modules in a multi-module project.

Then you must add the dependency to your library in app. To do so edit the my app/build.gradle and add this line :

dependencies {
    compile project(':my library')
}

I also notice that you don't use default structure for your projects (i.e. you put the code in src/ instead of src/main/java) so you will have to overwrite some values of the default fileSet in the build.gradle of your projects. Be sure to have something like this in my app/build.gradle and my library/build.gradle :

android {
    sourceSets {
        main {
            java.srcDirs = ['src']
        }
    }
}