AndEngine and Android Studio

From now on, I will be using AndEngine for making game tutorials for Android. It is really easy to use it and much more powerful and reliable then the one that I showed you before.

AndEngine Android Studio

Importing AndEngine into Android Studio project

1. The number one thing to do, is to create a new project, I will call it "AndEngineFramework", with an activity called GameActivity.

2. Now lets add AndEngine to the new project. First we need to download it from https://github.com/nicolasgramlich/AndEngine, select the branch GLES2-AnchorCenter and then there is a "Download ZIP" button on the right side.

3. Now that we have AndEngine source files lets add new module to the project.
In Android studio go to "File" -> "Project structure" and click green + sign in the left top corner.
Under "More Modules" select Android library. In the next window give the library a name AndEngine and a package name org.andengine and click next. And no activity to the module.
Now we need to add a dependency to the new module for our project. In the "Project structure", make sure that "app" is selected under "Modules" on the left side, and select "Dependencies" tab. Then click green + sign on the right, top side of the window and click "Module dependency" and select andengine.
Now you can click "OK" on the "Project structure" window.

4. Now we need to copy the source files of the AndEngine that we download before.
Under your new project, go to "AndEngineFramework\andengine\src\main\java\org\andengine" and copy into this folder content of the "AndEngine-GLES2-AnchorCenter\src\org\andengine" folder.

5. There are some libraries that are in the AndEndine project that we downloaded from github.
So we need to create folder "jniLibs" under "AndEngineFramework\andengine\src\main" and then copy into this new folder content of the "AndEngine-GLES2-AnchorCenter\libs".

6. To test if everything is working, copy the following code to GameActivity.java and rune it.
If everything is ok, you should see screen with blue background.

GameActivity.java code:
package net.gametutorial.andengineframework;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.color.Color;

public class GameActivity extends SimpleBaseGameActivity {

    private Camera camera;
    private static final int CAMERA_WIDTH = 540;
    private static final int CAMERA_HEIGHT = 960;

    @Override public EngineOptions onCreateEngineOptions() {
        camera = new Camera(0, 0, GameActivity.CAMERA_WIDTH, GameActivity.CAMERA_HEIGHT);

        RatioResolutionPolicy ratioResolutionPolicy = new RatioResolutionPolicy(
                GameActivity.CAMERA_WIDTH, GameActivity.CAMERA_HEIGHT);
        EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED,
                ratioResolutionPolicy, camera);

        return engineOptions;
    }

    @Override protected Scene onCreateScene() {
        Scene scene = new Scene();
        scene.setBackground(new Background(Color.BLUE));
        return scene;
    }

    @Override protected void onCreateResources() {
    }
}

AndEngine extensions

On the github page of the AndEngine you can see that it has a lot of extensions.
Lets add extension for physics, AndEnginePhysicsBox2DExtension.

1. Download zip file from https://github.com/nicolasgramlich/AndEnginePhysicsBox2DExtension, the GLES2-AnchorCenter branch.

2. First lets copy the source files.
Go to "AndEnginePhysicsBox2DExtension-GLES2-AnchorCenter\src\org\andengine" and copy "extension" folder into your project under "AndEngineFramework\andengine\src\main\java\org\andengine".
Then go to "AndEnginePhysicsBox2DExtension-GLES2-AnchorCenter\src" and copy "com" folder into "AndEngineFramework\andengine\src\main\java".

3. Now we need to copy libraries.
Go to "AndEnginePhysicsBox2DExtension-GLES2-AnchorCenter\libs" and copy content of each sub-folder into corresponding folder in your project under "AndEngineFramework\andengine\src\main\jniLibs".
So content of "AndEnginePhysicsBox2DExtension-GLES2-AnchorCenter\libs\armeabi", which is "libandenginephysicsbox2dextension.so", will go into "AndEngineFramework\andengine\src\main\jniLibs\armeabi". Do the same for "armeabi-v7a" and other two.

4. To test it, you will have to write some code :)

Download

Here is a zip file which contains project files:
Download AndEngineFramework project source code.