I saw two method of adding external jar.
First is copying to libs folder and then adding manually in build.gradle.
compile files('libs/commons-net-3.1.jar')
Other is the graphical method: right click on app folder> import external .jar > then adding dependency........
I choose the first one but this gives me and error:
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java - Dfile.encoding=UTF-8 -jar /Users/abhimanyuaryan/AndroidStudioProjects/UniSnap/app/libs/commons-net-3.1.jar
no main manifest attribute, in /Users/abhimanyuaryan/AndroidStudioProjects/UniSnap/app/libs/commons-net-3.1.jar
Process finished with exit code 1
Your problem is better solved by not including a local jar at all, but by a remote artifact.
Make sure your build.gradle has to following to load from maven central -
repositories {
mavenCentral()
}
and add the following to your dependencies block --
compile 'commons-net:commons-net:3.1'
Do a gradle sync after making these changes.
The format is 'group:name:version'. See the docs on dependencies for more info. To find libraries you are interested in, check their install docs for a maven or gradle artifact description, or you can search Maven Central.
Lastly, want to point out that 3.3, not 3.1, is not the most current version of Apache Commons Net.
Related
I have upgraded Android Studio from 4.1.3 to 4.2, using latest gradle and gradle plugin. Now references to using jcenter() in the build script are deprecated due to jcenter being end-of-lifed:
The suggestion is to "migrate" to mavenCentral(). I have various dependencies that are seemingly not on mavenCentral(), because gradle cannot find them, for example:
I Googled the artifact ("materialsearchview" in this case) and found it on the search platform "MVNrepository":
So here is what I have tried (all unsuccessful) to put a reference into my build script to have gradle find this artifact:
I added a reference to the mvnrepository to my project level build.gradle file (which I didn't expect to work given that mvnrepository is a search mechanism) highlighted in the red box in the pic, i.e.:
maven { url 'https://mvnrepository.com/artifact/' }
I added a reference to the repository identified in the blue box at the bottom where mvnrepository says the artifact is located, i.e.
maven {url 'https://repo.spring.io/plugins-release/'}
This generated a slightly different error:
Could not HEAD 'https://repo.spring.io/plugins-release/com/miguelcatalan/materialsearchview/1.4.0/materialsearchview-1.4.0.pom'. Received status code 401 from server: Unauthorized
I found the .aar file for this dependency, added it to my 'libs' directory and updated my app module level build.gradle file like this:
implementation fileTree(include: ['.jar','.aar'], dir: 'libs')
I did an 'invalidate caches and restart' at this point, thinking AS needed to index the newly added .aar file before gradle would recognize it. No joy.
I specifically added the .aar file to the libs directory, then added a reference to it in the app module build.gradle:
implementation(name:'materialsearchview-1.4.0', ext:'aar')
then did another invalidate cache/restart. No joy.
So I guess I have three questions:
Once I find a reference to an artiface in mvnrepository, is there a proper way to reference it in my gradle script so that the build system can reconcile what it needs?
What other ways are there to find what other repositories that gradle CAN address to see if the item is there?
Why is using the .aar file in the libs directory as I am doing it failing? Why can't Gradle see it there?
Thanks!
After discussing this with Mark Murphy of CommonsWare, I realized I was under a misunderstanding about the relationship between the repositories section of the project level build.gradle file, and the implementation statements of the module level build.gradle file.
Here is a good way to think of it thanks to Mark:
Project level build.gradle, i.e.:
repositories {
google()
mavenCentral()
maven { url 'https://maven.preemptive.com/' }
maven { url 'https://jitpack.io' }
//jcenter()
}
Think of these as "Gradle, look into these repositories to reconcile all the 'implementation' statements."
Then in the module level build.gradle I had this:
implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')
Think of this as "Oh yeah, grab all these things too, even if I didn't mention them as specific dependencies".
My mistake #1 was in thinking I could have an .aar file in the libs directory which ALSO had a corresponding 'implementation' statement. You can't.
My mistake #2 was that I had an implementation statement that referenced a .aar file that was in maven (which is fine since maven is in my repositories block) which itself referenced an .aar file that was not in maven but only in jcenter (which is not since I just removed jcenter from the repositories block). When I removed the reference to jcenter in the repositories block this dependency ("transitive dependency") was not reconciled and the build failed.
So lessons learned:
Any resource referenced in an 'implementation' statement AND ANY RESOURCE THAT IS IN ITS POM THAT IT DEPENDS ON must ALL be in repositories that you define in the repositories block.
If ANY of the dependencies were in jcenter, since I got rid of jcenter then I must:
Find another repository instead of jcenter that has the same resources needed to satisfy the ALL dependencies associated with the resource in question OR
Gather all the .jar/.aar files that represent that resource (the one you want and all the ones it references), place them in the /libs directory (under the module you are building), make sure your have your fileTree statement correctly formed, and REMOVE references to all those same resources from your 'implementations' statements.
How does one identify all the dependencies you ask? Well you can do it the slow way (like I did at first) and keep finding/adding them until the build doesn't break anymore. Or you can be more clever about it and let Gradle tell you which it will do if you ask it (see the docs here).
Finally - don't forget that it is not enough to make sure the referenced .aar file is in libs. In other words, Gradle does not work like: "I will look first at the repositories to satisfy your implementation dependencies and any dependency they reference, and if they are not there then I will look through all the .jar and .aar files in the spot you told me to look in the fileTree statement". Instead, note that any dependency that is referenced by an item in an 'implementation' statement needs to ALSO be able to be found within the repositories. If it can't then your only choice is to use that dependency graph, determine ALL the dependencies, and put all the .aar files into /libs AND remove any from your 'implementation' statements.
Much easier if you can find another repository instead of messing with all the individual .aar files!
For those who want to try the easier way, just go here Android Studio Archive and download the Android Studio v4.1 (Oct 12, 2020) and install it. It is working fine, I have checked it myself.
I have to create an application that makes extensive use of charts.
Reading the web I chose achartengine that seems to have everything I need.
I downloaded the jar file, I plugged in the libs folder, I selected "add to library" and I lunch the gradlew clean.
Result in the sources where I do the import of org.achartengine.xxxx I always returned the error that fails to resolve symbols .
Do you have suggestions?
Thank you
Andrea
I am able to use this library in my Android Studio project, this topic explains how to add AChartEngine repo to your project.
What I did:
Added following to project-wide build.gradle (one from the project root):
allprojects {
repositories {
...
maven {
url "https://repository-achartengine.forge.cloudbees.com/snapshot/"
}
}
}
For every module that uses the library, add this to its build.gradle (you may put this to the top-level build.gradle if it should be included in all modules):
dependencies {
...
compile group: 'org.achartengine', name: 'achartengine', version: '1.2.0'
}
Now I can use the library in the project, I see the classes in code assist popups and build runs as succeeds.
It seems like the new version (1.2.0) is not available for download anymore in the http://www.achartengine.org/ site. and that's why the gradle/maven method doesn't work (or the snapshot file was removed).
I succeeded using and adding it to my project by downloading the jar file from here:
https://github.com/ddanny/achartengine/files/460139/achartengine-1.2.0.zip
I have an Android Instant App with following structure:
B: base module
Installed: installed app module
Instant: instant app module
F: feature with functional specific to Installed app. F depends on local aar library local-lib located in project\F\libs.
F's build.gradle is following:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
api ":local-lib#aar"
}
I tried to include F module to Installed app module like this:
dependencies {
implementation project(':B')
implementation project(':F')
}
But gradle couldn't resolve local-lib, giving error:
Error:Could not resolve all dependencies for configuration ':Installed:releaseCompileClasspath'.
> Could not find :local-lib:.
Searched in the following locations:
... some remote repositories ...
Required by:
project :Installed > project :F
I tried to duplicate libs folder to project\Installed\libs, and it worked. So basically I need 2 copies of local-lib to make this work? How should I organise imports to avoid duplication? Perfect case would be if libs folder was inside F module.
IMHO, the cleanest way to make it work would be to use some local repository, like a Maven, publish your local-lib here, and reference it from here, and do the same for each of your libraries. When you publish to an artifact repository manager - let's say a Maven - you will have your .aar coupled with a pom file, containing all the needed dependencies.
You have to keep in mind here that your aar is kind of a flat file, meaning, while you reference it somewhere, there is not way to keep track of the transitive dependencies of it (that's the job of the pom files on Maven).
This means that when you reference F in Installed, the F aar is added, but Installed doesn't know that it has to get local-lib in order for F to work properly, or doesn't know where. That's why you have lines on the remote repositories: gradle searches everywhere (in every possible place = in every repository you have listed) for the dependency.
When you copy/paste the code as a module of your project, the gradle knows what are the transitive dependencies because it can access the gradle file for each dependency.
When you copy the aar directly inthe Installed/libs folder, it also works because gradle checks here (you probably have a compile line in your gradle checking for that folder).
If you want to keep the flat file, you should try putting somewhere reachable by all modules, on the same folder level (take a look at that question), or you could try to add the local-lib as an Android module project, and not just put it in the libs folder.
I have to create an application that makes extensive use of charts.
Reading the web I chose achartengine that seems to have everything I need.
I downloaded the jar file, I plugged in the libs folder, I selected "add to library" and I lunch the gradlew clean.
Result in the sources where I do the import of org.achartengine.xxxx I always returned the error that fails to resolve symbols .
Do you have suggestions?
Thank you
Andrea
I am able to use this library in my Android Studio project, this topic explains how to add AChartEngine repo to your project.
What I did:
Added following to project-wide build.gradle (one from the project root):
allprojects {
repositories {
...
maven {
url "https://repository-achartengine.forge.cloudbees.com/snapshot/"
}
}
}
For every module that uses the library, add this to its build.gradle (you may put this to the top-level build.gradle if it should be included in all modules):
dependencies {
...
compile group: 'org.achartengine', name: 'achartengine', version: '1.2.0'
}
Now I can use the library in the project, I see the classes in code assist popups and build runs as succeeds.
It seems like the new version (1.2.0) is not available for download anymore in the http://www.achartengine.org/ site. and that's why the gradle/maven method doesn't work (or the snapshot file was removed).
I succeeded using and adding it to my project by downloading the jar file from here:
https://github.com/ddanny/achartengine/files/460139/achartengine-1.2.0.zip
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'
}
}