How to Share a Single Library Source Across Multiple Projects

How to share a single library source across multiple projects

(as of version 2.1+):

Below are the steps that I took to share library source outside of the project directory. Instead of plain Java library, my codes are compiled as Android module, but that library module is not contained inside the main project. It is fine with me as long as there are no code duplications and I only need to maintain one set of library code for all projects:

1) File->new Project. Give a name to your library project (here I use LibraryProject). Continue the remaining steps to create a normal project (since this is intended as a library, I chose "add no activity")

2) By default, Android studio creates the module named as "app" inside the project folder. To prevent names collision with the actual application module, rename the module to something else (Right click "app" module at left panel->Refactor->Rename).

3) In the build.gradle inside your library module folder, change the top line

apply plugin: 'com.android.application'

to

apply plugin: 'com.android.library'

and then remove the "applicationId" line under "defaultConfig". Also, since this is a library, remove the xlmns:... namespace and <application ..> body from Manifest.xml as well.
That's all for the library part. Now, to create/modify your main application:

4) If it is a new project, first create new project File->new Project->ApplicationName.

5) Open settings.gradle (there should only be one such file in every project) and include the following line (note the missing leading semi-colon in library module):

include ':app', '..:LibraryProject:yourLibraryModule'

6) Then go to File->Project Structure.

7) Under the tab "Dependencies" click the green "+" button at right. Select "Module dependency". Choose your library module, then click OK.

You should now be able to use the library classes in your application.


ALTERNATIVE METHOD
If, for some reason, there are still problems with the above method, you can try the following (suggested in here):

1) Repeat steps 1 to 4 above. By default the main and external (library) project look something like this:

  /MainProject
+ build.gradle
+ settings.gradle
+ app/
+ build.gradle
+ src/

/LibraryProject
+ build.gradle
+ settings.gradle
+ app/
+ build.gradle
+ src/

2) As usual, refactor the modules name (in android studio right-click module->refactor->rename) to something less confusing, such as

  /MainProject
+ build.gradle
+ settings.gradle
+ MainModule/
+ build.gradle
+ src/

/LibraryProject
+ build.gradle
+ settings.gradle
+ LibraryModule/
+ build.gradle
+ src/

3) Modify the settings.gradle in MainProject:

include ':LibraryModule', ':app'
project(':LibraryModule').projectDir = new File(settingsDir, '../LibraryProject/LibraryModule')

Sync the project and you're done.


Note on Proguard

Currently you should not use a proguard on external library projects/modules. Instead, you replace the following (original answer here)

buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
debug {
minifyEnabled false
}
}

with the following (in build.gradle of the library):

buildTypes {
release {
consumerProguardFiles 'proguard-project.txt'
}
}

where proguard-project.txt is the file that contains the proguard rules for your library project.

multiple shared libraries from one project with common source files

There are two ways to solve the problem.

  1. You should use subprojects. Extract your common source-files into separate subproject as static library. More info here

  2. You may create .pri file:

    SOURCES += plugin.cpp
    HEADERS += plugin.h

and include it in all .pro files:

include(deployment.pri)

What's the correct way to include shared libraries across multiple projects (for purpose of CI)

It is more natural for CruiseControl.NET to work at the solution level. I would suggest checking out your entire repository and using different triggers per project (as you state in your own comment:)).

Sync shared library projects/modules with its source

Ah yes, this can be very helpful in many cases. You can do the following to achieve this.

Let's say you have two projects - MyApplication and MyLibraryDemo containing the library module libmodule with the following paths:

MyApplication - "/../AndroidStudioProjects/MyApplication"

MyLibraryDemo - "/../AndroidStudioProjects/MyLibraryDemo"

libmodule - "/../AndroidStudioProjects/MyLibraryDemo/libmodule"

And let's say you are trying to use libmodule in MyApplication.
Then, in your settings.gradle of your MyApplication project, do this

include ':app', ":libmodule"
project(':libmodule').projectDir = new File(settingsDir, '../MyLibraryDemo/libmodule')

You may have to make relevant corrections, but I hope the idea of linking another module is clear.

All the best :)

Source Control for multiple projects/solutions with shared libraries

You don't mention in your question what source control you are using. As it doesn't sound like you need to limit your outside developers access to the rest of the repository I would not bother with setting up multiple repositories. I would assume that unless your code runs into the millions of lines size that repository size is not an issue.

It all depends what functionality your revision control system supports. In subversion you can declare other folders as external and provide a file URL for the content of that folder, this will cause subversion to deal with that folder as a separate repository even though it is within your folder structure.

How to share react component across multiple projects?

There are multiple ways depending on your setup. As @Alex Wayne suggested the easiest way is to use a monorepo. If you setup does not allow it, you can publish a package to npm. If you can't or don't want to package a public package, you can publish private packages (with a payed account) or use a private package registry, such as GitHub packages (also payed for private packages). There are also self managed solutions for package registries if you want to safe some bucks in exchange for some work.



Related Topics



Leave a reply



Submit