title: Android Studio - Importing external Library/Jar
body: <p>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. <strong>NOTE</strong>: the following tests were conducted on a <strong>new</strong> application project created from scratch in Android Studio.</p>
  
  <p>Example 1: JAR Import.</p>
  
  <ol>
  <li><p>Download a fresh copy of the Admobs SDK from Google.</p></li>
  <li><p>Copy the library jar <code>GoogleAdMobAdsSdk-6.4.1.jar</code> to the project's /libs/ folder.</p></li>
  <li><p>In the project explorer, right click on the newly added library.jar and click on 'Add as Library'.</p></li>
  </ol>
  
  <p>Technically at this point <strong>everything works</strong>, imports work just fine, the layout editor shows a preview of the <code>AdView</code> widget and all that. The <strong>only</strong> problem is that it doesn't compile successfully.</p>
  
  <p>Log from console:</p>
  
  <pre><code>Gradle: 
  FAILURE: Build failed with an exception.
  
  * What went wrong:
  Execution failed for task ':APITests:compilePaidDebug'.
  &gt; 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'.
  </code></pre>
  
  <p>I tried running <code>gradlew compileDebug --stacktrace</code>, and the problem seems to be that the despite being able to import the classes successfully in both the code &amp; design editor, at compile time, it cannot resolve the imports. Here's the relevant part of the log: (<a href="https://gist.github.com/daniel-c05/a70a4b3551089cfd731b" rel="nofollow noreferrer">full stacktrace here</a>)</p>
  
  <pre><code>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
  </code></pre>
  
  <p>But again, the imports work well in the editor, and the dependency is there:</p>
  
  <p><img src="https://i.stack.imgur.com/kPnZt.png" alt="enter image description here"></p>
  
  <p>It's also worth noting that without attempting to add a library/JAR, the projects compiles just fine.</p>
  
  <p>I then tried editing the build.gradle file to include the new lib like this:</p>
  
  <pre><code>dependencies {
  compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.foo.test-1.apk
  </code></pre>
  
  <p>Any ideas?</p>