Respect the fruits of labor, reproduced please specify the source: http://blog.csdn.net/growth58/article/details/47441245
Follow Sina Weibo: @ in Wei
Email: [Email protected]
When we had two Android studio projects, we wanted to use one as the library project to import into another project as a module. We can do it easily using Android studio.
Let's assume we have two projects: MyApplication and MyLibrary, and we want to import mylibrary to MyApplication as a module.
First we need to find the Build.gradle file in the module from the MyLibrary project, which is usually in the app directory unless you modify the module's name.
Note The plugin of the Builde.gradle file is ' com.android.application '. In order to use this project as a library project, we need to revise it to ' com.android.library '. The contents of the file are like this:
Try to sync the project, there will be an error: Library projects cannot set ApplicationID (class project cannot be set ApplicationID).
We can delete the ApplicationID line in the Gradle file, inside the defaultconfig brackets.
The MyLibrary project was revised.
Then we'll import it in the MyApplication project.
MyLibrary as a module.
In the MyApplication project, click Import Module, File-New.
A dialog box appears
Find the module directory you want to import from the MyLibrary project. Usually it is the app module unless you give it another name.
If the module uses the same name in the MyApplication project, an error will occur. In order to import a modified module from a library project, we can give it a version number in the Module name setting box.
The name of the modified module is My-library, and the import succeeds.
This step is almost complete, and the final step is to modify dependencies in the MyApplication project.
Many people will forget to do this step.
Open the Project Structure dialog box, select the app Module (which can be distinguished by name), and click on the Dependent tab on the right.
Find the plus sign and click on it to select module dependency from the pop-up menu.
Then select the Library module that we just imported from the MyLibrary project.
Click OK and you're done.
The IDE adds a good dependency on the Myapplication/app/build/gradle file.
The dependency section is the same as the following:
dependencies { compile fileTree(dir: ‘libs‘, include: [‘*.jar‘]) compile ‘com.android.support:appcompat-v7:22.2.0‘ compile project(‘:my-library‘)}
It may work very well, but there is a very important problem, and the MyLibrary project has its own dependencies. For example, MyApplication and mylibrary use support libraries, which can cause conflicts.
We can avoid this problem by configuring dependencies in Gradle through transitive.
We can modify this line.
project(‘:my-library‘)
For
compile(project(‘:my-library‘)){ transitive=false;}
This problem can be avoided. done!
That's all. Happy coding!
Import a Androidstudio project as a library Module