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

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.

Related

Debugging tomcat filter chain in intellij [duplicate]

When creating a new Java project in IntelliJ IDEA, the following directories and files are created:
./projectname.iml
./projectname.ipr
./projectname.iws
./src/
I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jar to the project. What is the correct way to achieve this in IntelliJ IDEA?
Steps for adding external jars in IntelliJ IDEA:
Click File from the toolbar
Select Project Structure option (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
Select Modules at the left panel
Select Dependencies tab
Select + icon
Select 1 JARs or directories option
IntelliJ IDEA 15 & 2016
File > Project Structure...
or press Ctrl + Alt + Shift + S
Project Settings > Modules > Dependencies > "+" sign > JARs or directories...
Select the jar file and click on OK, then click on another OK button to confirm
You can view the jar file in the "External Libraries" folder
Just copy-paste the .jar under the "libs" folder (or whole "libs" folder), right click on it and select 'Add as library' option from the list. It will do the rest...
If you are building your project with gradle, you just need to add one line to the dependencies in the build.gradle:
buildscript {
...
}
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
and then add the folder to your root project or module:
Then you drop your jars in there and you are good to go :-)
You add them as libraries to your module.
I usually have a /lib directory in my source. I put all the JARs I need there, add /lib as a library, and make it part of my module dependencies.
2018 update: I'm using IntelliJ 2017/2018 now.
I'm fully committed to Maven and Nexus for dependency management.
This is the way the world has gone. Every open source Java project that I know of uses Maven or Gradle. You should, too.
I use this method and it works well:
1- Copy And paste the .jar files under the libs folder.
2- Add compile fileTree(dir: 'libs', include: '*.jar') to dependencies in build.gradle then all the jars in the libs folder will be included..
3- Right click on libs folder and select 'Add as library' option from the list.
Libraries cannot be directly used in any program if not properly added to the project gradle files.
This can easily be done in smart IDEs like inteli J.
1) First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)
2) then copy or add your library file (eg: .jar file) to the folder named 'libs'
3) now you can see the library file inside the libs folder. Now right click on the file and select 'add as library'. And this will fix all the relevant files in your program and library will be directly available for your use.
Please note:
Whenever you are adding libraries to a project, make sure that the project supports the library
Some great help found here. However, I still could not make it to work despite loading JAR properly. I found out later that I accidentally created module in the file structure instead of regular folder and this very module was pre-selected in the project setting.
Here is the footprint:
File -> Project Structure -> Modules -> (select proper module if you have more) -> Dependencies -> + -> JAR or Libraries
While I agree with the previous answers, it's important to note how to access the code of those external libraries.
For example to access a class in the external library, you will want to use the import keyword followed by the external library's name, continued with dot notation until the desired class is reached.
Look at the image below to see how I import CodeGenerationException class from the quickfixj library.
File > Project Structure
Project Settings > Modules > Dependencies (Select one of)
1 JARs or Directories...
2 Library...
3 Module Dependency...
Apply + Ok
Import into java class
You can put the JAR in the libs folder and add it from there. This can be done in 2 different ways in IntelliJ:
Right-click on the libs folder and add from there:
Add the JAR from the project structure:
If you are building your project with maven, you just need to add one line to the dependencies in the pom.xml:
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxxx-server</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/libs/xxx-server-1.0.0.jar</systemPath>
</dependency>
and then add the folder to your root project or module:
this is my personal experiences. I wish they would help you

How to import a jar package into my IntelliJ [duplicate]

When creating a new Java project in IntelliJ IDEA, the following directories and files are created:
./projectname.iml
./projectname.ipr
./projectname.iws
./src/
I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jar to the project. What is the correct way to achieve this in IntelliJ IDEA?
Steps for adding external jars in IntelliJ IDEA:
Click File from the toolbar
Select Project Structure option (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
Select Modules at the left panel
Select Dependencies tab
Select + icon
Select 1 JARs or directories option
IntelliJ IDEA 15 & 2016
File > Project Structure...
or press Ctrl + Alt + Shift + S
Project Settings > Modules > Dependencies > "+" sign > JARs or directories...
Select the jar file and click on OK, then click on another OK button to confirm
You can view the jar file in the "External Libraries" folder
Just copy-paste the .jar under the "libs" folder (or whole "libs" folder), right click on it and select 'Add as library' option from the list. It will do the rest...
If you are building your project with gradle, you just need to add one line to the dependencies in the build.gradle:
buildscript {
...
}
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
and then add the folder to your root project or module:
Then you drop your jars in there and you are good to go :-)
You add them as libraries to your module.
I usually have a /lib directory in my source. I put all the JARs I need there, add /lib as a library, and make it part of my module dependencies.
2018 update: I'm using IntelliJ 2017/2018 now.
I'm fully committed to Maven and Nexus for dependency management.
This is the way the world has gone. Every open source Java project that I know of uses Maven or Gradle. You should, too.
I use this method and it works well:
1- Copy And paste the .jar files under the libs folder.
2- Add compile fileTree(dir: 'libs', include: '*.jar') to dependencies in build.gradle then all the jars in the libs folder will be included..
3- Right click on libs folder and select 'Add as library' option from the list.
Libraries cannot be directly used in any program if not properly added to the project gradle files.
This can easily be done in smart IDEs like inteli J.
1) First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)
2) then copy or add your library file (eg: .jar file) to the folder named 'libs'
3) now you can see the library file inside the libs folder. Now right click on the file and select 'add as library'. And this will fix all the relevant files in your program and library will be directly available for your use.
Please note:
Whenever you are adding libraries to a project, make sure that the project supports the library
Some great help found here. However, I still could not make it to work despite loading JAR properly. I found out later that I accidentally created module in the file structure instead of regular folder and this very module was pre-selected in the project setting.
Here is the footprint:
File -> Project Structure -> Modules -> (select proper module if you have more) -> Dependencies -> + -> JAR or Libraries
While I agree with the previous answers, it's important to note how to access the code of those external libraries.
For example to access a class in the external library, you will want to use the import keyword followed by the external library's name, continued with dot notation until the desired class is reached.
Look at the image below to see how I import CodeGenerationException class from the quickfixj library.
File > Project Structure
Project Settings > Modules > Dependencies (Select one of)
1 JARs or Directories...
2 Library...
3 Module Dependency...
Apply + Ok
Import into java class
You can put the JAR in the libs folder and add it from there. This can be done in 2 different ways in IntelliJ:
Right-click on the libs folder and add from there:
Add the JAR from the project structure:
If you are building your project with maven, you just need to add one line to the dependencies in the pom.xml:
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxxx-server</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/libs/xxx-server-1.0.0.jar</systemPath>
</dependency>
and then add the folder to your root project or module:
this is my personal experiences. I wish they would help you

Importing non-Android Java Library into Android Studio Project

I would like to import a non-Android Java library to an Android project.
Based on my research, there seem to be two main options:
Add the source code of the Java library as a separate module. However, the source code cannot simply be imported (e.g. added as a subrepo) since the Android package names would need to be added to each file.
Add a JAR file of the Java library. I have been following the instructions available here, and the project builds successfully, though I am still unable to import classes from the library. I suspect this may be because the project is a standalone Java library rather than an Android library.
Any advice on how to add the library (either as a JAR or as a copy of the existing library) would be greatly appreciated. Thanks.
Add the jar file for the library under app/libs and make sure your app/build.gradle file has the following:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
After you've included the jar file, click on the "Sync Project with Gradle Files" button. After the sync is complete you should be able to use the classes in the library.

Error in adding external Jar libraries in android studio

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.

In android studio (1.1.0) how do I add a dependency on a java file from another source?

I am trying to incorporate a C library. I have the NDK portion worked out. However, the library includes a java interface module as a package which the top level application file imports. The build fails to find that package import. I think that I need to add a dependency but can't find how to do so. I don't want to simply copy the package into the project because I hope to have several projects using this library and that simply isn't a good coding practice. It seems that I should be able to add a classpath somewhere but gradle/Android Studio seem to reject everything I have tried. Does anyone have any suggestions? Thank you very much. Chuck
If you are trying to import a .jar library, look at the build.gradle (app level) file. From there you will see a "dependencies" field, which should look like the following.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// other dependencies should be added here
}
Just add the following line to the dependencies field :
compile files('PATH_TO_YOUR_JAR_FILE/YOUR_JAR_FILE.jar')
The path to your .jar file can be either absolute or relative (relative root being the "app" module).
Finally, you will have to sync your project with Gradle files in order for the changes to be taken in account.
If it doesn't work, could you be more specific about the way you compile your C library, is it by using Gradle or are you using an Android.mk file ?
OK, I gave up. At this point I don't know if there is a way to include a .java file from somewhere other than the app directory. So I copied the directory structure (org/x/y/something.java) under my app/src/main/java directory and it completely built. Then I had to copy the appropriate lib files (*.so) to the appropriate directory (under app/src/main/jniLibs/armeabi - built by ndk-build APP_PLATFORM=9) for the build and it works. I don't know what I would do if I ever have to change that Java package and it has been included in multiple projects! If this is the way that it is, this seems like a huge limitation in Java.
One solution would be to automate the copy of the external files before each compilation so you are sure to have the latest version whenever you compile your project.
To do so, you can easily create a Gradle task that runs an OS command like "cp" for example (take a look at this page for more details).
Once you've configured you copying task, you have to add a dependency between it and the compiling task by adding the following to your build.gradle (app level) file :
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn NAME_OF_YOUR_COPYING_TASK
}

Categories