libs folder doesn't work in android studio - android

I am using the mac version of android studio 1.2 Beta 2, built on April 7, 2015. Gradle version 2.2.1 and Gradle plug-in version 1.1.0.
After I create a new project, The following code exist by default in my app/build.gradle file.
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
It means that any jar file I add to the app/libs directory should be loaded to my project, right? However, when I add my Parse-1.9.0.jar to the app/libs, it is not loaded. I failed to use the code from the API in MainActivity.java.
I got it working by right click Parse-1.9.0.jar -> Add As Library... The app/build.gradle became:
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/Parse-1.9.0.jar')
}
I am confused of why. Shouldn't compile fileTree() include all the jar files? Why do I need to do the additional step?
The Parse-1.9.0.jar file I downloaded is from: https://parse.com/apps/quickstart#parse_data/mobile/android/native/existing
Thanks

(1) Select the Packages view for the project
(2) Drag your library (jar) file into the "libs" folder in Packages view
(3) Right-click the newly added jar file - and select "Add as library" in the dialogue - Then click OK in next dialogue (Gradel Sync will start)

For every change you make to your libs folder (add, remove .jar) you need to sync your gradle files. Jar file libraries are not discovered automatically by AS - as it was with Eclipse.

I don't know 'why' is fileTree option not working. But surprisingly, wildcard approach works.
dependencies {
compile files('libs/*.jar')
}

implementation fileTree(dir: "libs", include: ["*.jar"])

Related

JAR libs are not added to the apk file

I've moved my project from ant build to gradle build, but I can't get it to use the jars from my libs directory.
I have created app/libs directory
I have moved there my jars
I have added compile fileTree(dir: 'libs', include: '*.jar') to the app/build.gradle
Unfortunately, it didn't work, the classes from those jars are not available during runtime.
I have ran the gradle from command line with debug option, to check the whole output, and it's clear to me that they are not processed.
I know I'm doing something wrong, but I have no idea what. I thought that in my other projects I have used it multiple times, but after going through every one of them, I can see that I have never used jars loaded from libs directory.
What am I missing?
I assume you are using Android Studio and you have a .jar file which you want to include in your project : (get rid of any changes you made in that matter- I just opened a new empty project:)
right click on your app name in project structure column and click on 'open Module settings'
go to dependency tab and click on '+' and click on 'Jar dependency' and navigate to your jar library (you can also look for a online library dependency from 'library dependency' if you do not have local library)
that's it , it will add your jar library to your project (automatically will add it to the app/build.gradle)
Edited:
if you are not using Gui then add this to your Gradle under dependencies
compile fileTree(include: '*.jar', dir: 'libs')
and make sure you have libs directory on root(with your jar in it)
finally run a clean build
First you are missing the brackets in this line fileTree(dir: 'libs', include: '*.jar').
You need to write like this
compile fileTree(dir: 'libs', include: ['*.jar'])
Then make sure that you have added your .jar files to Project Structure->app->dependencies->Plus button -> add file dependency.

How do I know that I've imported a jar properly in AndroidStudio project

I'm trying to use a third party library in AndroidStudio and am having some problems, it is the JSch library.
What I did was download the ".jar file
On my app within Module Settings I added it
And then selecting the app module again I added it within the dependencies
Yet when I try to code it into my files through "import com." it does not show.
I am too new at this to know if I added it incorrectly or if it has a problem within the ".jar" file.
Does this ring a bell with anyone who might know what I am doing wrong?
Just follow this steps:
1.- Put the jar into the libs folder
2.- Right click it and select "Add as library"
3.- Check that your build.gradle file contains:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You'll provably need to do a clean build
Add library to yours app dependencies:
dependencies {
// Other libraries...
compile group: 'jsch', name: 'jsch', version: '0.1.29'
}
And synchronize your gradle.

Adding Parse-1.8.0 to Android Studio 1.0.1 (Or any .zip file)

So there seems to be a few threads that attempt to explain how to add .zip files to android studio but I am making no progress. I am migrating from eclipse so perhaps that is why I am so incompetent in figuring out how to perform this task. Can someone please explain how to add external libraries to Android Studio v1.0.1? In eclipse it was simply importing jar/zip and done.
I was in the same situation, trying to integrate Parse 1.8 with Android Studio 1.0.2.
On Parse's instruction page, it simply tells you to import the library into Android studio, which isn't too detailed. Here is how I solved this problem.
Choose to import from a "Non Android Studio Project", right when Android Studio starts.
When it asks you to choose the project, give the path, on Windows e.g.C:/path/to/parsesdk/. On *nix systems, it should be to where you have extracted to it, /home/user/path/to/parsesdk.
After you choose the path, Android Studio will import the project accordingly.
Click on the Application root folder (the top most folder in the folder hierarchy to the left), right click > New > Package > and add it under the src folder, name is libs.
Copy paste the jar to the libs folder (I only copied the jar file, as I didn't need the other extra material)
Right click on the jar, there should be an option labeled as "Add as library" towards the bottom, click on that.
Android Studio will automatically add the following to the build.gradle file.
compile files('src/libs/Parse-1.8.0.jar')
Once the above step is completed, click on File > Project Structure.
On the left, there should be a section called "Modules", click on the "Dependencies" tab on the top.
Click the green "+" sign > Module Dependency
Select the module from the list.
Last thing, in the build.gradle file for the "ParseStarterProject" module, if there is a red line under the classpath, change it to the following
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
After completing this step, I was able to successfully build the app.
Hope this helps :)
If you are using gradle with android studio (which is the preferred way now) you can include jars in a folder using this code snipet from my build.gradle.
buildscript {
repositories {
flatDir { dirs 'c:\\path\\to\\folder' }
mavenCentral()
}
}
Or by including a single file in depencies like below.
dependencies {
compile fileTree(dir: 'a-folder-in-root-of-project', include: 'a_jar.jar')
}
This is the only way it worked for me :
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Parse
compile 'com.parse.bolts:bolts-android:1.+'
compile files('libs/Parse-1.9.2/Parse-1.9.2.jar')
compile files('libs/Parse-1.9.2/ParseCrashReporting-1.9.2.jar')
compile files('libs/Parse-1.9.2/ParseFacebookUtilsV3-1.9.2.jar')
compile files('libs/Parse-1.9.2/ParseFacebookUtilsV4-1.9.2.jar')
compile files('libs/Parse-1.9.2/bolts-android-1.2.0-javadoc.jar')
compile files('libs/Parse-1.9.2/bolts-android-1.2.0.jar')
}
OR
//Parse
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs/Parse-1.9.2', include: 'Parse-*.jar')
compile fileTree(dir: 'libs/Parse-1.9.2', include: 'ParseCrashReporting-*.jar')

How to add a jar in External Libraries in Android Studio?

I am new to Android Studio.
How can I add a few jar files in the External Libraries below the < JDK > folder?
A late answer, although I thought of giving an in-depth answer to this question. This method is suitable for Android Studio 1.0.0 and above.
STEPS
First switch your folder structure from Android to Project.
Now search for the libs folder inside the app folder.
Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library. This will take care of adding compile files('libs/library_name.jar') in build.gradle [You don't have to manually enter this in your build file].
Now you can start using the library in your project.
Add your jar file to the folder app/libs. Then right click the jar file and click "add as library".
If there is no libs folder you can create it. Click on the combo box that says "Android", and change it to "Project"
From here, you can right click on "apps" in the directory tree and go to "New" => "Directory"
Put your JAR in app/libs, and in app/build.gradle add in the dependencies section:
implementation fileTree(dir: 'libs', include: ['*.jar'])
Create "libs" folder in app directory copy your jar file in libs folder right click on your jar file in Android Studio and Add As library...
Then open build.gradle and add this:
dependencies {
implementation files('libs/your jar file.jar')
}
Step 1: Download any JAR file for your Project.
Step 2: Copy .jar file and past in libs folder.
Step 3: Click on File > Project Structure >Select app > Dependencies
Step 4:
Step 5:
Step 6: After click Ok button then we can see the Dependencies add like this way:
In Android Studio version 3.0 or more I have used bellow like :
Create libs to the app directory if not exist folder like
Set Project view in upper left corner
Go to project
Go to app
click on apps->New->Directory
name the folder libs
paste the jar to the libs folder
directory will look like bellow image
In build.gradle add these lines
// Add this line if was not added before.
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation files('libs/com.ibm.icu_3.4.4.1.jar')
Example with Parse jar...
Add jars to libs folder from Project view … create lib folder if not exists
Copy all jars there...
Add libs to gradle.... in build.gradle file :
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:percent:23.0.0'
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs', include: 'Parse-*.jar’)
}
For add all jars of lib folder... change Parse-*.jar to *.jar
This is how you can add .jar file in Android Studio 2.1.3.
Copy the .jar file
paste the file in Libs folder and then right click on .jar file and press Add as library
open build.gradle
add lines under dependencies as shown in screenshot
Now press play button and you are done adding .jar file
The GUI based approach would be to add an additional module in your project.
From the File menu select Project Structure and click on the green
plus icon on the top left.
The new Module dialog pops
From the phone and tablet application group select the "Import JAR or AAR package" option and click next.
Follow the steps to create a new module that contains your JAR file.
Click on the entry that corresponds to your main project and select the dependencies tab.
Add a dependency to the module that you created in step 4.
One final piece of advice. Make sure that the JAR file you include is build with at most JDK 1.7. Many problems relating to error message "com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)" root straight to this :0.
A simple way to add Jar file Android Studio Steps:
Copy and paste your jar file to libs folder of your project.
Click File from File menu -> Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X).
Select Modules at the left panel -> Dependencies tab.
Add... → Project Library → Attach Jar.
If anyone is looking for another solution without actually copying the jar file(s) into the project directory, e.g. when using a jar in multiple projects:
Open build.gradle and add
def myJarFolder = 'C:/Path/To/My/Jars'
[...]
dependencies {
[...]
compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
compile fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
[...]
}
Note that of course you don't have to use the myJarFolder variable, I find it useful though. The path can also be relative, e.g. ../../Path/To/My/Jars.
Tested with AndroidStudio 3.0
Update:
For Gradle Plugin > 3.0 use implementation instead of compile:
dependencies {
[...]
implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/1', include: ['*.jar'])
implementation fileTree(dir: myJarFolder + '/jar/Sub/Folder/2', include: ['*.jar'])
[...]
}
Create libs folder under app folder and copy jar file into it.
Add following line to dependcies in app build.gradle file:
implementation fileTree(include: '*.jar', dir: 'libs')
If you dont see option "Add as Library", make sure you extract (unzip) your file so that you have mail.jar and not mail.zip.
Then right click your file, and you can see the option "Add as library".
Please provide the jar file location in build.gradle
implementation fileTree(dir: '<DirName>', include: ['*.jar'])
Example:
implementation fileTree(dir: 'C:\Downloads', include: ['*.jar'])
To add single jar file
implementation files('libs/foo.jar')
Note:
compile is deprecated in latest gradle, hence use implementation instead.
The "official way" to add a jar into your android project as an
external library, is to add the jar in dependencies { } section in
build.gradle.
If you've done all of the above, and none of the above works, then there are two other possibilities:
If Android Studio or some other IDE doesn't give red underlined errors in editor but runs in error in the Java Compiler, CHECK YOUR JAR.
Your jar may not have a proper Manifest included therefore the compiler doesn't know what the jar can provide/don't know it's package name
Maybe it's folder structure is messed up. The folder structure doesn't match with its package name. package a.b.c; should be matching to folder a > folder b > folder c.
The package name has some conflict. (Trust me, this happened to me and it took hours to figure it out.)
However, if you are going with cordova, here are some tips of adding external jars.
"build-extras.gradle" is the better way to manage your gradle file.
Here are the steps to manage extra settings in cordova-based android project:
Adding your jar into build-extras.gradle:
//Other settings go here, e.g.: buildscript { ... }
ext.postBuildExtras = {
// you may have some other settings, e.g.: android { ... }
dependencies {
compile files('libs/abc.jar')
}
}
(More detailed steps here: Extending the cordova gradle file to include google services )
In terminal, do:
cordova build android
Manually:
Add libs folder in your project(Where build.gradle is located). E.g. app
Move .jar into libs
Add implementation files('libs/<name>.jar') into build.gradle. It is the same as UI Add as library do

How to Import jar file into an Android project?

I am trying to use the loopj library in my Android project. In the project home page it says "Download the latest .jar file from github and place it in your Android apps libs folder". I did that!
As indicated in the documentation of the loopj I try to import it using the following
import com.loopj.android.http;
and nothing happens. It is not even coming up in intellisense.
Am I doing anything wrong?
in the build.gradle file add your jar such as below then rebuild.
dependencies {
compile files('libs/android-async-http-1.4.4.jar')
}
into the build.gradle file add the following::
dependencies {
// ... other dependencies
compile files('libs/<your jar's name here>')
}
Rebuid your project or Run ./gradlew assemble. This should compile the project with the library.
To let the Android Studio load all of your jar files by itself automatically, just write this line in the dependency section
compile fileTree(dir: 'libs', include: ['*.jar'])
This will help android studio to load all of your jar files available in libs folder.

Categories

Resources