How to add the library live to my project? - android

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

Related

How to add a maven dependency locally in android project?

I am integrating with Samknows library. Access to their gradle dependency is password protected, hence, I want to add this library locally in the project so, i don't need this password anymore.
I am able to locate the jar and pom file of the dependency. I tried to add this jar in the project as library but seems to not work, as POM file attach to it has all the dependency.
I am not able to figure out how to add these dependence files locally in my project. Any suggestions or approach will be helpful.

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.

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 include external libs in Android source controlled project?

How should external libraries be included into Android projects?
I see this documentation from Google:
http://developer.android.com/tools/support-library/setup.html#libs-with-res
...which says they should be kept outside the source tree for the project, and referenced as dependencies.
The guide for Facebook libraries says the same thing:
https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/
What about when the project is going into source control, and will be worked on by multiple developers? Is it possible to be sure other developers will have the correct versions of libraries if they're not included in source control?
It seems as though it might be better to check in the whole tree of these external libraries under say an "external" folder in the project and then reference them as libraries from there? The above links don't say this is wrong, but is there any reason not to do that?
I could not find anything against this approach, but maybe my search skills are off.
Thanks!
You have basically tree options (referring to git):
Putting the source or binaries into your git repository.
You can create/clone extra repositories and link these as submodule into your main repository.
Use gradle/android-studio to maintain remote binary dependencies.
In my opinion, option 3. is the best. It speeds up build time and reduces the date saved in your internal repository. Referencing most open source projects, googles libraries and even the Facebook API is just a one liner in your build.gradle file.
For internal libraries or anything not uploaded to some maven repository, you can create a local maven repository and link that.
And in the end, you have the option 2. to create a library submodule within git and gradle to handle it efficiently.
If you want to stick to eclipse + ant, try 2. first.
At least ant will work out of the box for building all things.
Setting up eclipse is a bit more difficult but can be done.
Option 1. is easy to implement, but It might get messy at some point.
Copy jar file in android project libs forlder and right click on jar file and click on bulid path-> add to build path.
If you want to add jar file then copy your jar file and put in to libs folder, and if you want to add external library then import your library project go to project properties and select android tab and add external library with add button.

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