Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - Importing external Library/Jar

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.

  1. Download a fresh copy of the Admobs SDK from Google.

  2. Copy the library jar GoogleAdMobAdsSdk-6.4.1.jar to the project's /libs/ folder.

  3. 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:

enter image description here

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?

like image 921
daniel_c05 Avatar asked May 27 '13 20:05

daniel_c05


People also ask

How can I use external JARs in an Android project?

Add your jar file to the folder app/libs . Then right click the jar file and click "add as library". You can then expand your Project to find the "libs" folder or right click your project and make a new directory. You can then move the JAR file for your library into that directory using your OS file manager.

How do I create a JAR file with an external library?

You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.


2 Answers

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.
like image 102
daniel_c05 Avatar answered Sep 23 '22 18:09

daniel_c05


You don't need to close the project and go to command line to invoke grade:clean. Go to Build-> Rebuild Project

like image 29
Tej Avatar answered Sep 22 '22 18:09

Tej