0

MyClass.java is a part of Project B and it produces B.jar

Project A has B as a dependency. It produces A.jar

Project A builds fine(so the dependency is resolved at compile time)

My android app opens the A.jar file and tries to load MyClass using reflection(this has to be done this way, reasons are complicated to explain here).

My problem is, I get this error in runtime:

String java.lang.ClassNotFoundException: Didn't find class "com.foo.MyClass" on path: DexPathList[[zip file "A.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

PS: If I copy source code from project B into A and build A.jar the program finds it. But I want to avoid source code copying.

Is there a way to tell maven to include a class from a dependency in the resulting jar file?

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
Caner
  • 57,267
  • 35
  • 174
  • 180
  • If you're expressing B as a dependency of A via Maven, this should already work - both Jars should be on your runtime classpath. – Oliver Charlesworth Aug 09 '16 at 13:07
  • @OliverCharlesworth Yes, I'm doing so but it doesn't – Caner Aug 09 '16 at 13:07
  • You need to create an executable jar, like shown here http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven or make sure the dependencies are in the classpath at runtime. – Tunaki Aug 09 '16 at 13:09

1 Answers1

0

I managed to fix the problem using the maven shade plugin:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-shade-plugin</artifactId>
     <version>2.4.3</version>
     <executions>
         <execution>
             <phase>package</phase>
             <goals>
                 <goal>shade</goal>
             </goals>
             <configuration>
                 <createDependencyReducedPom>false</createDependencyReducedPom>
                 <artifactSet>
                   <includes>
                       <include>com.project.B</include>
                   </includes>
                 </artifactSet>
                 <filters>
                   <filter>
                       <artifact>com.project.B:B</artifact>
                       <includes>
                           <include>**/*.class</include>
                       </includes>
                   </filter>
                 </filters>
             </configuration>
         </execution>
     </executions>
 </plugin>
Caner
  • 57,267
  • 35
  • 174
  • 180