title: How to build an android library with Android Studio and gradle?
body: <p>I am trying to migrate a project from Eclipse but nothing I have tried is working. In Eclipse I have 3 projects (2 android app projects and 1 android library project). The 2 app projects depend on the library project. When I do the gradle export I get 3 projects that don't work. I am open to restructuring the project but haven't found any documentation on how this should be done.</p>
  
  <p>Is there a way to make the 3 projects that Eclipse exports work together? Am I better off restructuring things and if so is documentation for how this should be done?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have uploaded the entire project to GitHub <a href="https://github.com/respectTheCode/android-studio-library-example">https://github.com/respectTheCode/android-studio-library-example</a></p>
  
  <p><strong>Update 1</strong></p>
  
  <p>Based the suggestions from Padma Kumar this is what I have tried.</p>
  
  <ol>
  <li>Create a new Project called <code>MyApp</code></li>
  <li>Click <code>File &gt; New Module</code>, choose <code>Android Library</code> and name it <code>MyLib</code></li>
  <li>Click <code>Build &gt; Make Project</code></li>
  </ol>
  
  <p>The build fails with this error</p>
  
  <pre><code>Module "MyLib" was fully rebuilt due to project configuration/dependencies changes
  Compilation completed with 1 error and 0 warnings in 19 sec
  1 error
  0 warnings
  /.../MyApp/MyLib/build/bundles/debug/AndroidManifest.xml
  Gradle: &lt;manifest&gt; does not have package attribute.
  </code></pre>
  
  <p>I then added a <code>package</code> attribute to the manifest making it</p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mylib" &gt;
  </code></pre>
  
  <p>After building I get this error</p>
  
  <pre><code>Module "MyApp" was fully rebuilt due to project configuration/dependencies changes
  Compilation completed with 2 errors and 0 warnings in 13 sec
  2 errors
  0 warnings
  /.../MyApp/MyLib/src/main/java/com/example/mylib/MainActivity.java
  Gradle: package R does not exist
  Gradle: package R does not exist
  </code></pre>
  
  <p>Adding dependency doesn't seem to have any impact on the error. Continuing from above</p>
  
  <ol>
  <li>Click <code>File &gt; Project Structure &gt; Modules &gt; MyApp-MyApp</code></li>
  <li>Switch to <code>Dependencies</code> Tab</li>
  <li>Click <code>+ &gt; Module Dependency</code> and pick <code>MyLib</code></li>
  <li>Click <code>Apply</code> and <code>OK</code></li>
  <li>Click <code>Build &gt; Make Project</code></li>
  </ol>
  
  <p><strong>Update 2</strong></p>
  
  <p>Based the suggestions from Ethan this is where we get</p>
  
  <p>The 2 sub project <code>build.gradle</code> seem to have all of the correct parts and the only difference is the plugin line bellow is the <code>MyApp/build.gradle</code>.</p>
  
  <pre><code>buildscript {
      repositories {
          maven { url 'http://repo1.maven.org/maven2' }
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:0.4'
      }
  }
  apply plugin: 'android'
  
  dependencies {
      compile files('libs/android-support-v4.jar')
  }
  
  android {
      compileSdkVersion 17
      buildToolsVersion "17.0.0"
  
      defaultConfig {
          minSdkVersion 7
          targetSdkVersion 16
      }
  }
  </code></pre>
  
  <p>The root project <code>build.gradle</code> was empty so I added the two projects like this</p>
  
  <pre><code>dependencies {
      compile project(":MyLib")
      compile project(":MyApp")
  }
  </code></pre>
  
  <p>I now get this error when building</p>
  
  <pre><code>Gradle:
  FAILURE: Build failed with an exception.
  * Where:
  Build file '/Users/kevin/GitHub/AppPress/MyApp/build.gradle' line: 2
  * What went wrong:
  A problem occurred evaluating root project 'MyApp'.
  &gt; Could not find method compile() for arguments [project ':MyLib'] on root project 'MyApp'.
  * Try:
  Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  </code></pre>
  
  <p><strong>Update 3</strong></p>
  
  <p>Big thanks Ethan for solving this.</p>
  
  <ol>
  <li>Add <code>compile project(':SubProjects:MyLib')</code> to the <code>MyLib/build.gradle</code></li>
  <li>Remove <code>compile files('libs/android-support-v4.jar')</code> from the <code>MyLib/build.gradle</code></li>
  <li>Close project and Import the root project from gradle</li>
  </ol>
  
  <p><strong>Update 4</strong></p>
  
  <p>As of 0.1.2 you can now include <code>compile "com.android.support:support-v4:13.0.0"</code> instead of <code>compile files('libs/android-support-v4.jar')</code>. Since it is coming from mavin now you can include this in multiple projects without problems.</p>
  
  <pre><code>buildscript {
      repositories {
          mavenCentral()
      }
  
      dependencies {
          classpath 'com.android.tools.build:gradle:0.4.2'
      }
  }
  
  apply plugin: 'android'
  
  dependencies {
      compile "com.android.support:support-v4:13.0.0"
  
      compile project(':SubProjects:MyLib')
  }
  </code></pre>
  
  <p><strong>Update 5</strong></p>
  
  <p>As of 0.1.3 there is now a "Sync Project" button in the toolbar. You can click that instead of reimporting your project after making changes to <code>.gradle</code> files.</p>