After spending some times looking for how to use andEngine with android studio, I decided finally to write an article about it.

Why ? I have to say that the articles on the net are pretty useful but… none of them are 100% complete and/or don’t cover all errors what you can have in following the tutorial.

I tried to write this tutorial as explicit as possible and cover all cases of error what you could get (and I got..). I hope it will help you !

For information, I am using Android studio 1.1.0 for this tutorial.

 

Ok now … Let’s go !

 

Step 1 : Get Andengine

Use git in order to clone :

  • AndEngine  :  
    git clone https://github.com/nicolasgramlich/AndEngine.git andEngine
  • AndEnginePhysicsBox2DExtension : 
    git clone https://github.com/nicolasgramlich/AndEnginePhysicsBox2DExtension.git andEnginePhysicsBox2DExtension

 

Screen Shot 2015-03-22 at 16.43.42

 

Checkout the branch  “origin/GLES2-AnchorCenter” on both projects.

Go into the andEngine directory and checkout the branch :

git checkout -b GLES2-AnchorCenter origin/GLES2-AnchorCenter

Go into the 2nd directory and execute  the same command.

Screen Shot 2015-03-22 at 16.47.17

 

Step 2 : import the modules into your Android Studio project

 

Go back to Android studio and import only the module “andEnginePhysicsBox2DExtension”.

The button “Import module” is located under “File” nav bar.

The source directory required is the path to the andEnginePhysicsBox2DExtension.

Screen Shot 2015-03-22 at 16.49.01

Once selected and finished to be imported, It will automatically add andEnginePhysicsBox2DExtension and andEngine modules into your project.

So you don’t need to do this action again with the andEngine directory.

If you add both of them, you will have some issues during the compilation : duplicated files.

 

Once done, Gradle will automatically build your project.

You might come across this error during the build :

Screen Shot 2015-03-22 at 16.56.11

As written, your NDK is not configured…. not yet !

 

Step 3: NDK Configuration

 

One line of code :  Add the path of your NDK in the  local.properties file, located to the root directory of your project.

Screen Shot 2015-03-22 at 16.58.41

 

 

Step 4: Update Build.gradle file 

Gradle needs to know about your JNI otherwise it could lead to some errors.

To avoid having any issues, add these lines in both build.grade files of your imported modules :

sourceSets {
        main {
             jni.srcDirs = []
             }
           }

 

Screen Shot 2015-03-22 at 19.42.43

If you don’t add  thiese lines in both build.gradle, you will encounter a NDK error:

 

Screen Shot 2015-03-22 at 19.48.16

 

Please note that there is no AndroidId in both build.gradles files. This can also lead to an error :

 

Screen Shot 2015-03-22 at 20.19.56

So make sure there is no androidId in both build.gradle files !

 

 

Step 5 : Build andEnginePhysicsBox2DExtension using the NDK

First, open the Application.mk file of your andEnginePhysicsBox2DExtension located in the src/main/jni folder.

Add in the end of the file the following line : Screen Shot 2015-03-22 at 17.13.29

APP_PLATFORM:=android-19

 

Ok, let’s compile this project !

 

Go to the directory src/jni directory of your andEnginePhysicsBox2DExtension project and run the command :

 

PATH_TO_YOUR_NDK/ndk-build

 

/!\ Be Sure to run the ndk-build in andEnginePhysicsBox2DExtension  and not app/andEnginePhysicsBox2DExtension. Otherwise it will lead to some issues.

 

BOOM ! The project is compiling and your get your lib (.so) under the directory libs of your modules.

Screen Shot 2015-03-22 at 17.17.05

 

 

Now, let’s insert the libraries into your main app !

 

Step 6 : Add your *.so files into your app 

 

2 solutions : 

 

Solution 1 : Creation of a JniLibs folder

Create a folder called “jniLibs” into your app and the folders containing your *.so inside.

It should look like this.

|--app:
|--|--src:
|--|--|--main
|--|--|--|--jniLibs
|--|--|--|--|--armeabi
|--|--|--|--|--|—andEnginePhysicsBox2DExtension.so
|--|--|--|--|--X86
|--|--|--|--|--|--andEnginePhysicsBox2DExtension.so

….

Screen Shot 2015-03-23 at 17.36.29

 

I haven’t add any tasks in my build.gradle file to make it work.  Gradle understand by itself that it needs to include them into your project.

You don’t need to configuration the build.gradle file of your application at all.

 

Screen Shot 2015-03-23 at 17.42.21

 

Solution 2 : Modification of  the build.gradle file

 

If you don’t want to create a new folder and keep your *.so files into the libs folder, it is possible !

In that case, just add your *.so files into the libs folder (please respect the same architecture as the solution 1 : libs/armeabi/.so for instance) and modify the build.gradle file of your app to add the source directory of the jniLibs.

 

sourceSets {
        main {
             jni.srcDirs = ["libs"]
             }
           }

 

Screen Shot 2015-03-23 at 18.09.41

 

It is done !

Pretty easy, is’nt it ?

Now you can sync your project and run it on your phone ! 

 

 

 

 

PS : If you get this runtime error  at the some point it means that your library is not included correctly.

Screen Shot 2015-03-22 at 20.41.38

 

 

 

If you have any issues or suggestions, do not hesitate to comment this article !

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>