Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an Eclipse "Android Library" project and building via Maven

I am trying to include cocos2d into a preexisting app. I've done things the Eclipse way, such as setting "isLibrary" and adding the library project to the build path in Eclipse, and I have the following dependency in my app's pom.xml:

<dependency>
    <groupId>cocos2d_android</groupId>
    <artifactId>cocos2d_android</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <scope>provided</scope>
</dependency>

I thought this would've taken care of the issue, but when I build, the library seems not to be included. I know this because when I start an Activity, SimpleGame, referencing one of the classes in the cocos2d source, the Activity dies and I get this stacktrace in DDMS:

E/AndroidRuntime(10621): FATAL EXCEPTION: main E/AndroidRuntime(10621): java.lang.NoClassDefFoundError: org.cocos2d.opengl.CCGLSurfaceView E/AndroidRuntime(10621): at com.xyz.game.SimpleGame.onCreate(SimpleGame.java:22) ...

I'm looking for two things:

1) a reliable way to see if a certain class/jar/whatever was packaged up into my apk, as the steps to get to this point in my app are long and complicated right now

2) Something in the Manifest or pom.xml for either the main app or the library project seems to be missing - something needed to signal to Maven to pick up this other project - what is it?

I'm using Maven 3.0.4 and 3.0.0-alpha-13 of the plugin, building for level 8 and up.

like image 682
Bry M. Avatar asked Mar 29 '12 18:03

Bry M.


1 Answers

1) a reliable way to see if a certain class/jar/whatever was packaged up into my apk, as the steps to get to this point in my app are long and complicated right now

Build project from command-line (i.e. mvn clean install), maven will output verbose logs during every single phase/goal of build, in the dex goal, you can see something like this:

[INFO] --- android-maven-plugin:3.1.1:dex (default-dex) @ myproject ---
[INFO] C:\Program Files\Java\jdk1.6.0_21\jre\bin\java [-Xmx1024M, -jar, C:\Progr am Files\Android\android-sdk-r16\platform-tools\lib\dx.jar, --dex, --output=C:\workspace\myproject\target\classes.dex, C:\workspace\myproject\target\classes, C:\maven\repository\cocos2d_android\cocos2d_android\1.0.0-SNAPSHOT\cocos2d_android-1.0.0-SNAPSHOT.apklib, C:\maven\repository\com\google\code\gson\gson\1.7.1\gson-1.7.1.jar, ... ...]

The Android Library Project is actually dex-ed as cocos2d_android-1.0.0-SNAPSHOT.apklib with some other regular jar library archives.


2) Something in the Manifest or pom.xml for either the main app or the library project seems to be missing - something needed to signal to Maven to pick up this other project - what is it?

It is ApkLib, which is simply a zip archive of the Android Library Project (src/, res/, AndroidManifest.xml and etc.). We usually create/implement our own Android Library Project with the dependant Android Project as multi-module maven project, however, this is not necessary in case if you need reference Android Library Project written by other developers. Thanks to the developer, android-maven-plugin has already supported non-Maven Android Library Projects, check out Compatible with non-Maven Android Library Projects:

The generated .apklib file will have the layout of a standard Android/Eclipse library project. This means that regardless of your Maven layout, the layout inside the .apklib file will be that source code is in "src/" instead of "src/main/java/", but still interpreted correctly when used in an Android/Maven application project. This is to be compatible with non-Maven developers' library projects, which is important to grow the Android developer community.

Use other non-Maven developers' libraries

It also means we can take any external Android library project zip file (from non-Maven developers) and do mvn install:install-file ... on it and simply start using it as a dependency.

Share your .apklib with non-Maven developers

To share your .apklib file with a non-Maven developer, they will probably feel more comfortable if you rename it to .zip. They can then simply unpack it in a directory and use it from there.

So the workaround is:

  1. Pack your Android Library Project into zip archive properly, then rename it to something.apklib.

  2. Install something.apklib into your Maven local repository by using mvn install:install-file.

  3. In any Android Project that requires Library dependency, simply using:

    <dependency>
      <groupId>cocos2d_android</groupId>
      <artifactId>cocos2d_android</artifactId>
      <version>1.0.0-SNAPSHOT</version>
      <type>apklib</type>
    </dependency>
    

Check out Samples on android-maven-plugin website to see how to use apklib properly.

like image 197
yorkw Avatar answered Sep 23 '22 01:09

yorkw