Error in adding external Jar libraries in android studio - java

This is w.r.t Android Studio 1.3.2
So when i try to add the Repo from the Import Module->Import Existing Jar,the library is added as a separate module and included in the settings.gradle.
After this when I try to use the Jar, I get the message
Add Library 'x' to the classpath
Once i add this to the classpath,I am able to access the classes however the project does not build with the following error,
class Xx cannot be found(Xx belongs to the jar)
Is there a work-around to this.
I can add the jar by creatings a libs folder in the app module and clicking on add as library.
However I do not want the jar inside the main module.

When you add any module dependency then it will not automatically added to gradle file.
You have to add that dependancy to gradle file manually..
Now here you are adding module to Android Studio project then, You have to add
compile project(':module_name')
So that you module will be attached to your app module.
Hope It will help.
Thank you.!

Do something like this:
To import your module:
Step-1; Goto File->new->import module. Select and import your module.
Step-2: In your app's build.gradle add compile project(':yourlibrary')
Step-3: Sync your gradle.
Now to add jar in the above module:
Step-1: Copy your jar file into your module's lib folder.
Step-2: In your module's build.gradle file add a dependency like compile files('libs/your_jar_file.jar')
Step-3 Sync your gradle and you are good to go.

Related

Have dependengy jar automatically add to STS project when added as dependency in build.gradle

I created a Spring Boot application at https://start.spring.io.
The dependencies I added at the time all exist. Now that I'm developing the app in STS I have to add dependencies to build.gradle from time to time. How can I get the relevant jars to be added to the project without manually downloading them and adding them through Configure Build Path...?
The component in Eclipse that supports Gradle is called Buildship. To add an new dependency you should modify your build.gradle file and add the dependency in the dependencies {} block. You can then right-click the project and select Gradle -> Refresh Gradle Project as described here. This will download the dependency (and all of its dependencies) and then update the classpath of the project.

Building a jar file from a gradle dependency

I need to make dex file from my android project. First, i need to compile my classes to jar. But i need dependencies in jar. otherwise, i'll get about 300 errors because of unknown methods. I need Timber in jar. but i can't find it in the internet. I searched a lot, but no luck. This is the link to the source code in github.
https://github.com/JakeWharton/timber
How do i build a jar from this gradle project?
To add gradle library to your project, you need to add implementation "package-group:package-name:package-version" to your build.gradle.
For Timber, you would do implementation "com.jakewharton.timber:timber:4.7.1".
If you want to compile your project, with its dependencies into a jar, then you want to look up "fat jars". Gradle Shadowjar is a plugin that does this.

How to Add JAR (HTMLCleaner) to Project in Android Studio 1.1?

I've spent countless hours trying to add a simple JAR (HTMLCleaner) to my project in Android Stuio 1.1 to no avail. I imported the JAR as a module through the interface (File > New Module > Import JAR...), added it as a dependency on my app module, and even reference it in my AndroidManifest.xml <uses-library etc...>.
When I try to "import org.htmlcleaner;" in any of my classes I get a "cannot resolve symbol error." I've researched and tried every suggestion in every permutation and combination. Can anyone offer additional direction or a step-by-step tutorial on the proper way to do this?
Remember that Android Studio uses Gradle as a build tool, and part of the build flow is to handle dependencies. That being said, you have to specify to Gradle that includes your external jar files, making the following modifications:
Open build.gradle file of your module project (generally called "app")
Inside "dependencies", add compile fileTree(dir: 'libs', include: ['*.jar'])
Create a folder called libs inside your module app.
Copy your jar file inside of it
Run the build process of Gradle clicking in the "Sync now" link of Android Studio, and now your library should be recognized.
You can add jar file by following Rodrigo Ayala answer. Also Instead of using jar file you can also add the dependency in your project by add the following lines to build.gradle
dependencies {
// your other dependencies
compile 'net.sourceforge.htmlcleaner:htmlcleaner:2.16'
}
Inside your "app" module create a new folder "libs".
Paste your jar file inside libs folder.
Right Click on the jar file and select "Add as library".
DONE.

Why does Gradle only include classes from my library with project dependency

I'm building an Android app that has a dependency on a custom library, and Gradle is only willing to include my custom library when I use a project dependency, not when I use a files dependency to include the library's jar file. I'm building both my app and the library with the API levee 19 SDK.
failing dependencies section from build.gradle:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile files('libs/MyLibrary.jar')
}
If I use the above dependencies section, none of the class in MyLibrary.jar are included in the build apk file, as verified by extracting its classes.dex and running dexdump. I have also verified that all of the classes are present in the jar file I'm using.
If I use the following dependencies section, then all of the classes in MyLibrary are included in the apk file:
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile project(':MyLibrary')
}
I'm using Android Studio 0.4.0, Gradle 1.9, and I think the Gradle plugin 0.7.1.
What is going on here? I'd really like to build my app with the API level 18 sdk to test compatibility, but I can't get that working unless I'm able to just use the jar file for my library.
Okay, this was my fault. My library's build.gradle was configured to only include the source files in the jar output file. The following is incorrect Gradle code and will give you the same problems as I've had.
task jar(type: Jar) {
from android.sourceSets.main.java
}
This answer shows how to fix the jar file creation. It's ugly, but it seems to work.
Jar task does not include dependencies in the final jar artifact.
From Gradle documentation on jar task:
The jar task creates a JAR file containing the class files and
resources of the project.
It assumes that since you are building jar for your project, all dependencies will be provided during runtime. As opposed to war, where all dependencies are usually included in the final artifact.
If you need to create "fat jar", which will include the dependencies, then look into specific plugins, for example gradle-fatjar-plugin.
It's a little bit of a longshot, but if you're not using Android Studio 0.4.0 and you've just added the jar file, try cleaning your project and rebuilding from scratch. We've seen this bug: https://code.google.com/p/android/issues/detail?id=63366 where libraries don't get included without cleaning the project, though this bug refers to a dependency downloaded from Maven and not a local jar file (which may or may not be an important difference). This was fixed in Android Studio 0.4.0 (more specifically, in the Gradle plugin 0.7.0).

Gradle 2 android projects using same library

I have the following structure
MainProjectRoot
Android Project 1:
...
Android Project 2:
...
Shared Library:
...
The library project is shared between both of the Android projects. I am trying to convert things to work with the new build system.
This is what export from eclipse generated for the build.gradle for Project1.
compile project(':D::workspace:MainProjectRoot:shared-library')
How do i fix this reference?
i tried this but it expects the library to be inside the Project1 Folder if i leave it out as
compile project(':shared-library')
The export from Eclipse is broken on Windows when dealing with multi-module projects. We are fixing this.
In the meantime ensure you have the settings.gradle file under MainProjectRoot/
It should contain:
include 'project1'
include 'project2'
include 'shared-library'
(or whatever those folder names are).
Then you change the dependency line to be
compile project(':shared-library')

Categories