title: NoClassDefFoundError for dependent library projects in Android app project (using maven as build tool)
body: <p>I developed two Android library projects: <strong>Project A (MyBaseA)</strong> &amp; <strong>Project B(MyBaseB)</strong>. </p>
  
  <p>I am using maven as build tool, <strong>MyBaseA</strong> is a <code>&lt;dependency&gt;</code> of <strong>MyBaseB</strong>.  </p>
  
  <p>I also have an Android app project which uses the built <strong>MyBaseB.jar</strong> as library, this jar is added to the build path of the Android app project.</p>
  
  <p>For <strong>library project A</strong>, the maven coordinate definition in pom.xml is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
  
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.my.base.a&lt;/groupId&gt;
      &lt;artifactId&gt;MyBaseA&lt;/artifactId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;packaging&gt;jar&lt;/packaging&gt;
      &lt;name&gt;MyBaseA&lt;/name&gt;
      ...
  </code></pre>
  
  <p>For <strong>library project B</strong> which is dependent on above <strong>library project A</strong> project, the <strong>pom.xml</strong> is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
  
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.my.base.b&lt;/groupId&gt;
      &lt;artifactId&gt;MyBaseB&lt;/artifactId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;packaging&gt;apklib&lt;/packaging&gt;
      &lt;name&gt;MyBaseB&lt;/name&gt;
  
      &lt;dependencies&gt;
        &lt;!--include library project A as dependency--&gt;
        &lt;dependency&gt;
              &lt;groupId&gt;com.my.base.a&lt;/groupId&gt;
              &lt;artifactId&gt;MyBaseA&lt;/artifactId&gt;
              &lt;version&gt;1.0&lt;/version&gt;
         &lt;/dependency&gt;
        ...
     &lt;/dependencies&gt;
  </code></pre>
  
  <p>I <code>mvn clean install</code> library project <strong>A</strong> &amp; <strong>B</strong> which are all successful. </p>
  
  <p>Under <code>target/</code> directory of <strong>project B</strong>, there are <strong>MyBaseB.ap_</strong>, <strong>MyBaseB.apklib</strong> &amp; <strong>MyBaseB.jar</strong> files.</p>
  
  <p>At this point, I created a new Android app project, in which I included the above <strong>MyBaseB.jar</strong> to the build path of my new Android app project, and used java classes defined in <strong>MyBaseB</strong> project, which in turns call functions defined in <strong>MyBaseA</strong> project.</p>
  
  <p>But when I run my new Android app project, I got exception:</p>
  
  <p><code>java.lang.NoClassDefFoundError: com.my.base.a.ProjectAClass</code> . </p>
  
  <p>My android app can not find the java class defined in <strong>project A</strong>. Then I checked the content of <strong>MyBaseB.jar</strong>, all classes of <strong>library project A</strong> are <strong>not</strong> included in <strong>MyBaseB.jar</strong>.  Why? I think the <strong>MyBaseB.jar</strong> should include <strong>project A</strong> classes, because the pom has included <strong>project A</strong> as a <code>&lt;dependency&gt;</code>. Why this error happen?</p>
  
  <p><strong>==================== Update =====================</strong></p>
  
  <p>After I changed <strong>library project A</strong> 's <code>&lt;packaging&gt;</code> section of pom from <strong>jar</strong> to <strong>apklib</strong> , and added the <code>&lt;type&gt;apklib&lt;/type&gt;</code> in the dependency of <strong>project B (MyBaseB)</strong>:</p>
  
  <pre><code>  &lt;dependency&gt;
          &lt;groupId&gt;com.my.base.a&lt;/groupId&gt;
          &lt;artifactId&gt;MyBaseA&lt;/artifactId&gt;
          &lt;version&gt;1.0&lt;/version&gt;
          &lt;type&gt;apklib&lt;/type&gt;
     &lt;/dependency&gt;
  </code></pre>
  
  <p>now the <strong>MyBaseB.jar</strong>  contains classes of <strong>project A</strong>. The above problem solved ( I checked the content of <strong>MyBaseB.jar</strong>, now, it contains all classes of <strong>project A</strong>, previously, it didn't.</p>
  
  <p>But the new problem is that, when I run my Android app, the <code>java.lang.NoClassDefFoundError</code> occurs whenever a class of <strong>project A</strong> is instantiated. That's when app reaches :</p>
  
  <pre><code>ProjectAClass projAclass = new ProjectAClass();
  </code></pre>
  
  <p>I got <code>java.lang.NoClassDefFoundError</code>. Why? Why it complains even now the <strong>MyBaseB.jar</strong> contains the ProjectAClass.class.</p>