Importing a JAR into Android Studio 2.2 - java

I'm trying to import a JAR that I made in Groovy into an Android Studio 2.2 project and all the instructions I can find say to just add it under Project Structure -> app -> Dependencies. I did this and it's still not letting me use the imported JAR. I've imported JAR files from Groovy into other Java projects in Eclipse. Did something change in Android Studio 2.2 that changes how you're supposed to do this?

I thinnk this step help You
1 ) Put the jar (in my case, yourJar-2.2.4.jar) into the libs folder.
2) Right click it and hit 'Add as library'.
3) Ensure that compile files('libs/yourJar-2.2.4.jar') is in your build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files).
4) 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.
5) Clean Project.
6) Run.
May be it,s Working

I figured it out but I'm still not sure why it works. I changed the package name of the JAR to match the Android project. My Android project is com.johnwstanford.simplegps and I originally just called the JAR package SimpleGpsOutputFormatter. I changed it to com.johnwstanford.simplegps.SimpleGpsOutputFormatter and for some reason, that made it work. Anyone know why that would make a difference?

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!!!

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.

Androidstudio - gradle - cordova: import.org.apache.cordova.* fails

because of a high priority security fix in cordova for android we need to update our cordova version (from 2.7.0 to 3.6.3) in our app. So we thought it would be better to create a new project.
I installed cordova and create a project with cordova cli api. Also I added the android platform added many cordova plugins. Then I copied our sencha touch stuff into the www folder.
Then I called "cordova build android" (before I also called "cordova prepare android").
After that I imported this android project in Android Studio (0.8.11).
I needed to configure the gradle files a bit (buildToolsVersion, etc.)
For our app I needed to copy the following things from our old project
AndroidManifest.xml
All the stuff out of the res/ directory
in the src/ directory I added some stuff we need in our app
Then I recognized that the android-support android-support-v13.jar library was missing in the lib/ folder. So I copied it out of the sdk\extras\android\support\v13 directory. After that nearly all red stuff in the code disappeared, so I tried to run it.
The app starts but I'm getting many errors like: "Cordova exec fail: Class not found".
So I looked into our src/ directory and there in our java files. There is everything red because the impor of
import org.apache.cordova.*; or
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
fails.
The needed java files I found in /CordovaLib/ant-build/classes/com/org/apacke/cordova/
Is there anything which I still need to configure?
My gradle file dependencies:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
for (subproject in getProjectList()) {
compile project(subproject)
}
}
Please help me/ Thanks ahead!! :-)

Categories