How to add classpath in an Android Studio project - java

I have an Android project with an Android application that depends on a pure Java library (and the Java library uses some others compiled jars libraries). I added the dependencies, I can build the project, but at run time I have a ClassNotFoundException error.I had to add to theCLASSPATHenvironment variable the path to the jars.Is there a way to set the classpath locally for the project only, like using the command line option
java –classpath <path to the jars>
in the Android studio Run/Debug Configurations?

First off all, be sure there's a "libs" subfolder in the "app" folder of your project. If there's not, create one.
Next, in the app/build.gradle file, add this line of code:
dependencies {
...
compile fileTree(dir:'libs', include: ['*.jar'])
...
}
Place all of your .jar files in the "libs" folder, and voila, you're done

Thanks to Arnold Layne, I created a folder named libs.
Then, I went to
Files -> Project Structure (new menu opens) -> Modules (on the left) -> app -> Dependencies
There, I could add the library with the + Button.
This created this entry:
dependencies {
implementation files('libs/commons-lang3-3.7.jar')
}

Add this lines to build.gradle of app module.
dependencies {
...
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation files('libs/mysql-connector-java-8.0.16')
...
}

Related

Jxl.jar in android

I don't know how to add jxl.jar in android studio
I tried to pasts jxl.jar in lib project and using project structure
then tried to start to create WorkBook obj
but not working.
what I missed?
If you use gradle (Android Studio), then add in dependencies:
dependencies {
implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
}
Paste your jxl.jar into the libs folder
Right click it and select 'Add as library'
Make sure that compile files('libs/jxl.jar') is in your
build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar')
if you are using many jar files) if it still not there then you can try to add it manually by adding this compile files('libs/jxl.jar') inside dependancies
Synch your project

Trying to import a android project into Android studio error

I am new to android studio and tried to import a project which someone else had made into it .The project has google play services jar file in its libs folder.I got the following error while importing:
Why does this error occour and how do i fix it?Will it be shown for other library files also?
This can happen when your external library or the dependencies are not added in the Android Studio Properly .
So what you can do is :
1.Put the jar file into the libs folder.
2.Right click it and hit 'Add as library'.
3.Ensure that compile files('libs/google-play-services_lib.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')
compile files('libraries/google-play-services_lib.jar')
}
Hope this helps :)
With Gooole Play Services - you don't have to even copy library from old project to new one. You can get it is easily by adding in build.gradle in dependecies section:
compile 'com.google.android.gms:play-services:5.0.89'
(or any other version, that will be suitable for you) and lib will be automatically downloaded/added

Java only project not adding dependecies on Android Studio

I am using Android Studio 0.8.2 and in my project I have one Android module and one Java only module.
The java only module depends on a external library. I have tried adding it in the libs dir of the project:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
And using maven repositories:
repositories {
mavenCentral()
}
dependencies {
compile 'net.sf.kxml:kxml2:2.3.0+'
}
Both systems allows compilation but the library is not included on final jar. I have unzipped the jar and the classes are not there!
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
A Gradle sync shows this warning message:
"A top-level `dependencies` block should only appear in build files that correspond to a module"
Any idea? Thanks advanced.
Looks like you are not declaring dependencies in build.gradle in app folder, but in the main roof of project? Am I right? You should compare declaring dependencies with other projects like hmm on github. Example: https://github.com/loopj/android-async-http/blob/master/sample/build.gradle (this is build.gradle file in module project, if you go up to root, in build.gradle file there is not declaring not connected to gradle depenncies). Read carefuly the comments and warnings.

Add library to gradle build

I'm trying to add org.apache.commons.lang3 to my build. I've downloaded the library which is directory containing jar files.
My group is using gradle to build the project, and I know just enough to maybe ask the right question. So what I think the build is doing is
copying a bunch of .bnds to the build directory
compiles the java we have in src/main/java (via source sourceSets.main.java.srcDirs?)
I would like to add the lang3 library, but I'm not sure how to go about doing that. Can I just dump it into src/main/java? Or do I have to tell gradle about it?
This is what I think is relevant from the current build.gradle
ext.releaseDir = "${buildDir}/release/${tpVersion.getProgramName()}"
ext.bundlesDir = "${releaseDir}/nucleus/bin/nucleus_java/bundles/"
dependencies {
compile fileTree(dir: bundlesDir, include: '*.jar')
bnd {
source sourceSets.main.java.srcDirs
include '**/*.bnd'
You could declare it as a dependency, if it exists in any remote repository. That's the way I would do it.
But if you want to use the local file, do not put it in src/main. Use an extra folder called lib or similar on the same directory level as src or you build script.
Then you can add the local dependency to the build.gradle as in this sample:
repositories {
//central maven repo
mavenCentral()
}
dependencies {
//local file
compile files('libs/toxiclibscore.jar')
//dependencies from a remote repository
compile 'java3d:vecmath:1.3.1', 'commons-lang:commons-lang:2.6'
}
The simplest way is to use maven repository for accessing dependencies.
You can also access this jar directly from filesystem with file dependencies.
dependencies {
compile files('libs/a.jar', 'libs/b.jar')
compile fileTree(dir: 'libs', include: '*.jar')
}

Adding JBoss AS 7 libs as Gradle Dependency

I need to add JBoss 7.1.1 libs as a Gradle dependency in my project, because I need them in compile time.
But all the dependencies I have are added from a Maven Repo.
I have seen how to do it, I can add them as a providedCompile dependency, using this technique right here. But what do I associate it to?
How do I add libs that I have in my JBoss Modules as a Gradle dependency?
EDIT
I have seen I can add a file tree to my dependencies like this:
dependencies {
provided fileTree(dir: 'myDir', include: '*.jar')
}
But it doesn't work recursively. It only adds the defined folder,
and JBoss AS 7.1.1 has all the jars splitted in modules, is there any way I can reference the entire module folder, instead of each specific one?
What I want to do is somehting like this, but it doesn't work with this syntax:
dependencies {
provided fileTree(dir: '%JBOSS_HOME%/modules', include: '*.jar')
}
Try the following:
dependencies {
provided fileTree(dir: 'myDir', include: '**/*.jar')
}
This should add all .jar files in all subfolders of 'myDir'.
Explanation: The **/ before the *.jar tells it to recursively check all directories for the .jar files.

Categories