Code exchange in Android Studio

I started working on a project where I would need to share a bunch of Java classes through many applications. In Eclipse you could create one project with all such classes and use it as a library in the workspace with all your dependent projects, but in Android Studio this is not possible (at least not easy).

I read a bunch of posts, and many of them suggest setting up a library project by creating an aar file and then using it 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 the client, and I want the code base to be closed.

In addition, I know that a module can be imported into a new project. But this creates a COPY of the source module. This is not at all what I want. I do not want to support multiple copies of the same classes, which completely violates the goal of "code exchange."

Is there a good way to achieve what I'm looking for? Any help is appreciated.

+6
source share
4 answers

You have several different options.

One option is to maintain your libraries as separate projects and compile them into an archive format such as a JAR or AAR; JAR files are for pure Java libraries, and AARs are for Android libraries (which contain code that accesses the Android API and / or has Android resources). As noted in the comments, AAR does not force you to publish your code in the world more than JAR files; it is simply an archive file format whose files may be local to your computer or your organization.

Using this archive file, you can include it in other projects. If you are part of an organization with several developers, you may find it convenient to use the repository manager to publish and maintain these libraries within your organization , and you can use the Maven specifications for coordination to include libraries in your projects that you do not need to manually copy to your development machine.

The disadvantage of this approach is that it is a little more difficult for it to make changes to these libraries: you need to download the project, make changes, create an archive and distribute the archive.

Another approach is to save the library as a source module, as in Eclipse. You noticed that Android Studio will make a copy of the module if you import it through the user interface, but if you bypass the user interface and make changes to the build scripts directly, you can do what you want, namely use the module in place and share one copy several projects. To do this, work in the settings.gradle file and add this:

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

I would strongly recommend that you not use the pure relative path here; in this example, the path is bound to the settingsDir variable provided by Gradle, which is defined as the directory where settings.gradle is located. If you use a pure relative path (i.e. are not attached to anything), you depend on the fact that the working directory is the same in all environments where the build file (command line against Android Studio against the CI server) is executed, which is not good to assume.

+7
source

You need to think in eclipse projects as Android Studio / IntelliJ Idea modules. Then you can create Android (or java) libraries and then include them in your project.

To mark the Android Studio module as a library, you can go to “File” → “Project Structure” → “Faces” and click “Library Module”

enter image description here

+2
source

You can create and use the library without opening the source code or available to others.

First, you do not need to do this aar if it does not contain resources. If these are simple classes, you can simply make it a .jar file.

In any case, the easiest way to share these libraries (aar or jar) is to create your own repository. Nexus and Artifactory are the two most common repository managers.

You save the library in your own project and then publish it in your own internal repository.

Then the projects that should use the library are configured (in gradle) to use the internal repository and get the library from it.

0
source

I was in the same situation as you, and I founded the approach using git. Actions to have a library:

  • Create a project in Android Studio.
  • Create an Android library module in this project.
  • In this library module, create a git repository.
  • Add modulename.iml to the .gitignore file
  • Use GitHub or Bitbucket for private cloud storage. and click on your library.
  • Create a new Android library model in any project you want.
  • Close Android Studio (not sure if this is required).
  • Using Explorer, navigate to the folder of the created module.
  • Delete all data except modulename.iml .
  • Cloning your library from "GitHub" into it.

What all.

Now you can use the library in several projects, regardless of whether you are at home or at work. Once you're done, you remember to skip the changes in the library. And after opening new ones, pull them out. I think you can somehow automate this.

The advantage is that you do not need to create any jar or aar.

0
source

Source: https://habr.com/ru/post/979505/


All Articles