Multiple AAR files - android

I am using Android Studio 1.2
I create a private library I want to use that one in another application.
To use it I create an AAR files, but this AAR don't work. I have in my library a dependency to an AAR file.
The AAR files do not the dependencies?
If I use the jar and I includ ans create all the dependencies the project works fine.
NOTE :
I know how to importe the AAR file. The problem is to use an AAR in the AAR..
Thanks.

If I'm understanding your question correctly, there are 3 projects involved:
Library Project 2 --> Library Project 1 --> Application Project
You are editing "Library Project 1" and have added to it's app/build.grade a dependency on the Library Project 2's aar. Something like this: compile 'com.arasthel:gnavdrawer-library:1.1.5'
I am not sure where you are running into an issue, but I'll attempt an answer anyway. If I'm completely off-base, can you please elaborate on how the AAR dependency is not working? Any error messages?, a class/resource not found, etc.
I think it's unlikely you are unable to use a class from Library Project 2 inside Library Project 1, because I just tried this myself and it seems to be working just fine. It's worth noting that the Library Project 1 aar file will NOT include classes or resources from Library Project 2. Library Project 2 will be noted as a dependency in Library Project 1's pom if published using gradle's maven plugin to publish Library Project 1.
My guess is that you are having a problem in the Application Project? Perhaps the class from Library Project 2 is not found in the Application Project?
If that is correct, then there are two possible solutions:
Enable transitive dependencies on the aar dependency in the Application project's app/build.gradle: Instead of compile 'com.example:myLibrary:versionX', make it compile('com.example:myLibrary:versionX'){transitive=true}. I just verified this causes gradle to read Library Project 1's pom and automatically add dependencies found there into the Application Project.
If you would like to use transitive dependencies, your Library Project will need to be generating a pom and publishing it along with the aar. See https://stackoverflow.com/a/30085677/431296 for some additional information on how I have this working.
Manually add the dependency on Library Project 2 to the Application Project - so that your Application has a dependency line for both Libraries. Depending on your specific situation this may or may not be a workable solution.

Add following code to you project build.gradle file, and you should put you AAR file to the libs folder.
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
And finally add compile info to your dependencies:
dependencies {
compile(name:'AARFileName', ext:'aar')
}

Related

Android imported AAR how to keep changes in sync with original AAR project?

If I have project-A which is a SDK and its being exported as a AAR into project-B which imports the module as a AAR how could i go about linking the two modules so changes made to the AAR in project-B don't have to be duplicated in the original project-A?
Create a Project A (library), create project B(application) and put them in the same project.
update the settings.gradle file like this:
include ':projecta', ':projectb'
And make sure the projects are in the same directory
.gradle
.idea
projecta
projectb
etc
By doing this you won't have to explicitly add the aar to project B because they will be in the same project and both automatically be built when you make a release.
Edit about linking with multiple projects
In the case you want to have links to multiple projects your best bet is to create a maven repository.
Doing so will allow you to use your project in the following way:
build.gradle
dependencies{
implementation com.mydomain.projecta:1.0.1
}
Any project you want to use you project A with can be used by writing the line above.
When you update project A you update your maven server and change the version to 1.0.2
You also will have to update the dependencies in the linked projects:
dependencies{
implementation com.mydomain.projecta:1.0.2
}
I think that is the most stress free way to distribute your SDK in one place and easily resuse it in multiple places.
Here are some links about setting up a maven server
https://inthecheesefactory.com/blog/how-to-setup-private-maven-repository/en
https://www.androidauthority.com/add-a-github-library-to-android-studio-using-maven-jcenter-jitpack-777050/

Add an Android binary library to an Eclipse project without gradle

I have a legacy eclipse Android project. The classic old way without gradle.
Suppose I found a library I want to integrate with, for example 'commons-codec', than quoting from gradle manual https://docs.gradle.org/current/userguide/organizing_build_logic.html#sec:external_dependencies, I need to paste the below in my gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath group: 'commons-codec', name: 'commons-codec', version: '1.2'
}
}
Now my eclipse project does not use gradle.
The Question: Is there a way to get the binary lib & integrate with it in eclipse (something similar to download the library's jar and add it)? I know this question is bizarre since I need the jar to be in the repository, but I want to be sure I'm not overlooking anything.
Many Thanks!
If you're just using a jar, you can include it in the libs folder of the Eclipse project. But that's code only. If there's resources in the library, you'd need to build an Android Library project out of it and include that in your project. Which I'm not sure how you'd do without code.
If you are having .jar file the simply right click on your project then
Properties-->Java Build Path-->Libraries-->Add Jar/ Add External Jar--> Apply
And if you are having library in your workspace then
Properties-->Android-->Add-->locate your library-->Apply

AAR file updation when imported and added as a module in an Android project

I am using a library by importing the aar file which creates a module. And this module is then added as a dependency.
I did it for one of the libraries I'm using in my app and it worked fine. But now I want to update to the latest version of the library I'm confused about the following:
How do i find out which version I'm using? Don't remember what the version was when i imported the aar file. Is there a way to check this?
I want to update the aar with its latest version. How do i do that? Do i simply re-import and it will overwrite? or do i delete the existing module and import again?
I found only this one related post on SO and it did not answer any of the above two questions. It suggests another way of using the aar but I want to know how to continue using the import aar approach.
The aar file in question is that of millennial-media
How do i find out which version I'm using? Don't remember what the version was when i imported the aar file. Is there a way to check this?
There isn't a standard way to know the version of your aar.
You can use the name or a file inside the aar or you can simply use doc.
I want to update the aar with its latest version. How do i do that? Do i simply re-import and it will overwrite? or do i delete the existing module and import again?
You can simply overwrite the aar file.
Importing the aar file means:
copy the aar file in a folder
add the dependency and the repository in the build.gradle
Somenthing like:
repositories {
flatDir {
dirs 'libs'
}
}
and add the dependency:
dependencies {
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}
When you want to update, just copy the aar file overring the exiting file.
I suggest you using a maven dependency. In this case someone uploaded the library in a maven repository.
Currently it is the best solution in my opinion.
In this case just add a dependency in your project
dependencies{
compile 'group:name:version'
}
and it is very simple to know the version and to update the library.

Dependency error when using aar library

I have a module with a .aar file in libs folder. I used the solution posted here
[1]: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/ to add the .aar file as dependency and was able to compile the module properly.
Now I want to use this module as a dependency to the main module in my project and compile. However when i try to compile, i do see an error which says that gradle was not able to find the particular .aar file. why would my main module not find a file which is in the libs folder of my sub module. Was wondering if anyone came across this issue.
my project structure is like this
--mainmodule
--build.gradle (submodule as a dependency)
--submodule
--libs
-- abc.aar
Here is the error gradle throws: When unzipping library ':abc:, either group, name or version is empty
If I understand your problem right and you've followed the steps described in the link you shared, then adding this to your mainmodule's build.gradle should do the job:
flatDir {
dirs "../submodule/libs"
}
You basically have the same issue that you fixed in your submodule, since the mainmodule is struggling to resolve transitive dependencies (abc.aar) of submodule.
Recommended way:
While the answer above should fix your issue, Android Studio supports a better way to do this. Import a local aar file via the File>New>New Module>Import .JAR/.AAR Package option in Android Studio v1.3+.
You can then have your submodule depend on that aar-module as follows:
dependencies {
compile project(':aar-module')
}

The best way to integrate third party library in Android studio

We can find some very good open source libraries for android. I want to know what is the best way to integrate them to our own projects in Android studio. Here are some basic methods:
Copy the source code and resource files into our own project. We need to change a lot of codes (the package name, and the name in xml,etc)
If jar files is provided, I just create libs folder for my project and copy the jar files inside. And add the jar file in Module setting's dependencies. But unfortunately I got a lot of error messages like "Gradle: Package com.google.gson doesn't exist".
Is there a general rule to add third party source or jar files into an existing android studio project? Thanks
I prefer to use central repository for dependencies management. So for gson 2.3 dependency you should add to build.gradle file:
Specify that you want to use maven central repository for your dependency
repositories {jcenter()}
Add compile dependency to gson 2.6.2
dependencies {compile 'com.google.code.gson:gson:2.6.2'}
Android Studio as well as your CI server should easily build your project now. And you can continue app development.
I prefer to use central repository for dependencies management because:
easier scope management - some libraries are only for testing, some should be included to apk and some are part of running environment (like android.jar itself)
easier transitive dependencies management - it is quite hard to collect libraries dependencies and if you use "jar-with-dependencies" you could get error "class already added" during dexing
lighter repository and easier dependency upgrade
Examples:
Robolectric jar should be used for unit testing only and shouldn't be part of apk itself
Repository is clean from different folders with jars, checkout takes much less. No needs to download and replace old jars with new jars
I should notice:
Not many libraries are in maven central and you should make some effort to use them such way in your project
You could much easier get to "class already added" error during dexing with central repository approach
You can mix usage of dependencies from central repository and from lib folder, but I prefer to use only one way for simplicity
Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
Right click it and hit 'Add as library'
Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file
Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean. I'm on Mac OS X, the command might be different on your system
This series of steps was taken from Android Studio: Add jar as library? and is not my original answer. I am posting them here, again, because your question was the third in search results on Google when looking up this same topic. Hence, copying.
All credits to the one who wrote the steps.
Download & Copy Your .jar file in libs folder then adding one line to build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
compile 'com.google.code.gson:gson:2.3.4' ----------> I added this one
}
Do not forget to click "Sync now"
I´m using Android Studio 1.1.0
Download and copy your jar to libs folder then add the following to your app.gradle file and SYNC.
dependencies {
compile 'com.google.code.gson:gson:{version_you_need}'
}
repositories{
flatDir{
dirs 'libs'
}
}

Categories

Resources