Modify files from maven library - android

I'm developing my project using Android Studio and I have a library imported as module into my project with some xml files modified to fit my needs. My question is if there is a way of modifying those resource files if I add the library as a maven dependency or it can be done only by adding it as a module.
Thanks!

If you add a library as a Maven dependency, e.g.
dependencies {
compile 'com.foo:1.0.0'
}
it's going to download that dependency from the Maven repository in whatever format is specified in the artifact, typically JAR or AAR. If it's downloading a prebuilt library like that, you can't make any local changes to it.
If you need to make changes, your best bet is to include it as a module instead of a Maven-style dependency like that. However, if this is a common library that will be shared among multiple projects, you have a number of options to make that sharing easier. You could build it as a library and include it in your projects via that Maven-style include (perhaps deploying the library to a local or organization-wide repository), or if it suits you better, you could put the library in a common place on local disk and reference it in-place via your settings.gradle file.

Related

How to reference multiple aar artifacts in a com.android.library module?

In my Android application project I am trying to avoid referencing an external Maven repository in my project from where I would load multiple .aar artifacts which make up one SDK.
Instead, I would like to put the .aar files into my project and then reference them from one of my Gradle Android library modules.
I already tried different approaches - none worked, though.
Approach 1: Composite build (includeBuild)
Inspired by https://stackoverflow.com/a/72672032/35689
Here my artifact(s) cannot be resolved by Gradle.
I also tried this in isolation with the sample project - for some reason it does not work with my artifacts. In the example there is only one .aar file which might be the reason.
Approach 2: One module per aar
Inspired by https://stackoverflow.com/a/70074787/356895
Here I end up with this error:
jetified-externallibrary-1.2.3/res/values/values.xml:113:5-122:13: AAPT: error: style attribute 'attr/shimmer_auto_start' not found.
You have to manually add all of the .aar file's dependencies to your project in order to make this work. You will also have to substitute the maven dependency with the .aar file manually.
The reason is that an individual .aar file does not contain any metadata like maven coordinates, version, dependencies, etc. Therefore Gradle cannot handle any of this for you automatically.

Adding jar file in libs vs. adding dependencies in gradle

We are using projectlombok in our android app. Our approach to use this library is -
We have added lombok.jar to the android studio - inside libs folder and using compile files('libs/lombok.jar')
However, on the project home page of projectlombok the approach explained to add it is like below,
dependencies {
compileOnly 'org.projectlombok:lombok:1.16.18'
}
Is there any performance or app-size impact of one approach over the other?
One of the biggest differences is the amount of work required to upgrade to a new version of the library. If you use Gradle, then all it takes is a small edit to your build.gradle file. The other option requires downloading the JAR file manually and copying it to the libs directory.
For JAR-only libraries, this difference is pretty small. However, for libraries which also include Android resources, a JAR file is insufficient. You have to build a AAR file instead and bring it into your build process. Gradle manages all of this for you.

How to add maven/repo libraries to old project without gradle?

Where can I include online (maven etc) libraries/repo in Android Studio 2.0 without build.gradle?
Before it was somethere in add library dialogs, but now i can add local jars only. But how to add online repositories to old projects without gradle?
Note, i do not want to include all library project/source, import module etc. Just want to link online libs as before.

How to convert apklib to aar

As Gradle does not support apklib dependencies how can one migrate from apklib dependencies to aar dependencies? Is it possible to either manually or automatically convert apklib dependency to aar? If yes - how, if no - why not?
In this question I assume that I don't have original project for apklib, but rather the file itself.
apklib doesn't contain object files, only source files, so the only way it can be included in any project is by recompiling it. According to some docs you can read at:
Android dependencies : apklib vs aar files and https://plus.google.com/+ChristopherBroadfoot/posts/7uyipf8DTau
The apklib format was invented as a way to share Android code+resources. It’s essentially a zipped up Android library project, which was already the status quo for code+resource sharing.
And you can see in Chris Broadfoot's post that the task that generates the apklib just zips up the AndroidManifest.xml file and the src and res directories:
task apklib(type: Zip) {
appendix = extension = 'apklib'
from 'AndroidManifest.xml'
into('res') {
from 'res'
}
into('src') {
from 'src'
}
}
Note that his post is about creating an apklib from Gradle, which is a slightly weird thing to want to do, but it provides a pretty clear idea of how one of these things is structured.
The good news is that this is "standard" Android project structure as of when Eclipse was the primary IDE, and Android Studio knows how to import modules that look like this. So follow these steps:
Unpack your apklib into a temporary directory. It's in zip format, so something like unzip library.apklib should work.
Look at what's unpacked. You should see a directory containing AndroidManifest.xml, src, and res folders.
In Android Studio, inside an existing project, choose File > Import Module and point it at the directory from step 2.
Android Studio should manage the import by copying the files into your project in a new module, arranging them in its preferred directory structure, and will add a build.gradle file for the module.
At this point you're up and running. If you actually want the .aar file, then check to see if your new module is set up as a library module or an application module. In its build.gradle, if you've got apply plugin: 'com.android.library' then it's a library module, and Gradle will generate an .aar as part of the build: it will be in the build/outputs/aar directory after a successful build.
If your module imported as an app module and not a library (apply plugin: 'com.android.application', then change that apply plugin statement, and you may also need to remove the applicationId statement from the build file. Now it should compile as a library and generate a .aar.
Happy coding!
You cannot convert the apklib to aar. You have to update the dependencies manually to point to an aar file. The aar is compiled and contain lint and proguard rules which the apklib can't necessarily determine automatically.
You can read a bit more on the differences here:
Android dependencies : apklib vs aar files

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