How to create and use an Android Library within existing project - android

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.

Related

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

Use different Git repo for different modules

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.

How to soft import a module in a project in Android Studio?

I have 2 projects, MyLibrary, and MyProject. They each have their own repository. I want to develop and debug MyProject and MyLibrary together inside the same Android Studio window. I want to be able to add breakpoints inside MyLibrary and MyProject codes while I run my app. I tried using the Import module in Android Studio and it actually copied MyLibrary into MyProject repo. Is there any way I can just reference the MyLibrary as a directory path inside MyProject? So that I can commit my codes in their own repository?
Let's say my paths are:
MyLibrary - /Users/me/repo/MyLibrary
MyProject - /Users/me/repo/MyProject
Thanks
Add a module in your project repo, then configure its sourceSets (java, resources etc.) in module's build.gradle file to point to your library repo.
See this. And then add a compile dependency for your prjects. perform a gradle sync and you should be good to go.

How to include GitHut library in my project (Android Studio)

I am trying to add this library to my project because I want to change something in a class there. That is why I adding it as a dependencies in my gradile setting is not enough.
I know that in the library it says "If you want use this library, you only have to download MaterialDesign project, import it into your workspace and add the project as a library in your android project settings.". But what does that mean, should I copy all files and put them in my libs folder ?
Quoting the documentation:
If you want use this library, you only have to download MaterialDesign project, import it into your workspace and add the project as a library in your android project settings.
Step #1: Download the project, whether via the ZIP file that GitHub gives you or by using git to clone the repository
Step #2: Move the MaterialDesign/ directory into your project directory, as a peer of your existing app/ module
Step #3: Modify your settings.gradle in your project directory to include :MaterialDesign in the list of modules to build
Step #4: In your app module's build.gradle file, add compile project(':MaterialDesign') to your dependencies

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