Difficulty with Android library to keep as dependency - android

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

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.

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.

Migrating from Eclipse to Android Studio and library issues

I have decided to migrate my projects from Eclipse to Android Studio and I am finding a lot of difficulties in doing so. The biggest problem I have is the following:
I have several applications that are using some common projects (libraries). When importing an application into Android Studio, it detects the referenced libraries and adds them to the application (copies the folders of the libraries under the application folder). This is something I do not want to do because the libraries are common for more that 1 applications so I want them to be at the same directory level as the application folder and just reference them from the application. How is this done in Android Studio? In Eclipse, you would just add a reference from the project properties. Furthermore, how do I import a library project in Android Studio? I would like to import each of the libraries as separate projects and build them individually.
Is there any good tutorial about this? I have a lot of experience with Eclipse but I am completely new to Gradle.
Here is an example of what I mean:
I have two projects, ProjectA and ProjectB. They both use some library projects developed by me, LibA and LibB. All 4 projects are under the same folder called Applications - the two libraries are therefore common and if I change one of them I only need to recompile both projects. After importing ProjectA into Android Studio (having the latest version), the import process created the following structure. Under the base folder, Studio Applications, I have a folder for the project named ProjectA. Under folder ProjectA, I have another folder, ProjectA (where the main project files are) and two more folders, LibA and LibB. What I would like to have is have the two library projects imported separately (as library projects in Android Studio) and under the main application folder have only the ProjectA folder (and the rest of the standard gradle folders, ie, .gradle, .idea, build etc).
How can this be done? I need two things: First, import a library project from Eclipse into Android Studio and then import a project but instead of having the libraries copied under the project path, reference them.
Just import your apps with no project libs (I'm assuming the libs are available in maven)
After that you just need to add the dependencies in gradle file
// your gradle file inside your app folder
dependencies {
compile 'com.squareup.okhttp:okhttp:2.3.0' // example of importing okhttp
compile 'com.facebook.android:facebook-android-sdk:4.1.1' // example importing facebook sdk
compile project(':name_of_local_project') // local project
}
and if you want to include a project of another folder (different project)
add this to settings.gradle
project(':module1').projectDir = new File(settingsDir, '../another_path')
It's a simpler approach, and you get the ability to update the lib without much work
The problem was that I had proguard enabled for the library modules as well. Proguard was obfuscating class and function names in the library modules and they were not found in the app that was referencing them. Moving/merging the proguard files in the app was the solution.

Shared Library On Android Studio

I'm switching from Elcipse to Android Studio. In Eclipse I have some "library projects" (no jar files, but Android projects marked as "library") that are used by several apps. In Eclipse I used to have this library project in only one place, and I liked to modifiy the library only once, and see the effect on all the other projects depending on it.
In Android Studio, when I add a library module, all the code is copied inside the app. I don't like it, because it's easier to maintain having it only in one place. Is there any way to reference the original library instead of copy it inside the project?
I've been reading a lot of about this, but I can't find the right solution. These are the ways I'm thinking to do it:
a) Aar files and local maven repository: I would compile the library module into an aar file, and I'll save in an local maven repository, so I can reference it on my projects. But this has a drawback: every time I want to modify the library, I need to recompile to an aar file before I can test the changes in my project.
b) Symbolic links: Instead of copy all the library module inside the app folder, I'm thinking in creating symbolic links to the original folder. So the library will stay in only one folder. But I think this approach is so tricky, and there must be a better way to do it.

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