• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How do I organize common code that will be shared by multiple projects?

RhinoCan

Well-Known Member
As I get further and further into my first serious project, it's becoming very clear that some of the bits and pieces of code I'm developing will get used in other projects as well. The obvious solution here is to put that kind of code in some kind of library that is accessible by ALL projects. For example, I expect to have routines to send texts, manipulate dates, do (some) error handling, etc. etc. in such a library.

How are people doing that in Android Studio? I knew how to do it in Eclipse but Android Studio doesn't show you all of your projects simultaneously on a "main screen" the way Eclipse does.

Naturally, Gradle will have to include this common code and the APK packager will have to include it as well.
 
Thanks for that answer!

Unfortunately, there wasn't a lot of "how to" in that answer. I found some YouTube videos on the subject but most were 4 years old and talking about much older releases of Android Studio so I wasn't confident they were still accurate so I improvised.

I clicked on File and looked for an option to create a new Library but didn't find one. However, I *did* see an option to create a new Module. I'm not sure what a "module" is in Android or how it compares to a "library" so I decided to create a module and see.

The created module looks a lot like a project and it let me create classes just like a regular project. It also added a new Gradle file to the project I was working on when I clicked on File/New Module...

I created a trivial class called Foo which has a single method named writeLog() which, as you might imagine, writes a message to the Log. Then I went back to my current class and tried to execute writeLog() in it. But my project couldn't see Foo and had no idea what it was. Clearly, this was NOT a winning approach so I guess I'm going to look at the least old of the YouTube videos and see if they explain the process clearly. I may just need to do one little thing differently or maybe I have to do EVERYTHING differently :-)

Anyway, I'll poke around a bit and if I figure it out, I'll post the information I used to create my Library.
 
Just to follow up, I found a short recent video on creating a library here:

I followed the steps, including doing the final assembleRelease, and all of that seemed to work.However, when I tried to *use* the trivial methods in my common library, the class names were not resolved, even after I typed in the appropriate import statements myself.

What step is the video missing?

Oh my goodness! I just watched/skimmed a couple of additional videos on this topic. It looks very much as if I can't actually *use* my library until I go through all the rigamarole of publishing it to Git and then setting up a dependency to Git so that my project can see it!

Is that really true? Is there no way to use my shared code directly from within Android Studio? This was never a problem within Eclipse. Or are these videos just "looking ahead" to when you implement this code in the real world as production code?
 
Last edited:
In case anyone finds this thread later, it looks like you do NOT have to get GitHub involved if you want to create shareable code. I found two discussions of the techniques for creating an Android library (also known as a module):
I have nothing against GitHub but I'm not ready to put my code out there yet. I wanted a way that I could create common code and use it within my projects without getting GitHub involved. I'll probably use GitHut or something similar when my code is truly finished.
 
You certainly don't have to use Github to do this. Github is primarily a source control versioning system.
In fact you don't necessarily have to create a module, because Java has a standard mechanism for using common library code. It's called the JAR file. If your library just involves Java code, then a standard JAR would do the job.
But it's great that you now have a procedure to do this.

And on a side note, if you want to use a source control system, there are options for that. You don't have to use the public Github repository. You can use a completely local code repository if you wish. You don't have to push your code to a remote repository.
 
You certainly don't have to use Github to do this. Github is primarily a source control versioning system.
In fact you don't necessarily have to create a module, because Java has a standard mechanism for using common library code. It's called the JAR file. If your library just involves Java code, then a standard JAR would do the job.
But it's great that you now have a procedure to do this.

And on a side note, if you want to use a source control system, there are options for that. You don't have to use the public Github repository. You can use a completely local code repository if you wish. You don't have to push your code to a remote repository.

As I was reading the cited websites, the option of using a JAR became clear and I may go that way. I've used them - and even created them - before in Eclipse. But the additional things you can store in an AAR are intriguing so I tried following the instructions on those pages to create an AAR. Unfortunately, I had only limited success. I didn't find enough detail in the first one so I tried the second one. I was able to create a common code module for one project and even managed to "see" the code in the main project. But when I tried to do the exact same thing in a second project, it wouldn't work. Then I did some more searching and found the third answer to this question, which seems very promising: https://stackoverflow.com/questions...android-studio-and-an-application-project-tha. Unfortunately, I'm not finding it clear enough that I can follow it.

I like its approach of having all the common code in a project of its own but then usable by any other project that needs the code. I also like the ability of having XML files like layouts in there. It would let me make activities with screens that could be used in multiple projects for things like, say, error handling or logins. I'm not sure how to get a more complete explanation of that third answer though. Any ideas? Or is it so clear to you that you can explain it to me in a bit more detail?

As for GitHub, I have a superficial familiarity with it from having done its beginner's tutorial. I was also working through a Udacity course on Git (as opposed to GitHub) but ran into problems with bugs in the course so I've put that aside for now. However, I did get as far as setting up my own Git repository on my laptop and will be quite happy to use it for my code once I get back to learning Git properly. (Any suggestions for a better course than the Udacity one?) And ultimately, when I get this app working 100% to my satisfaction, I want to put it on GitHub and write a tutorial or blog or make videos to explain all the key parts. One of my biggest frustrations with Android has been the inability to find really good examples to imitate, examples that do real things rather than trivial placeholder things. I get why the videos simplify things; if they didn't, they'd be hours long rather than minutes long and no one would finish them. But somewhere along the line, people need non-trivial programs to learn from and I may just take that on with at least one serious program that uses RecyclerView, AsyncTasks to read and write from a MySQL databases, preferences, and proper error handling.

But I'm not ready to publish that example to GitHub yet; it needs to work to my satisfaction before I do that. Having the ability to put some utilities and convenience methods in a common code module would help me organize my code better. Two videos I saw both talked about putting that common code out on GitHub and left me with the impression that this was the *only* way to have common code in Android. That astounded me and made me ask whether it was the only way. Luckily, the impression they gave was clearly wrong :)

Now if only I could get this module to be visible from any project that needs it, I'd have one major item on my TODO list settled....
 
Back
Top Bottom