Sharing code in Android Studio
Smarter project structure for apps with multiple platforms.
Let’s say you’re developing a same Android app for Android Mobile, Android TV, Android Watch, and you start to notice that most of the code is repeating, for example API calls and models, POJO classes for JSON parsing, common constants that are used across all the apps etc… Well there’s a cool way to share those chunks of code!
For the sake of simplicity let’s use a blank project with 3 platforms: Mobile, TV and Wear. Now let’s say that we have a string that we would like to display on all of the apps. Instead of copying the string and pasting it in every project, we can make a new Module that we’re gonna call Common for example, and let that string live there.
So let’s do that! File > New > New Module… will open a window with Module options.
Go ahead and select the Android Library and click Next. Name it “Common” and make sure that the package name is the same as your other modules and append common at the end of it, like so: com.example.lazarnikolov.common and click Finish. You can now see the new common module:
Now let’s add a new Java class in the common module! Expand common, expand java and right-click on the first package, New > Java Class. Name it as you wish, in my case Constants. Add a dummy string, for example:
public static String sharedText = "This string is shared with the other modules!";
Now in order to use this “sharedText” string, open the build.gradle file of every module you want to share the project, and append this in “dependencies”:
compile project(":common")
Your dependencies of the build.gradle file should look like this:
After the project syncs with the Gradle files, our shared string is ready to be used! So let’s add a simple TextView with the ID textView:
Now let’s go to the Java class assigned with this screen, in my case MainActivity, and initialize the TextView variable with the ID:
Now let’s set the textView’s text to our shared variable “sharedText”. In order to do that, we need to import the common project’s package name. Expand the imports and append this line:
import com.example.lazarnikolov.common.Constants;
And now we’re ready to use our shared string! Let’s set the textView’s text to Constants.sharedText:
textView.setText(Constants.sharedText);
Your whole MainActivity class should look something like this:
And if we run this now, we’ll see that our sharedText string appears in the mobile app’s main screen:
Alright alright alright!
Let’s do the same for the other modules as well! Add compile project(“:common”) in the other build.gradle files, import the package in the activities where you’d like to use the string, and set it on a text view!
Would you look at that! Now let’s change the string in the common module and run our apps again to see the result:
Sweeeet! Now the possibilities are endless! You can share lots of stuff, for example, if you’re using Retrofit you can share your API calls, endpoints, models, you can share app constants that you use with SharedPreferences as keys for consistency and a lot more! It is a great practice if you’re working on projects that have multiple platforms and modules, it saves time and it prevents future headaches!
You can download the project here.
I hope you liked this article. Don’t forget to like it and share it around if you found it interesting.