Android Studio - Importing external Library/Jar - android
I recently downloaded Android Studio to develop Android applications (I'm coming from Eclipse), and I am having issues using external libraries and/or external JAR files alongside my own project. NOTE: the following tests were conducted on a new application project created from scratch in Android Studio.
Example 1: JAR Import.
Download a fresh copy of the Admobs SDK from Google.
Copy the library jar GoogleAdMobAdsSdk-6.4.1.jar to the project's /libs/ folder.
In the project explorer, right click on the newly added library.jar and click on 'Add as Library'.
Technically at this point everything works, imports work just fine, the layout editor shows a preview of the AdView widget and all that. The only problem is that it doesn't compile successfully.
Log from console:
Gradle:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':APITests:compilePaidDebug'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Could not execute build using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
I tried running gradlew compileDebug --stacktrace, and the problem seems to be that the despite being able to import the classes successfully in both the code & design editor, at compile time, it cannot resolve the imports. Here's the relevant part of the log: (full stacktrace here)
java:6: error: package com.google.ads does not exist
import com.google.ads.AdRequest;
java:7: error: package com.google.ads does not exist
import com.google.ads.AdView;
java:11: error: cannot find symbol
AdView mAdView;
symbol: class AdView
location: class MainActivity
java:22: error: cannot find symbol
mAdView = (AdView)this.findViewById(R.id.adView);
symbol: class AdView
location: class MainActivity
java:23: error: cannot find symbol
mAdView.loadAd(new AdRequest());
symbol: class AdRequest
location: class MainActivity
5 errors
:Test:compileDebug FAILED
But again, the imports work well in the editor, and the dependency is there:
It's also worth noting that without attempting to add a library/JAR, the projects compiles just fine.
I then tried editing the build.gradle file to include the new lib like this:
dependencies {
compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')
}
This time, it did compile successfully, but the app now forces closes, as apparently, it cannot find a specific class from the lib in the application package.
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.foo.test-1.apk
Any ideas?
So,
Steps to follow in order to import a JAR sucesfully to your project using Android Studio 0.1.1:
Download the library.jar file and copy it to your /libs/ folder inside your application project.
Open the build.gradle file and edit your dependencies to include the new .jar file:
compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')
File -> Close Project
Open a command prompt on your project's root location, i.e 'C:\Users\Username\AndroidStudioProjects\MyApplicationProject\'
On the command prompt, type gradlew clean, wait till it's done.
Reopen your application project in Android Studio.
Test run your application and it should work succesfully.
You don't need to close the project and go to command line to invoke grade:clean.
Go to Build-> Rebuild Project
Easy way and it works for me. Using Android Studio 0.8.2.
Drag jar file under libs.
Press "Sync Project with Gradle Files" button.
"simple solution is here"
1 .Create a folder named libs under the app directory for that matter any directory within the project..
2 .Copy Paste your Library to libs folder
3.You simply copy the JAR to your libs/ directory and then from inside Android Studio, right click the Jar that shows up under libs/ > Add As Library..
Peace!
Here is how I got it going specifically for the admob sdk jar file:
Drag your jar file into the libs folder.
Right click on the jar file and select Add Library
now the jar file is a library lets add it to the compile path
Open the build.gradle file (note there are two build.gradle files at least, don't use the root one use the one in your project scope).
Find the dependencies section (for me i was trying to the admob -GoogleAdMobAdsSdk jar file)
e.g.
dependencies {
compile files('libs/android-support-v4.jar','libs/GoogleAdMobAdsSdk-6.3.1.jar')
}
Last go into settings.gradle and ensure it looks something like this:
include ':yourproject', ':yourproject:libs:GoogleAdMobAdsSdk-6.3.1'
Finally, Go to Build -> Rebuild Project
you export the project from Eclipse and then import the project from Android Studio, this should solve your problem, open a eclipse project without importing it from Android Studio you can cause problems, look at:
(Excuse my language, I speak Spanish.)
http://developer.android.com/intl/es/sdk/installing/migrate.html
I had the problem not able to load jar file in libs folder in Android Studio.
If you have added JAR file in libs folder, then just open build.gradle file and save it without editing anything else. If you have added this line
compile fileTree(dir: 'libs', include: ['*.jar'])
save it and clean the project .. In next build time Android Studio will load the JAR file.
Hope this helps.
I'm using Android Studio 0.5.2. So if your version is lower than mine my answer may not work for you.
3 ways to add a new Jar to your project:
Menu under Files-->Project Structure
Just press 'F4'
under Project navigation, right clink on any java library and a context menu will show then click on 'Open Library Settings'
A Project Structure window will popup.
On the left column click on 'Libraries' then look at the right pane where there is a plus sign '+' and click on it then enter the path to your new library.
Make sure the new library is under the 'project\libs\' folder otherwise you may get a broken link when you save your project source code.
I am currently using Android Studio 1.4.
For importing and adding libraries, I used the following flow ->
1. Press **Alt+Ctr+Shift+S** or Go to **File --> Project** Structure to open up the Project Structure Dialog Box.
2. Click on **Modules** to which you want to link the JAR to and Go to the Dependency Tab.
3. Click on "**+**" Button to pop up Choose Library Dependency.
4. Search/Select the dependency and rebuild the project.
I used the above approach to import support v4 and v13 libraries.
I hope this is helpful and clears up the flow.
I use android studio 0.8.6 and for importing external library in project , paste that library in libs folder and inside build.gradle write path of that library inside dependencies like this
compile files('libs/google-play-services.jar')
In Android Studio (mine is 2.3.1) go to File - Project Structure:
Check the libs version requirements. The lib might be trying to call a method from from a higher Android version. Try adding the apps compatability libs.
When all else fails try switching from a Gradle build to an Eclipse build.
Delete and restructure your folders to the eclipse style and delete everything but your code. Then reload the project with your libs.
Once it runs go back to Gradle, if you wish.
Related
Importing roottools.jar into Android Studio
I'm trying to import roottools from here: https://code.google.com/p/roottools/ (jar file) to the Android Studio project. So far I've tried the following: copy RootTools-javadoc-3.4.jar into libs directory under my project. Add imports Reorganize imports. Rebuild project But still I don't have "acces" to it's methods. Is it possible that is only compatible with Eclipse?
I tried using the JAR available for download and it didn't work either. To get this working, I had to download the project available at github here. Compile and run the task "makeJar" (right click on task makeJar in the gradle.build and select run). I just added this new jar in the libs and referenced in the gradle.build file. dependencies { ... compile files('libs/RootTools.jar') } or you can add it via Project Properties. It worked perfectly (don't forget to sync your the gradle files if you edit it manually).
In your project, choose File menu > Project Structure (there's a bug in 0.4.4 and this menu item won't have a title at all; it still works) > Modules > choose your module > Dependencies > + button > File dependency > choose your library from the file picker. That file needs to be somewhere beneath the root directory of your projet; the libs directory is fine. Screenshot of the Project Structure dialog:
Android Studio fail add libs Universal Image Loader
Android-Universal-Image-Loader(https://github.com/nostra13/Android-Universal-Image-Loader) is my favorite library. But it can't add libs when i use Android Studio(use 0.1.5 version). i know how to add libs in Android Studio (https://stackoverflow.com/a/16628496/397457) when i add Android-Universal-Image-Loader libs to Android Studio. Editor error about "can't reslove R". i try this Max OSX(i try too Windows but fail. Windows error message => "Gradle: error: package com.nostra13.universalimageloader.core does not exist". but Editor no error message. only show when i run application) other libs like Gson no problem. but only Android-Universal-Image-Loader have this problem. Your Android Studio success add Android-Universal-Image-Loader? if success tell me how to.
I just added the universal-image-loader like this: Firstly download the universal-image-loader-1.8.5-with-sources.jar. (For 5/2014. universal-image-loader-1.9.2.jar works great. Don't need the "with-sources" file.) Then put it on my MyAppProject/MyApp/libs. Right clicking on universal-image-loader-1.8.5-with-sources.jar i add it as a library with the default configuration. Finally on MyAppProject/MyApp/build.gradle add the following: dependencies { compile files('libs/universal-image-loader-1.8.5-with-sources.jar') }
From the error "package com.nostra13.universalimageloader.core does not exist", it seems like you have not added the source as the library. Right click the "universal-image-loader-1.8.4.jar" file under the libs folder and from the context menu, click "Add as a library" option. If after this you are getting the "ClassDefNotFoundException", please follow the following steps: Add the library to the project. Edit your Gradle build file and add the dependency for the UIL in it Close Android Studio Open command prompt and go to the project's root folder (you will find 'gradlew' file there) and clean your build by issuing the clean command C:\CarApplicationProject> gradlew clean Restart Android Studio.
1.Put the jar (in my case, gson-2.2.4.jar) into the libs folder. 2.Ensure that compile files ('libs/gson-2.2.4.jar') is in your build.gradle file. 3.Now Click on the "Sync Project with Gradle files"(Left to AVD manager Button on the topbar). After I did the above three, it started working fine.
There are two ways to use Universal Image Loader By downloading Jar and add it to lib folder and compile files('libs/universal-image-loader-1.9.5-with-sources.jar') Simply add gradle dependency compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' And here is a quick tutorial link
Why can't I add mopub-android-sdk as library?
I want to use MoPub for putting ads in my Android app. I have downloaded the required resources and set-up a library project (mopub-android-sdk) in Eclipse. Now I want to add this project as a library to my Android app. But when I try to do that it gives this error:
Are the mopub-android-sdk files in the same parent folder as your project? This seems like the same problem addressed in this question/answer: In eclipse, unable to reference an android library project in another android project Place the referencing project in the same physical folder and see if the error persists.
To get around this, don't set up the mopub-sdk as a library project. You can simply import it as a standard project. Go to File > Import >Existing Projects Into Workspace, find the right project and click finish. Right click your actual project and then click Build Path. Go to Link Source and then select the location of the project you just imported. That should resolve the issue for you without requiring you to build a JAR. -Franklin
Module dependency not detected during compile in IntelliJ IDEA 11
I am developing an Android app and I'm trying to include GraphView to plot some graphs. It is required to include it as a module dependency. After I do that, the IntelliJ IDE properly detects the packages and I can import them, but compiler throws errors when trying to compile: package com.jjoe64.graphview.GraphView does not exist cannot find symbol class GraphView etc. I followed this guide to include it: http://wiki.jetbrains.net/intellij/Sharing_and_re-using_Android_resources_through_library_projects
If it still occures to anyone , for me the reason was that the module's Android class libarary source folder was not marked as 'Sources' directory . Simply click on the sources folder and choose mark directory as --> source folder. After that instead of compile , choose 'make' , and then it will start working for you. Best of luck with that annoying bug !
I had the same problem and this is how I solved it: I was using Gradle so I right-clicked on the build.gradle.kts (it may just be build.gradle in your case) and clicked on "Import Gradle project" and everything worked again!
Why can't android find com.google.gson.Gson
I'm trying to use GSON in my project, but my application is crashing, with logcat saying that com.google.gson.Gson cannot be found. I've put import com.google.gson.Gson on my class files, I have gson in my package explorer, and added it by Right click -> build path -> add libraries. It also shows up in Project->properties->java build path->libraries tab->gson. What have I done wrong?
What worked for me: Check the checkbox next to the lib (gson-2.0.jar) in: 'Project Properties' -> 'Java Build Path' -> 'Order and Export' tab. Then do a clean/build. This adds the exported=true attribute to the classpath entry <classpathentry exported="true" kind="lib" path="libs/gson-2.0.jar"/>
Have you put the jar file in the libs folder of the project? If not , try moving it there, creating the folder if required. Should be at the same level as the src folder. I had a similar issue trying to get an app built and that solved it.
I'm using IntelliJ, not eclipse but here is how I did it: Download the source files from https://google-gson.googlecode.com/files/google-gson-2.2.4-release.zip Unpack them Copy them into the "libs" folder of your project. I did this manually by using the terminal but you can do it by going in your project folder located in your computer. Your Project_Name >app >Libs [paste them here] Go to your editor (IntelliJ in my case) You should see the the following 3 files in the libs folder of the project structure: gson-2.2.4.jar gson-2.2.4-javadoc.jar gson-2.2.4-sources.jar Open your build.gradle file and insert the following line in the dependencies section: compile files('libs/gson-2.2.4.jar', 'libs/gson-2.2.4-javadoc.jar', 'libs/gson-2.2.4-javadoc.jar') Right click on each of the source files and select "Add as Library..." Use the following options: Name: name of the jar file Level: Project Library Add to Module: select project your adding to Rebuild project Build > Rebuild project Import and use it! import com.google.gson.Gson;
I have observed today that it doesn't like when you add your library. The solution that worked for me was to add it as an external jar only. I have observed it by doing the steps described by author of this post i.e. Properties -> Java Build Path -> Libraries -> Add External JARs and point to the downloaded gson library.
I ran into the same issue (when installed SDK 17) ... The solution is, that you just put the pure jar files into the "libs" folder (without subfolders). You also don't need to declare them, android wil find them itself. You may need to do a Project > Clean
I had similar problem, but I guess my requirement was more complicated as the GSON libarary was used by an Android Library Project that my main Android project depended on. I have tried both approaches above but none of them worked for me. If I add the GSON jar file directly to the main Android project it works, but that is not what I wanted. With a bit of investigation I have realised that the GSON jar does not use any dependencies (third party libraries) itself. So my solution was to copy the source file of the GSON project to my Android Library project's src filder and it worked like a charm. so you can use the Source code rather then the binary code. The source code is part of the download in a jar file, just unzip it.
i'm sure you have copied the jar into "libs", and also added property> Java Build Path >(tab) Libraries, right? my issue was stupid... please check other than Gson jar, if you have two stuffs at least in above tab: android x.x Android Dependencies (especially this one, i missed this, eclipse show me an error of "could not find class com.google.gson.xx" ) simply do a project copy will solve this problem...
Do not edit .classpath file directly or even put files into lib folders out of Eclipse. Instead use the Java Build Path->Order and Export tab to select libraries to be exported in the final deployment unit (eg war/apk) To be on safer side, do a Project->Clean after you change the build path.
I faced the same thing.But the solution was quite easy,just Right-Click on your project->Properies->Libraries->Add external jars->OK and thats it.It solved my problem.hope it will solve yours as well
If Projct Properties -> Java build path -> Add external jars doesn't work and your project is a web project, try adding the gson jar directly to the server lib folder. Example: for apache Tomcat to apache-tomcat/lib.
I have been trying the same thing and read so many answers and tried so many things but to not vial, but i solved this problem by doing just one click and its simple. 1) Add your .jar file in libs folder make sure its libs not lib 2) Clean and Build your project you will see that file under libs folder 3) right click the file and go to build path and select add to build path Run your project it will work fine. Hope this helps
I am using Android Studio I had the same problem I had solve it by select project press f4 from popup click app and in dependencies select file dependency and select the three folders which I had pasted in lib directory