Avoid packing .jar into Android Library (.aar) - android

My project is an Android Library which depends on Dropbox's android library.
dependencies {
...
provided fileTree(dir: '../Libraries/Dropbox', include: ['*.jar'])
...
}
Everything works well excepts Gradle puts all the .jar files from Dropbox into my output .aar file.
MyLib.aar
|-classes.jar
|-AndroidManifest.xml
|-...
|-libs
|-bcprov-jdk16-146.jar
|-commons-logging-1.1.1.jar
|-dropbox-android-sdk-1.6.1
|-json_simple-1.1.jar
How can I avoid this?

something like this might help you:
android.libraryVariants.all { variant ->
variant.outputs.each { output ->
def packageLib = output.getPackageLibrary()
packageLib.exclude('libs/externalLibrary.jar')
}
}
inside android {} block

Why do you want to avoid this? When you give your library to someone, they have all the dependencies already in one file.
You can include the dependencies via
compile 'com.dropbox.core:dropbox-core-sdk:1.7.7'
compile 'com.googlecode.json-simple:json-simple:1.1.1'
compile 'commons-logging:commons-logging:1.2'
compile 'org.bouncycastle:bcprov-jdk16:1.46'
in your build.gradle file and remove it from the libs folder. Do the same with the other dependencies. This way they will not be packaged into your .aar file.

Related

Error building Android library: Direct local .aar file dependencies are not supported

We recently upgraded to Android Gradle Plugin 4.0.0-beta03. We are now seeing this error when building one of our library modules
$ ./gradlew library_module:assemble
Execution failed for task ':library_module:bundleDebugAar'.
> Direct local .aar file dependencies are not supported when building an AAR.
The resulting AAR would be broken because the classes and Android resources from any local .aar
file dependencies would not be packaged in the resulting AAR. Previous versions of the Android
Gradle Plugin produce broken AARs in this case too (despite not throwing this error). The
following direct local .aar file dependencies of the :library_module project caused this error:
______.aar
I can see this was added to AGP a few months ago. But they provide no further info on why.
So.
What was the problem? Any more info? I can't find a single bug report anywhere.
How exactly can I fix this? Is this saying that I can't build one .aar that depends on other local .aars? What if this local aar was instead hosted on Maven Central or another remote repo? Why would that make a difference?
I recently encountered the same issue, the fix was to remove the library from libs/ and import it using File -> New -> New Module -> Import .JAR/.AAR Package, then referencing it in the library module build.gradle file:
dependencies {
implementation project(":imported_aar_module")
}
If you are on a newer Android Studio version (4.0.0+), this option is not available. Instead you have to do it manually.
Create a new directory and put the following content into the build.gradle file withing the new directory:
configurations.maybeCreate("default")
artifacts.add("default", file('[nameOfTheAar].aar'))
Place the aar into this new directoy. Next to the build.gradle file.
Add the new created Gradle project to the settings.gradle file:
include(":pathToTheCreatedDirectory")
Include the project in your library where you want to use the aar:
implementation project(":pathToTheCreatedDirectory", configuration = "default")
I want to call out #StefMa's comment on this question which was incredible simple and solved this issue for me, but it's buried among many other comments on this thread and is easily missed.
The 'correct' answer on this thread no longer works because it's not possible to import AARs in Android Studio anymore as referred to in that answer. But, the solution referred to in StefMa's comment linking to this GitHub post does, and it works perfectly.
Long story short - put your AAR into a separate module.
There's no need to muck around with creating lib directories, just follow these directions -
Create a new directory in your project's root directory. The image below shows two of them - spotify-app-remote and spotify-auth, but one is sufficient. Within that, put your AAR in, and create a new build.gradle file.
Within the build.gradle file, add the following, replacing the aar filename with the name of your AAR file -
configurations.maybeCreate("default")
artifacts.add("default", file('spotify-app-remote-release-0.7.1.aar'))
Add this to your settings.gradle file, substituting the name of the directory you created
include ':spotify-app-remote'
Include your new module in the module you wish to use the AAR. eg, if you want to use it within your app module, open app's build.gradle and add
api project(':spotify-app-remote')
within your dependencies { } block, obviously again substituting spotify-app-remote with whatever the name of your module is.
When building an Android library that depends on other Android libraries (i.e., aar files), you will get the following error message if you include the aar files as dependencies in the project:
Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR. Previous versions of the Android Gradle Plugin produce broken AARs in this case too (despite not throwing this error).
As the above message states, when you build an Android library project, any aar it depends on is not packaged. If you built this way prior to AGP (Android Gradle Plugin) 4, you probably noticed that you had to include the aar dependencies on the project consuming your library.
You can compile your Android library project by specifying that the aar dependencies are compileOnly. See this for more info on when to use compileOnly.
So just add the following to your app build.gradle file:
compileOnly files('libs/some-library.aar')
Note that if you do this you will have to include the aar dependencies on the application project that consumes your library.
Alternatively, you can create a module that imports your aar dependency as #Sandi mentioned in the answer above.
Another way is to publish your aar dependencies to a maven repository and then add them to your library project like this:
implementation 'mylibrarygroup:mylibraryartifact:version-x.y.z#aar'
In my experience, when Gradle Plugin version is 4.2.2+ and Gradle version is 7.1+, as in #Luis's answer 'compileOnly' works.
compileOnly files('libs/your_library_name.aar')
It didn't work when the Gradle versions were lower.
Getting same error when use this code.
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation fileTree(include: ['*.aar'], dir: 'libs')
Replace your code with following.
Open the top level ‘build.gradle’ file and add.
repositories {
flatDir {
dirs('/src/main/libs')
}
}
Then in your project’s build.gradle add the following.
api(name:'aar_module_name', ext:'aar')
There are some changes now, You need to add your AAR or JAR as a dependency
1.) First, Navigate to File > Project Structure
[Reference Image 1]
2.) Then go to Dependencies > Declared Dependencies tab, click and select JAR/AAR Dependency in the dropdown
[Reference Image 2]
3.)In the Add Jar/Aar Dependency dialog, first enter the path to your .aar or .jar file, then select the configuration to which the dependency applies. If the library should be available to all configurations, select the "implementation" configuration.
[Reference Image 3]
4.) Click OK then Apply > OK.
You are good to go.
I had the same issue, in the sense I wanted to encapsulate a library dependency into a module. However this library dependency had a bunch of aars and creating separate module each of them is just clutter, and can't even find that option in the new studio.
To resolve it I published the aar-s into my local maven, before starting the build process.
So my encapsulating module's build.gradle looked like this:
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'maven-publish'
}
//..
parent.allprojects { // for some reason simply repositories didn't work
repositories {
mavenLocal()
}
}
//...
publishing {
publications {
barOne(MavenPublication) {
groupId 'foo-aar-dependency'
artifactId 'bar1'
version '1.0'
artifact("$libsDirName/bar1.aar")
}
barTwo(MavenPublication) {
groupId 'foo-aar-dependency'
artifactId 'bar2'
version '1.0'
artifact("$libsDirName/bar2.aar")
}
barThree(MavenPublication) {
groupId 'foo-aar-dependency'
artifactId 'bar3'
version '1.0'
artifact("$libsDirName/bar3.aar")
}
// and so on...
}
}
// add the publication before the build even starts
// used ./gradlew mymodule:assemble --dry-run to find where to put it
afterEvaluate {
tasks.clean.dependsOn("publishToMavenLocal")
tasks.preBuild.dependsOn("publishToMavenLocal")
}
dependencies {
implementation "foo-aar-dependency:bar1:1.0"
implementation "foo-aar-dependency:bar2:1.0"
implementation "foo-aar-dependency:bar3:1.0"
// and so on
// also I had to make sure to add the aar's transitive dependencies as implementation below
}
Note: When I sync for the first time the dependencies are not found, but as soon as any clean/assemble is called the dependencies are published prior so it runs as it needs.
Note2: most of this can be moved into a separate file to not clutter your build.gradle
Note3: If you actually want to publish your module as a library this solution is not for you.
Note4: This also works on CI if you run clean then your next task.
For those who prefer to use as a regular dependency (or an item on your Gradle's version catalog):
Create a folder eg. spotifyAppRemote at the same level of app folder
Add the desired .aar file at the root of spotifyAppRemote folder
Create a settings.gradle.kts file at the root of spotifyAppRemote folder. This file will be empty, it just needs to be there for the composite builds. See: docs
Create a build.gradle.kts file at the root of spotifyAppRemote folder:
plugins {
base //allows IDE clean to trigger clean on this module too
}
configurations.maybeCreate("default")
artifacts.add("default", file("spotify-app-remote-release-0.7.2.aar"))
//Change group to whatever you want. Here I'm using the package from the aar that I'm importing from
group = "com.spotify.android"
version = "0.7.2"
Next add Gradle files to this folder to allow this module to build itself. You can do it manually or add the following snippet at the root of settings.gradle.kts (!! the project root, not the empty one created above)
/* Optional - automatically sync gradle files for included build */
rootDir.run {
listOf(
"gradle.properties",
"gradlew.bat",
"gradlew",
"gradle/wrapper/gradle-wrapper.jar",
"gradle/wrapper/gradle-wrapper.properties"
).map { path ->
resolve(path)
.copyTo(
target = rootDir.resolve("spotifyAppRemote").resolve(path),
overwrite = true
)
}
}
Now you can go ahead and add this folder as a module at the settings.gradle.kts on your project root. The same where may add the snippet above:
rootProject.name = "Your project name"
include(":app")
includeBuild("spotifyAppRemote")
Sync and build your project.
Now your included build will be available for your as a regular dependency with the defined group and version. To use this dependency:
dependencies {
// group:moduleName:version
implementation("com.spotify.android:spotifyAppRemote:0.7.2")
}
Thanks other members for the solution.
Source code on github: https://github.com/rsicarelli/SpotifySdkCompositeBuild
If you want to bundle a local .aar within your library and use that library in another project, you could take a look at "fat aars" https://github.com/kezong/fat-aar-android
EDIT : if the AAR does not contain android resources or native code, this could help you.
If you want this local resource directly linked to an "app" or "sdk" module
(no compileOnly)
=> Use a jar.
Rename the .aar to .zip
Extract it
Use the classes.jar inside
That's it.
Patch the problematic 3rd party dependency's build.gradle file. Under their dependencies { } section, they had a line like this:
implementation fileTree(dir: 'libs', include: ['*.jar','*.aar']) //Load all aars and jars from libs folder
My patch changes that line to:
implementation(name: 'the-name-of-the-aar', ext: 'aar')
In my project's build.gradle, under allprojects { repositories { }, added:
flatDir { dirs "$rootDir/../node_modules/the-third-party-dependency/android/src/main/libs" }
Where the AAR file lives
It was tested with reactnative >= 0.69.x
I faced a similar problem:
Task: add .aar SDK inside another SDK
Solution:
We have to create new Android Library Module inside our library (right click on our library name -> module -> Android library )
Delete all files inside it
Insert our .arr inside this module
Create build.gradle file inside module and put there:
configurations.maybeCreate("default")
artifacts.add("default", file('your_arr_name.aar'))
Add to your library build.gradle inside dependencies block next:
implementation project(':your_library:your_arr_module')
Now rebuild project and everything should work fine
It is bug in Android Studio 4.0.+.However, there is a solution.
First, project/build.gradle:
allprojects {
repositories {
google()
jcenter()
mavenCentral()
flatDir {dirs "../MoudleA/aars,../MoudleB/aars,../MoudleC/libs".split(",")
}
}
}
Second, Moudle/build.gradle:
// MoudleA/build.gradle
repositories {
flatDir {
dirs 'aars'
}
}
dependencies {
api fileTree(dir: 'libs', include: ['*.jar'])
//api fileTree(dir: 'aars', include: ['*.aar'])
// aar
new File('MoudleA/aars').traverse(
nameFilter: ~/.*\.aar/
) { file ->
def name = file.getName().replace('.aar', '')
api(name: name, ext: 'aar')
}
}
// MoudleB/build.gradle
repositories {
flatDir {
dirs 'aars'
}
}
dependencies {
api fileTree(dir: 'libs', include: ['*.jar'])
//fullApi fileTree(dir: 'aars/full', include: ['*.aar'])
//liteApi fileTree(dir: 'aars/lite', include: ['*.aar'])
// aar
new File('MoudleB/aars/full').traverse(
nameFilter: ~/.*\.aar/
) { file ->
def name = file.getName().replace('.aar', '')
fullApi(name: 'full/' + name, ext: 'aar')
}
new File('MoudleB/aars/lite').traverse(
nameFilter: ~/.*\.aar/
) { file ->
def name = file.getName().replace('.aar', '')
liteApi(name: 'lite/' + name, ext: 'aar')
}
}
// MoudleC/build.gradle
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
//api fileTree(dir: 'libs', include: ['*.jar','*.aar'])
api fileTree(dir: 'libs', include: ['*.jar'])
// aar
new File('MoudleC/libs').traverse(
nameFilter: ~/.*\.aar/
) { file ->
def name = file.getName().replace('.aar', '')
api(name: name, ext: 'aar')
}
}
It works for me,You can also try.
You can upload the AARs to an Artifactory, and consume them.
In my case, I realised that I have created libs folder at wrong place then recreated folder in main folder and implementation fileTree(include: ['*.aar'], dir: 'libs') worked.
Adapt aar dependency to maven repo standards and depend on it.
Lets connect the dependency in build.gradle
repositories {
maven { url "$project.projectDir/libs" }
}
dependencies {
api "my-library-group:my-library-module:my-library-version"
}
Replace you libs/myLibrary.arr file with next files:
libs/my-library-group/my-library-module/my-library-version/my-library-module-my-library-version.aar
libs/my-library-group/my-library-module/my-library-version/my-library-module-my-library-version.pom
libs/my-library-group/my-library-module/maven-metadata-local.xml
Where my-library-module-my-library-version.aar is the original aar file
Content of my-library-module-my-library-version.pom
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>my-library-group</groupId>
<artifactId>my-library-module</artifactId>
<version>my-library-version</version>
<packaging>aar</packaging>
</project>
Content of maven-metadata-local.xml
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>my-library-group</groupId>
<artifactId>my-library-module</artifactId>
<versioning>
<latest>my-library-version</latest>
<release>my-library-version</release>
<versions>
<version>my-library-version</version>
</versions>
<lastUpdated>20211130111015</lastUpdated>
</versioning>
</metadata>
Feel free to replace my-library-group, my-library-module, my-library-version with any value you like
Good news for everyone. It seems that we can finally include AARs without subprojects again. I was able to accomplish it using the implementation files directive as follows in the dependencies { } block:
implementation files('ssi.aar')
I also hit this issue when I increase my Android plugin version to 4.0.1, and it turns to error, tried some solutions but none of them are actually doable in our project.
Since we are using product flavours, and different flavours are using different local aar file, we simply can not just using api(name: "xxx", ext: 'aar') since those aar files are located in different flatDir.
For now I have to roll back to previous gradle plugin version.
will edit this answer if I figure something out
Much lazier way to do this in build.gradle.kts files is to use a fileTree combined with flatDir repository.
repositories {
flatDir {
dir("$rootDir/libraries")
}
}
dependencies {
fileTree("$rootDir/libraries").forEach { file ->
implementation(group = "", name = file.name.removeSuffix(".aar"), ext = "aar")
}
}
This way when you add or remove deps to the folder they are automatically configured
for me works this solution:
put into dependences in build.gradle:app file this string:
api fileTree(dir: 'libs', include: ['*.aar'])

How to fix 'java.lang.NoClassDefFoundError' in Android .aar project

I have an android .aar library built and I am trying to integrate it with one of the projects. When the app tries to open the initial screen of the .aar library where I have API call using retrofit. I am getting the below exception
java.lang.NoClassDefFoundError: Failed resolution
of:Lokhttp3/OkHttpClient$Builder;
I have not obfuscated or enabled pro-guard in my .aar project.
Below are my .aar Gradle dependencies
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
OK, this is a common issue. There are several ways to use an android library(aar) in other projects. For example:
By importing this aar as a module into your sample project by using
implementation project(':mylibrary').
By uploading your aar to a maven repository(artifactory, maven local, Jitpack, etc)
Pay attention to this:
If you are using number 1 above, so you will also have to
add(retrofit, okhttp3, etc) to your sample project with the same
version, because the aar by default doesn't include child
dependencies. That's why you are getting that exception
"java.lang.NoClassDefFoundError: Failed resolution of:
Lokhttp3/OkHttpClient$Builder'".
If you are using number 2 above, so you will have to make sure that your pom.xml file includes your child dependencies, because the server needs to download and have them available in your sample project.
What do I recommend?
I recommend developers to use MavenLocal(), it replicates a real scenario before publishing your aar to a public repository like Jitpack or whatever you want.
How can I do it?
Inside build.gradle of your library module:
apply plugin: 'maven-publish'
project.afterEvaluate {
publishing {
publications {
library(MavenPublication) {
setGroupId 'YOUR_GROUP_ID'
//You can either define these here or get them from project conf elsewhere
setArtifactId 'YOUR_ARTIFACT_ID'
version android.defaultConfig.versionName
artifact bundleReleaseAar //aar artifact you want to publish
pom.withXml {
def dependenciesNode = asNode().appendNode('dependencies')
configurations.implementation.allDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}
Run assemble and publishToMavenLocal gradle tasks. And you'll see something like this:
In your Sample Project
allprojects {
repositories {
mavenLocal()
...
}
}
implementation '${YOUR_GROUP_ID}:${YOUR_ARTIFACT_ID}:${YOUR_VERSION}'
Let's assume you've built your .aar, and published it to a maven repository (artifactory, nexus, what have you) - e.g., "implementation 'com.mycompany:library:1.0#aar'". Any child dependencies need to be included in the pom.xml delivered by the server to have them available in your application.
Since you've used the "implementation" keyword, those dependencies are considered private to the .aar, and will not be listed in the pom.xml. So they will not be available in your aar, and not automatically imported into the project by gradle.
If you change to the api keyword to "api" instead of implementation, those dependencies become public, and should be listed in the generated pom.xml, and thus should be automatically imported into the project.
This is actually also true also with aar's inside modules, rather than referenced via external systems (e.g. implementation project(':mylibrary') ). If you need the dependencies of mylibrary to run the project, they need to be api.
For reference, you may want to take a look at the Android Studio Dependency Configurations Documentation.
If, however, you're manually including the arr via a files statement (e.g., implementation files('libs/my.aar')), then you don't get automatic dependency management, and you're going to have to add the libraries needed by your aar to the main project manually as well via copy and paste between the build.gradle files.
You can try using fat-aar to solve this https://github.com/kezong/fat-aar-android

Using .aar NoClassDefFoundError But Class Exists and is Dexed

I have several projects which I build to create an .aar. I then import this .aar into into Android Studio under /libs. The build.gradle file for this dependency looks as follows:
repositories{
flatDir{
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.0.0'
compile 'com.android.support:multidex:+'
compile(name: 'customApi-debug', ext:'aar')
}
Since the library is quite large I have set multiDexEnabled = true. Android Studio finds the library and autocomplete works. Building works fine too but running the app gives the following error:
java.lang.NoClassDefFoundError: com.companyx.android.api.ui.vision.metaio.MetaIoView
at com.companyx.android.api.ui.vision.metaio.MetaIoView$$InjectAdapter.<init>(MetaIoView$$InjectAdapter.java:29)
I uncompressed and disassembled the .aar and dex files, respectively, and verified that the classes its complaining about actually exist. I've tried existing approaches for dealing with this problem but none of them worked.
Anyone else experienced this? Thanks in advance.
I run into the same issue. The fix is firstly to deploy the AAR file to a local maven (I utilized the plugin at https://github.com/dcendents/android-maven-gradle-plugin). Then I referenced to the local maven as described at https://stackoverflow.com/a/23045791/2563009. And eventually I declared the dependencies with a transitive option, like this:
dependencies {
compile('com.myapp.awesomelib:awesomelib:0.0.1#aar') {
transitive = true
}
}
The error would be gone then.
Just fyi, you can use a simpler syntax, which is valid too
compile 'com.myapp.awesomelib:awesomelib:0.0.1'
Don't forget to omit the #aar thing at the end of the library name

Android Studio external library

I'm starting to do some projects with this IDE and still I find several anoying situations that where very easy to solve with eclipse but that I don't know how to face here.
I'm implementing mail sending functionality to my app. This must be transparent for the user, so I'm using javax.mail.
For this, I have downloaded the "activation.jar", "additionnal.jar" and "mail.jar" files and I have save them in the libs folder.
Now I'm creating the custom sender class, which extendes javax.mail.Authenticator. But it does not find the import for the javax.mail, mail is appearing in red. I'm triying to import it from the libs but it does not find them.
I have also included this in gradle.build:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
I have readed in several questions like this, that the way is to right click on the library and click on Add as library, but this option is not appearing for me.
So, whats the way of making Android Studio recognize these libraries?
I would encourage you to use an artifactory like Sonatype Nexus is for instance. In here you would deploy and retrive your artifacts and artifacts from other remote artifactories. But for now may just use MavenCentral.
In your top level build.gradle you would need to configure a repository where your artifacts reside.
repositories {
mavenCentral() //a public artifactory
maven {
url "https://your.privaterepo.com"
}
}
In your project level build.gradle you would add a dependency for javax.mail like
dependencies {
compile 'javax.mail:mail:1.5.0-b01' // G:A:V
}
For looking up GAV-coordinates of artifacts (GroupArtifactVersion) you could use just google, which often points you to http://mvnrepository.com/ where you can easily pick up dependencies in gradle notation.
You then will never need to download artifacts manually, gradle will handle this for you. All you would have to do is, to declare the repository and the dependencies.
For deploying artefacts from gradle, I use a goal uploadArchives in my module level build.gradle:
apply plugin: 'maven'
//noinspection GroovyMissingReturnStatement
uploadArchives {
repositories {
mavenDeployer {
repository(url: 'http://our.private.nexus/nexus/content/repositories/releases') {
authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD);
}
snapshotRepository(url: 'http://our.private.nexus/nexus/content/repositories/snapshots') {
authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD);
}
pom.groupId = "the.groupid.for.this.artifact"
pom.artifactId = "artifactid"
pom.version = "1.2.3"
}
}
}
if you have added libraries into lib folder in src. It will not going to work.
Click on android in project showcase and select Project, make new folder as libs and then add all third party libraries into it after that when you will right click on it, you will get option to add it as a library.
You also dont need to write in gradle file after adding it. Android Studio automatically add line itself.
I usually do a trick with import *.jar libs. After put *.jar files in libs folder. I delete some characters in compile fileTree(dir: 'libs', include: ['*.jar']) to compile fileTree(dir: 'libs', include: ['*.j']) and rewrite it to compile fileTree(dir: 'libs', include: ['*.jar']). It's a way to sync my build.gradle :)

Can't add External Library in Android Studio?

I Added a External Library to my project by following
this method. and tried this method too this method too.
Gradle build got Finished and i got this line added to my build.gradle
compile 'com.github.castorflex.smoothprogressbar:library-circular:1.0.1'
Now i am trying to import this library in my class. But i can't do this. I took a look at the files and they are located in the build directory under the exploded-aar. so i added #aar to the compile line. Still there are no changes.
How can i import this library to my class or where i am going wrong?
Thanks in Advance
Well, you don't need to download anything or point your dependency to a file.
This dependency, as most of the dependencies you use, can automatically fetched from JCenter, biggest repository for Java and Android components.
All you need to do is:
Add JCenter as your dependencies repository.
Declare that you need the library-circular dependency. This can be found and copy/pasted from the file icon in particular package/version in JCenter.
Resync your Android Studio project with your Gradle build file. This is the 'sync' button in Gradle pane in Android Studio.
Here's what your build.gradle should include:
repositories {
jcenter()
}
dependencies {
compile(group: 'com.github.castorflex.smoothprogressbar', name: 'library-circular', version: '1.0.1', ext: 'aar')
}
in build. gradle put following code
dependencies {
compile fileTree (dir: 'libs', include: ['*.jar'])
}
Did you add the dependencies?
You can do so in the following way -
dependencies {
compile files('insert ParentFolder/external_library_name with the extension here')
}
Add it to build.gradle based in the app folder
1.Put the jar file into the libs folder.
2.Right click it and hit 'Add as library'.
3.Ensure that compile files('libs/abcdef.jar') is in your build.gradle file and Finally add the dependencies.
Eg.
dependencies {
compile fileTree(dir: '../jar', include: '*.jar')
compile project(':pull-to-refresh')
compile files('libraries/abcdef.jar')
}
Hope this helps :)

Categories

Resources