Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code sharing in Android Studio

I have started working on a project where I will need to share a bunch of Java classes across a bunch of apps. In Eclipse it was possible to create one project with all such classes and use it as a library in a workspace with all your dependent projects, but in Android Studio it doesn't seem possible to do so (At least not easily).

I have been reading a bunch of posts and a lot of them suggest setting up a library project, generating an aar file and then using that in my projects. But, as I understand it, this will make my library open-source (Am I right?), which I don't want. I am doing this for a client and I want the code base to be private.

Also, I know that a module can be imported into a new project. But this creates a COPY of the original module. This is not what I want at all. I don't wanna maintain multiple copies of the same classes, which completely defeats the purpose of 'code sharing'.

Is there any good way of achieving what I am looking for? Any help is appreciated.

like image 584
Rameez Hussain Avatar asked Dec 12 '14 13:12

Rameez Hussain


People also ask

Can you share Android Studio project?

Once Android Studio selects the folder for you, it opens an Explorer, and selects a folder within your project folder. To create a zip you have to Right click it and select: “Send To/Compressed (zipped) folder”. With that, you get a “. zip” file that you can take with you, send over mail, share…

How do I collaborate a project in Android Studio?

Access the extensions marketplace in Android Studio (Android Studio >> Preferences >> Plug-ins) and quickly run a search for "collaboration." A list of extensions with different collaboration features will return.

Can we do coding in Android Studio?

You can download Android Studio 3.6 from the Android Studio page. Android Studio provides a complete IDE, including an advanced code editor and app templates. It also contains tools for development, debugging, testing, and performance that make it faster and easier to develop apps.

What is Android simple sharing?

Last Update date : Oct 14. 2020. Simple Sharing feature is an easier way to share large-capacity files with your friends via Contacts. Simple sharing is available in all applications that can use the Share via feature. (


1 Answers

You have a couple different options.

One option is to maintain your libraries as separate projects and compile them to an archive format, such as JAR or AAR; JAR files are for pure Java libraries, and AAR is for Android libraries (which contain code that accesses Android APIs and/or has Android resources). As was pointed out in the comments, AAR doesn't force you to publish your code to the world any more than JAR files would; it's just an archive file format whose files can be local to your machine or your organization.

With that archive file in hand, you can include it in other projects. If you're part of a multi-developer organization, you may find it convenient to use a repository manager to publish and maintain those libraries within your organization, and you can use Maven coordinate-style specs to include libraries in your projects, which you don't have to manually copy over to your development machine.

The disadvantage of this approach is that it makes it a little harder to make changes to those libraries: you need to load up the project, make changes, build an archive, and distribute the archive.

The other approach is to keep the library as a source module like you did in Eclipse. You observed that Android Studio will make a copy of the module if you import it via UI, but if you bypass the UI and modify the build scripts directly, you can do what you want, which is to use the module in-place and share a single copy among multiple projects. To do this, work in your settings.gradle file and add this:

include ':module_name'
project(':module_name').projectDir = new File(settingsDir, '../relative/path/to/module')

I would strongly encourage you to not use a pure relative path here; in this example, the path is anchored to the settingsDir variable supplied by Gradle, which is defined to be the directory where settings.gradle is found. If you use a pure relative path (i.e isn't anchored to anything), you're dependent on the working directory being the same in all environments where the build file is run (command line vs. Android Studio vs. CI server), which isn't a good thing to assume.

like image 196
Scott Barta Avatar answered Sep 20 '22 03:09

Scott Barta