Something strange with added in Android Studio .jar lib - java

I added in my Android Studio Project .jar library.
When I try to use its classes (an example NumberUtils class) inside a package directory, Android Studio is not see classes (but I know, that they exist):
But when I create class out of package directory - it's all ok:
How can I use it insida a package?
Edit:
Edit 2:

compile fileTree(dir: 'libs', include: ['*.jar'])
tells the build system that any JAR file inside app/libs is a dependency and should be included in the compilation classpath and in the final package.
So no need to add again like
compile files('libs/NumberToWordsLib.jar')
Remove this then sync and rebuild your project. Hope this will help you.

You should add a dependent library, from file->projectstructure->dependencies
and also after adding the new library as dependency, you have to do a clean Build, usually it does a gradle synch automatically, if you add new library
EDIT
I do not see any package declarations there

#Srinivasan show right way for resolving this problem.
This lib didn't have a package name, only classes inside. But when I added package name inside jar library - it bacame to work (when I use 'import' phrase).

Related

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.

How do I use a class from an imported jar file in Android Studio

I imported a jar file that I created with Eclipse to my Android Studio library but I don't know how to use the classes from it. Imported it here and I'm now trying to use the logarithm class
I tried to somehow get it with the import word on the beginning of a class but I don't get it to work. Sorry I'm a beginner. :p
Ok follow this:
Put the .jar into the libs folder
Put compile files('libs/your.jar') is in your build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files)
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. If this does not work then use the terminal tab and do this: gradlew clean.
Hope it helps!!!

Is it possible to change the java code files of external libraries downloaded by gradle in android studio?

I am now working with OSMDroid and I want to modify two java files that was downloaded by gradle and then put into External libraries section. The two java files are : MapTileFileArchiveProv.java and ZipFileArchive.java. The are located in one of the packages in a classes.jar file. I am 90% sure that they are compiled and cannot be modified just by opening them in the editor, edit them and save them.
What are my options here?
Since OSMDroid is on GitHub you can fork it, modify whatever files, and build it yourself. You could then have gradle reference it directly as a project or put the output jar(s) into your project's "libs" dir and then add the following as a dependency:
compile fileTree(dir: 'libs', include: ['*.jar'])
The downside of this is you'll have to maintain your fork by merging in future changesets periodically if you want to stay up to date with the master repo.

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
}

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.

Categories