Use different Git repo for different modules - android

In my Android application I've created a new library module. Now I have this structure:
Right now I have under version control on Bitbucket whole project less datingcorelib. I would like to use a different repo to this library module.
It's possible to use two different repos on the same project?

Yes, it is possible. You're looking for a thing called submodule.
However, it may be tricky to use such a submodule within a project as it will have its own structure. So you also need to include the right gradle project from that submodule.
Let's say you pushed your library project somewhere. Let it be git#github.com:Sami/my-library.git. We also assume that it has the common structure for Android library project, i.e. it has a root build.gradle file and a subfolder datingcorelib with actual source code. This is what we need to include into the app.
You need to delete datingcorelib from your app's project. Then add the library as a submodule:
git submodule add git#github.com:Sami/my-library.git libraries/datingcorelib
After that open settings.gradle file of your app's project and add a new line there:
project(':datingcorelib').projectDir = new File("$rootDir/libraries/datingcorelib/datingcorelib")
Sync the project. Now you should be able to use the code from another repository.

Related

How to create and use an Android Library within existing project

I have an existing android project project one, in which I want to add a feature lets say SpecialToast. Instead of adding it to the project directly, I want to create this feature as an android library so it can be used in other projects project two.
I am a bit confused on the procedure how to create.
I have gone through https://developer.android.com/studio/projects/android-library and I tried to create a library within project one which does create a library.
New -> New module -> Android library
what is the right procedure
Create a brand new android application and in that application create a new Android library.
In Project one create a new android library.
If option 2 is correct, once I create a android library within project one how can I use it in project one
If option 1 is correct, how can I publish that and test easily with project one
Thanks
R
Update
Both options are possible.
You can create the library module in a separate project and build and export it from there (your option 1). You can export your library by using the gradle wrapper task assemble. For example, if your library module is called brdroid, execute this from your project's root folder:
./gradlew brdroid:assembleRelease
This will create the brdroid-release.aar file in the folder {project root}/brdroid/build/outputs/aar.
You can then either copy the aar file to another project or upload it to a package manager. Either manually or for example with the maven publish plugin.
If you upload it to a package manager, you can add your library as a remote dependency just like you do with other libraries by using implementation or api + your package name, artifact id and version number.
For debugging purpose it is easier to use the library module in a project directly (this is your option 2). Meaning, that an app module and library module are in the same project side by side. In this example, if both modules are in the same project, you need to add the brdroid module as a dependency to project one by adding this in the project one build.gradle file:
implementation project(':brdroid')
Note, that you can still export your library as an aar file and publish it like in option 1.
And in this scenario, it's still possible to keep the library module in a separate project and repository and add it to your app's git-repository and project by using git-submodules. This is a little bit more.
Using git submodules
An example usage of git-submodules can look like this: Let's consider we have this two projects which are in separate git repositories:
My Library which has only the brdroid android library module
MyApplication which has only the app android application module:
In order to add the MyLibrary repository as a git-submodule to the MyApplication git repository, execute this from the MyApplication root folder:
git submodule add -b master https://{url-to-My-Library-git-repository}
The -b master only tells which branch to track. You can change it.
This clones My Library into a new subfolder which is not tracked by the MyApplication repository. It also creates two changes which you need to commit in order to make them permanent.
Next, open settings.gradle from the MyApplication root folder and add the brdroid module to the MyApplication project by changing the first row from:
include ':app'
to
include ':app', 'MyLibrary:brdroid'
MyLibrary is just the folder in which the brdroid module resides.
Also, add brdroid as a dependency to the app module by adding this to the build.gradle file of the app module:
implementation project(':MyLibrary:brdroid')
Your result should now look like this:
Now, app and brdroid are in the same project but the development of brdroid happens in a separate git repository.

Difficulty with Android library to keep as dependency

I have a few different apps and I want to create a module/library in Android Studio to re-use some activities, layouts and drawable in all of my apps, like an underlying framework.
I can only create a module within an app project, not as a separate project. I removed this folder from the app path to another folder outside this app (I want a separate Git repo for this). If I then import this module in my different app projects, it copies this module. So the changes I make in my current app project in the module, do not affect the module in my other app projects. I can't imagine I have to merge this manually every time. How to do, if not import? Further, if I open this module as a project with Android Studio, I can't build it because of apply plugin: com.android.library.
I went through this page and some Google search, but I am still confused: https://developer.android.com/studio/projects/android-library
How is it possible to have a module or library which is re-usable (activities, layouts) in many apps and has its own repository?
Is the only possibility to have the Android lib or module in one project and import in any other project as a .AAR ?
From android studio project terminal add git sub-module
git submodule add https://XXX#bitbucket.org/YYY/ZZZ.git
make sure the sub module save location folder name is different than the original library project name, else you might get
conflicts.
select ‘Import Gradle Project’
select the sub-module folder
give the actual sub-module project name
Now you have an application project in git, which uses a library that is added to it as a submodule in git. Now you can develop on the application and on the library parallelly. It's better to keep a separate branch of the library for an application so as not to conflict with another application usage, and if the library code changes can be used in other projects also you can make a PR request to the main branch of the library.
for more references follow this link
https://medium.com/#deepakpk/how-to-add-a-git-android-library-project-as-a-sub-module-c713a653ab1f

How to add the library live to my project?

I can create my jar file and add it to my android project as a library and use it. But I want any time I made a change in my library, without recopying jar file to my project, apply changes in it.
I don't want use Git
You can push the .jar as an artifact to JFrog or some Artifact Hosting Repository. It'll give you a unique URL which can then be included in your Gradle file.
Everytime you push your new Artifact, you'd mostly increment the version number, so you'll need to do the same in your project. Just like you would for any other library.
https://www.jfrog.com/confluence/plugins/servlet/mobile?contentId=46107909#content/view/46107909

Android Studio and Library Projects [duplicate]

I'm creating projects with dependencies in Android Studio. I know how to link projects by adding modules.
But I realized that 'importing modules' create a copy of the libProject inside the project.
Is there a way to prevent that ? Like an 'external module' ?
Since i'm in charge of both project, I want to be able to push changes to the libProject Repo, without having to copy paste files between folders.
Thanks
Yes, you can do it. The module needs to have a Gradle build file set up for it. If it's got that, then in the project you're linking to it, add this to the settings.gradle file at the project root:
include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')
where the path you specify in the second line is the path to the directory containing the library's build.gradle file. The path can be relative or absolute.
The solution:
include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')
was not working for me. After couple of wasted hours I figured out the issue. There are two build.gradle files, one for project and one for library name. If the library is in the folder '\MyLib' then there will be a build.gradle in '\MyLib' and another at '\MyLib\app'. You have to point to the '\MyLib\app' and not '\Mylib'.
Hopefully this saves some time for others.
If you have, like myself, have multiple modules (I only realised today that copies were included, I thought that the project included links to the source.)
You can have multiple modules/projects along the lines of :-
include ':app', ':sqlwords', ':dbindex', ':dbcolumn', ':dbtable', ':dbdatabase', ':displayhelp', ':pickdate'
project(':sqlwords').projectDir= new File('d:/Android_Applications/Modules/sqlwords')
project(':dbcolumn').projectDir= new File('d:/Android_Applications/Modules/dbcolumn')
project(':dbtable').projectDir= new File('d:/Android_Applications/Modules/dbtable')
project(':dbindex').projectDir= new File('d:/Android_Applications/Modules/dbindex')
project(':dbdatabase').projectDir= new File('d:/Android_Applications/Modules/dbdatabase')
project(':displayhelp').projectDir= new File('d:/Android_Applications/Modules/displayhelp')
project(':pickdate').projectDir= new File('d:/Android_Applications/PickDateShowCase/pickdate')
You can also use android { sourceSets{ main.java.srcDirs += '../../../library/src' }} in your app build.gradle . Not sure about supporting all android resources, for pure java library works well.

Is it possible to share library projects between projects when using Android Studio and gradle?

I'm struggling to figure out how to import library projects into Android Studio in a fashion that makes them available to multiple projects. The documentation for the new build system implies that you need to import library projects into the root of the project you are working on:
Gradle projects can also depend on other gradle projects by using a multi-project setup. A multi-project setup usually works by having all the projects as sub folders of a given root project
(http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup)
This is problematic since, as I mentioned above, I would like to keep frequently-used libraries accessible to all current and future projects. For example, I am currently trying to integrate Volley into a new project to evaluate it, with the assumption that I will want to use it in multiple other projects in the future. I cloned it to my "${PROJECT_ROOT}/" folder, which is the same level at which I created my test project, giving me:
${PROJECT_ROOT}/TestProject/Test
${PROJECT_ROOT}/volley
After trying to set up my project's build.gradle file in a variety of ways, the only way that I've managed to make the app compile is to move Volley into the TestProject 'main' project, giving me:
${PROJECT_ROOT}/TestProject/Test
${PROJECT_ROOT}/TestProject/volley
Not only does this prevent me from using Volley in other projects that are not a part of TestProject without duplicating it or cloning it a second time, but it means that git wants me to add it to the repo I've established at the root of TestProject.
How can I reference library projects in my Android Studio projects without including them in the projects themselves?
Couldn't you use git with submodules? e.g. In your .gitmodules file, you may add this:
[submodule "volley"]
path = volley
url = https://path/to/volley/repository
You can set the url to Volley's official repository, or to your own in-house version of volley. Other projects can be setup the same way and point to the same volley repository.
I think this way, other users can call git clone and all the dependent projects will be downloaded within the main project folder and they don't have to worry about downloading the library projects separately.
For Volley though, I would just compile it into a JAR file and stick it into the /libs folder of the main project. That is, if you don't need to modify its source.
[Update]
For library projects that you don't need to modify its source, you can try using Android Studio to compile them into AAR files for sharing. AAR file is like a JAR file to Android, so you can add them to your /lib source folder, or publish them to your local/intranet maven repository. If you choose the maven route, add your local/intranet repository in build.gradle, and reference the library project that you've published.
Hopefully in the future more Library projects owner will build their projects into AAR file and publish them to Maven Central Repository, so we can just reference them directly from the build.gradle file.

Categories

Resources