Android Studio - Importing external Library/Jar
AndroidIntellij IdeaDependenciesGradleAndroid StudioAndroid Problem Overview
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?
Android Solutions
Solution 1 - Android
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.
Solution 2 - Android
You don't need to close the project and go to command line to invoke grade:clean. Go to Build-> Rebuild Project
Solution 3 - Android
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.
Solution 4 - Android
"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!
Solution 5 - Android
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 twobuild.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
Solution 6 - Android
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
Solution 7 - Android
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.
Solution 8 - Android
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.
Solution 9 - Android
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.
Solution 10 - Android
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')
Solution 11 - Android
Solution 12 - Android
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.