Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am new coming to android studio and still discovering things.

I want to create my own class library to be used in any project I create later

I managed to do this

1- Create android project with No Activity
2- Insert a module >> Java or Kotlin Library >> I name it MyClass
3- I insert my code in MyClass

Then
1- I create another android project Empty Activity
2- I import a module and I chose my first project
3- In project structure I manage the dependencies >> Add Module Dependency

I managed to used my Library. What I noticed is the library project is being copied into new folder in the project. So now I have 2 copies of the project (If I do changes to MyClass) in my Library project it will not see changes in my second project and vise versa, if I change MyClass in Second project I will not see changes in MyLibrary Project

Is there another way to add reference to project and see changes (not folder copy) ?

What I have tried:

1- Create android project with No Activity
2- Insert a module >> Java or Kotlin Library >> I name it MyClass
3- I insert my code in MyClass

Then 
1- I create another android project Empty Activity
2- I import a module and I chose my first project
3- In project structure I manage the dependencies >> Add Module Dependency
Posted
Updated 19-Feb-21 22:24pm
Comments
David Crow 8-Feb-21 17:35pm    
Have you see any of these:

https://developer.android.com/studio/projects/android-library#AddDependency

https://developer.android.com/studio/build/dependencies

https://stackoverflow.com/questions/25610727/adding-external-library-in-android-studio
Samir Ibrahim 20-Feb-21 4:20am    
@David Thank you for your replay and sorry for my late response. The first link is No, The second link I did not understand what they are suggesting (my lake of knowledge maybe). The third link contains exactly what I asked for.. Thanks

1 solution

The solution to my question was provided by @David third link
To reference an external lib project without copy, just do this: - Insert this 2 lines on setting.gradle:

  include ':your-lib-name'
  project(':your-lib-name').projectDir = new File('/path-to-your-lib/your-lib-name)
Insert this line on on dependencies part of build.gradle file:

compile project(':your-lib-name')


In addition, I found my own solution

After I add the class by importing module, I right click on the module >> refactor >> Copy Class >> Then I point it to the original directory (it will ask to overwrite existing)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900