5

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.

Bry M.
  • 135
  • 2
  • 13

2 Answers2

4

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.

Community
  • 1
  • 1
yorkw
  • 40,926
  • 10
  • 117
  • 130
  • Thanks yorkw! Though is there a way for Maven to pull in and zip up the project? My copy of cocos2d is a read only clone from github and I'd love the convenience of not repeating this process each time a change comes out of github... – Bry M. Mar 30 '12 at 18:52
  • I am not aware if there are any maven plugins that can do pull, zip and install automatically from github. How often are you going to upgrade your app? From a productive perspective, it is unlikely need for one to maintain/upgrade required apps' dependencies at a regular daily or weekly base. The manual workaround is the only way android-maven-plugin provided and I think it is acceptable for productive maintenance in most conditions. – yorkw Mar 30 '12 at 21:17
  • quick note: the "Samples" hyperlink above is missing the final "s" (should be http://code.google.com/p/maven-android-plugin/wiki/Samples). SO wouldn't let me make a 1-character edit. – Andy Dennie May 02 '12 at 12:56
1

Scope shouldn't be provided. Provided means:

provided - this is much like compile, but indicates you expect the JDK or a container to provide it at runtime. It is only available on the compilation and test classpath, and is not transitive.

Use compile instead.

Extra tip: Since compile is the default, you can remove the <scope>provided</scope> line.

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • Funny you should mention that - using the `compile` default, I get a duplicate entry error when trying to build the .apk. Obviously I have an extra dependency specification somewhere, but I'm not sure where - I already tried removing the project from the build path in eclipse, not sure what else to try. – Bry M. Mar 29 '12 at 20:35
  • @BryM. this could be caused by a dependency in the cocos2d_android that already exists in the Android library. You can probably find the duplicate class in your classpath and use Maven dependency tag to prune the redundant dependency from cocos2d_android. – Ricardo Gladwell Mar 30 '12 at 09:38