How to update an imported module in Android Studio?
Asked Answered
A

1

53

While I develop an Android App, I have a library which I have created as separate Android Studio project and can use it by inserting it into new projects. I insert the library by choosing 'File|New|Import Module...' option.

The thing is that after the import, Gradle creates a hard copy of my library. If I change the library code in main external project, the code inside the project which is using the library won't get updated.

How can I have a library and share it among many project? I need to change the library in one single place and then all other projects which are using it get the update.

I found this post also which has no answer:

How to update imported modules with code modification from the their external library project in Gradle/Android Studio

Ansley answered 15/9, 2015 at 6:54 Comment(1)
Seems to me all they need to provide when adding a module to a project is a checkbox that says "make a symlink to source dir instead of copying sources into project" and all these problems would go away (yes, 2021, people are still dealing with this issue, including me). Simplest possible solution, as from Android Studio's point of view, a symlink ought to be as good as copying.Nail
A
71

OK I found the answer by myself:

You must not add the external library as an existing module. It will make a copy of it under your project folder.

What you have to do is:

  1. Delete the library folder in your current project. (You can also delete the ./idea/modules/[module_name] folder.)
  2. Open the setting.gradle file and add these:
include ':your_external_library_module_name'
project (':your_external_library_module_name').projectDir = new File('../path/to/your/external/library')

include ':perhaps_second_external_library'
project (':perhaps_second_external_library').projectDir = new File('../path/to/your/second/external/library')
  1. In your build.gradle (:app) file add dependency as:
dependencies {
    implementation project(':your_external_library_module_name')
    implementation project(':perhaps_second_external_library')
}
  1. Sync the project and you are done.
Ansley answered 16/9, 2015 at 15:27 Comment(10)
I followed the steps but when building the project, i get the following error: Error:Configuration with name 'default' not found. And i don't have anything named as 'default'. What could be happening?Loge
Add your library folder in your root location of your project and copy all the library files there. For ex YourProject/library then clean and rebuild the project.Ansley
Thank you, something like this really needs to be on the Create an Android library developers pageUnder
Wow. This is terrible. Why are we forced to use gradle again?Syncopate
I've succeeded in doing this. But is there a way that the path is not hard-coded so it points only my folder hierarchy, but it will work on other PCs as well?Tripodic
it gave error : Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve project :app-debug.Machine
I'm pretty sure this is not the cleanest way to go, but god, it's a great workaround ! Thank you very much, that will save me hours of reimport ^^Mediatory
Saved a lot of work for me. Thanks a lot. Note: to make this work, remember not to import the module. I did the mistake first time. Rather the steps shown here are steps to add module manually.Milline
5 years later, still works great in Android Studio 4.0.2!. Changes in the library are automatically updated in the host application. In Android Studio 4.0.2 it should be implementation project(':mylibrary') as compile project is now deprecated. Been looking a long time at other solutions that import a copy of the library sources. But this is simple and working.Rationalize
tried in Android Studio Chipmunk and it embedded successfully but unable to export module classes?Bistre

© 2022 - 2024 — McMap. All rights reserved.