How do I add a library project (such as Youtube Jar) to Android Studio?
(Not to the old ADT Eclipse-based bundle, but to the new Android Studio.)
i have one more question i if i have old version of youtube library in jar and i never want to update its affected to my future versionof android app or not.
Inside your project's app folder, there is one directory named libs which is empty may be. You need to paste your .jar lib file inside that libs folder.
After then go to your app's build.gradle file. In it inside your dependencies{} block write this line of code.
Inside your app's build.gradle file:
dependencies {
implementation files('libs/youtube_or_your_jar_file_name.jar')
}
I built one android studio project. I wanted to use it as a library in another project. I followed below steps for same:
Changed apply plugin: 'com.android.application' to 'com.android.library'
Removed applicationId from defaultConfig{}
Clicked on sync
This gave me .aar file in build-->outputs-->aar-->myapp.aar
I imported this .aar file in my other test project. I followed below steps for this:
File-->New-->New module-->Import .jar/.aar package-->myapp.aar
In build.grade of test application added compile project(path: ':myapp')
Clicked on sync.
This produced one folder inside external libraries called myapp-unspecified.
There i get all res files but i did not get the class files inside classes.jar.
Inside classes.jar I only have MANIFEST.MF.
Am I doing something wrong or am I missing something??
Experienced the same problem. My issue was that I had removed a dependency from my library project (in this case, testCompile 'junit:junit:4.12'), but did not delete the directory and classes that referenced the APIs from that dependency. An aar file was output to the build folder, but I was not able to access its contents when I included it in my application project.
After deleting the unused classes and directories and re-syncing my library project, I was able to produce a working aar.
Ran into the same issue. The classes are actually there or at least were for me. All that I needed to do was right click the application I was importing my library into. Click open module settings. Then go to dependencies on the right side. Add your library there as well so that the java will compile will your application.
I want import .arr in my library module not main module,how can i do?
in my library module build.gradle set aar, err: can not find it.
dependencies {
compile(name:'nameArrFile', ext:'aar')
}
repositories{
flatDir{
dirs 'libs'
}
}
To Add to the answer: From the Android Studio File Menu,
Click Import .JAR/.AAR Package then click Next.
Enter the location of the compiled AAR or JAR file then click Finish.
Import the library module to your project
(the library source becomes part of your project):
Click File > New > Import Module.
Enter the location of the library module directory then click Finish.
The library module is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
Make sure the library is listed at the top of your settings.gradle file, as shown here for a library named "my-library-module":
include ':app', ':my-library-module'
Open the app module's build.gradle file and add a new line to the dependencies block as shown in the following snippet:
dependencies {
compile project(":my-library-module")
}
Click Sync Project with Gradle Files.
In this example above, the compile configuration adds the library named my-library-module as a build dependency for the entire app module.
If you instead want the library only for a specific build variant, then instead of compile, use buildVariantNameCompile. For example, if you want to include the library only in your "pro" product flavor, it looks like this:
Your code is correct. Make sure the following things are in place and it will work just fine.:
nameAarFile should be just the file name without the extension .aar. Also make sure there is no spelling error in the file name.
make sure the extension of the actual aar file is right. I see you have mistyped it as arr at several places in your question (I corrected). make sure the same is no the case with extension in your file name
Make sure the .aar file is in the libs folder. If in any other directory either move to libs or include the other directory under FlatDir - > dirs
I am using Android Studio.
I have created a library project.and I have successfully included this library in another prstoject.I have followed the below steps to include that library in my project.
a.I have added that library folder in my project root folder.
b.In settings.gradle file include ':library'
c.In build.gradle file compile project(':library')
Now I want to add library as a gradle file.I want to implement just one line in build.gradle file compile 'com.example.library' and then I can access that library files etc.I don't want to add the library folder in my project.Because when the library folder gets increased at that time my project size is also get increased which is not beneficial for me.So guys please suggest me how can I achieve this.How can I get that gradle file or if you have any idea please suggest me.
Read this. This might help. How to distribute your own Android library through jCenter and Maven Central from Android Studio
I have created an Android project and added an external JAR (hessian-4.0.1.jar) to my project. I then added the JAR to the build path and checked it off in Order and Export.
Order and Export is ignored it seems, and all classes from the external JAR are missing at runtime.
Is there a trick to properly include the needed classes from an external JAR when building an Android application using the Eclipse plugin? I do not want to use ant or Maven.
For Eclipse
A good way to add external JARs to your Android project or any Java project is:
Create a folder called libs in your project's root folder
Copy your JAR files to the libs folder
Now right click on the Jar file and then select Build Path > Add to Build
Path, which will create a folder called 'Referenced Libraries' within your
project
By doing this, you will not
lose your libraries that are being referenced on your
hard drive whenever you transfer your project to another computer.
For Android Studio
If you are in Android View in project explorer, change it to Project view as below
Right click the desired module where you would like to add the external library, then select New > Directroy and name it as 'libs'
Now copy the blah_blah.jar into the 'libs' folder
Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradle as compile files('libs/blah_blah.jar') and sync the gradle. And you are done
Please Note : If you are using 3rd party libraries then it is better to use transitive dependencies where Gradle script automatically downloads the JAR and the dependency JAR when gradle script run.
Ex : compile 'com.google.android.gms:play-services-ads:9.4.0'
Read more about Gradle Dependency Mangement
Yes, you can use it. Here is how:
Your Project -> right click -> Import -> File System -> yourjar.jar
Your Project -> right click -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
This video might be useful in case you are having some issues.
I know the OP ends his question with reference to the Eclipse plugin, but I arrived here with a search that didn't specify Eclipse. So here goes for Android Studio:
Add jar file to libs directory (such as copy/paste)
Right-Click on jar file and select "Add as Library..."
click "Ok" on next dialog or renamed if you choose to.
That's it!
I'm currently using SDK 20.0.3 and none of the previous solutions worked for me.
The reason that hessdroid works where hess failed is because the two jar files contain java that is compiled for different virtual machines. The byte code created by the Java compiler is not guaranteed to run on the Dalvik virtual machine. The byte code created by the Android compiler is not guaranteed to run on the Java virtual machine.
In my case I had access to the source code and was able to create an Android jar file for it using the method that I described here: https://stackoverflow.com/a/13144382/545064
Turns out I have not looked good enough at my stack trace, the problem is not that the external JAR is not included.
The problem is that Android platform is missing javax.naming.* and many other packages that the external JAR has dependencies too.
Adding external JAR files, and setting Order and Export in Eclipse works as expected with Android projects.
Goto Current Project
RightClick->Properties->Java Build Path->Add Jar Files into Libraries -> Click OK
Then it is added into the Referenced Libraries File in your Current Project .
Android's Java API does not support javax.naming.* and many other javax.* stuff. You need to include the dependencies as separate jars.
If using Android Studio, do the following (I've copied and modified #Vinayak Bs answer):
Select the Project view in the Project sideview (instead of Packages or Android)
Create a folder called libs in your project's root folder
Copy your JAR files to the libs folder
The sideview will be updated and the JAR files will show up in your project
Now right click on each JAR file you want to import and then select "Add as Library...", which will include it in your project
After that, all you need to do is reference the new classes in your code, eg. import javax.mail.*
in android studio if using gradle
add this to build.gradle
compile fileTree(dir: 'libs', include: ['*.jar'])
and add the jar file to libs folder
If you are using gradle build system, follow these steps:
put jar files inside respective libs folder of your android app. You will generally find it at Project > app > libs. If libs folder is missing, create one.
add this to your build.gradle file your app. (Not to your Project's build.gradle)
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
// other dependencies
}
This will include all your jar files available in libs folder.
If don't want to include all jar files, then you can add it individually.
compile fileTree(dir: 'libs', include: 'file.jar')
create a folder (like lib) inside your project, copy your jar to that folder.
now go to configure build path from right click on project, there in build path select
'add jar' browse to the folder you created and pick the jar.
Copying the .jar file into the Android project's folder isn't always possible.
Especially if it's an output of another project in your workspace, and it keeps getting updated.
To solve this you'll have to add the jar as a linked file to your project, instead of importing it (which will copy it locally).
In the UI choose:
Project -> Import -> File System -> yourjar.jar -> (Options area) Advanced -> Create link in workspace.
The link is save in the .project file:
<linkedResources>
<link>
<name>yourjar.jar</name>
<type>1</type>
<locationURI>PARENT-5-PROJECT_LOC/bin/android_ndk10d_armeabi-v7a/yourjar.jar</locationURI>
</link>
</linkedResources>
PARENT-5-PROJECT_LOC means relative to the project file, 5 directories up (../../../../../).
Then add it to the libraries:
Project -> Properties -> Java Build Path -> Libraries -> Add Jar -> yourjar.jar
In the same window choose the Order and Export tab and mark your jar so it will be added to the apk.