Math Equations

Friday, February 28, 2014

How to setup AndEngine in Android Studio


This guide assumes that you have Android Studio, git, a terminal and rsync installed.
  1. Create and open the project in which AndEngine will be used.
  2. Create a module for AndEngine.
    File → Project Structure → Modules → + → Android Library → [Module name: AndEngine, Package name: org.andengine, Create activity: no] → Finish
  3. Tell the compiler to search the AndEngine module by adding a dependency.
    File → Project Structure → Modules → <the module that will use AndEngine> → Dependencies → + → Module dependency → :AndEngine
  4. Download AndEngine by running the following in a terminal:
    $ cd <project folder>
    $ git clone -b <GLES2 or GLES2-AnchorCenter> https://github.com/nicolasgramlich/AndEngine.git AndEngine.github
    $ rsync -a AndEngine.github/* AndEngine # This merges AndEngine.github and AndEngine
    $ rm -rf AndEngine.github
  5. Make a minor change to the file tree of AndEngine to make it work with Android Studio.
    $ cd <project folder>/AndEngine/src/main
    $ rm -r java/org
    $ mv org java
  6. Ensure that Android Studio notices the changes you did. Do this by right clicking on the AndEngine module in the Project tool window and choosing Synchronize 'AndEngine'.
You should now be able to use the core AndEngine package.

Optional: adding an extension

Adding an extension is much the same procedure, so I'll only list the additional things you need to do.
  • Before 2: To find the package name of the extension, browse the extension repo on github and look in AndroidManifest.xml.
  • After 3: Make the extension dependent on AndEngine.
  • At 5: This step may vary slightly. You may not need to do anything.

Fixing java.lang.UnsatisfiedLinkError

When I tried to use the physics extension, everything compiled fine, but when I ran my app, it crashed, logging: java.lang.UnsatisfiedLinkError: Couldn't load andenginephysicsbox2dextension from loader dalvik.system.PathClassLoader[...]: findLibrary returned null

That is, the .so files are not in the apk. Workaround:
  1. Manually copy <extension-module>/libs/* to a new folder <main-module>/lib
  2. Compress lib/ to lib.zip
  3. Rename lib.zip to lib.jar
  4. Add compile files('lib.jar') as a dependency in <main-module>/build.gradle
However, it must be noted that one has to update lib.jar manually in the future!

Happy coding!



15 comments:

  1. I can't solve java.lang.UnsatisfiedLinkError );

    I copied armeabi, armeabi-v7a, mips and x86 to my new folder lib,
    and I compressed and changed to lib.jar, and I added: compile fileTree(include: ['*.jar']) in my build.gradle dependency.

    What can be the problem?

    Thank you too much for the guide.

    ReplyDelete
    Replies
    1. Could you send me a copy of your project?

      Delete
    2. Here it is https://github.com/AlexBruch/DisIsEspain4

      Thanks!

      Delete
    3. It works!

      I'm so sorry I put the .jar in the wrong place :/// because my bad english ;)

      A thousands thanks!!!

      Delete
    4. You're welcome. Glad to hear that the method still works!

      Delete
  2. Do you know how to setup in Android Studio 1.2.2? There is no org folder.

    ReplyDelete
    Replies
    1. That happens to me too. Try ../org instead and tell me your results.

      Delete
    2. rm: ../org: No such file or directory

      Delete
    3. No, I mean mv. The rm step reems to be done already. First run "cd <project folder>/AndEngine/src/main" and then "mv ../org java".

      Delete
    4. "mv: rename ../org to java/org: No such file or directory", thanks a lot, Joel, but I have spent too much time on this problem and I am using cocos2d now, after all , thanks from the back of the earth :)

      Delete
    5. Ok, good luck with your project!

      Delete
  3. Hello,

    How to make changes if I am not using git but directly download zip and add to project ??

    ReplyDelete
    Replies
    1. Hello, in the zip file there should be a single directory AndEngine-GLES2, which corresponds to AndEngine.github. Extract it and keep following the steps from there, starting with the rsync command. Make sure you're in the right directory and everything.

      Delete