Use Android Studio to build ANE
Hi Devs, here I am not going to talk about how ANEs are built because there are a lot of good tutorials with details in this regard like ones from Nathan Weber which you can find on adobe http://www.adobe.com/devnet/air/articles/building-ane-ios-android-pt1.html we actually started from there few years back yet, what I’m going to talk about is how to use Android Studio instead of eclipse.
as you know, Google has recently dropped its support for eclipse and you won’t be able to install Android tools on a fresh eclipse installation anymore although you can still build for Android if you have your eclipse already setup but it’s time to move to Android Studio. it’s not as scary as you might think. What I’ll be talking about below is after messing around with Android Studio V1.2.2 for one day. there might be better approaches but this is what I found out and I’m very happy with it because I could use it to build my java side of the extension with no problem. The only bad thing I noticed about Android Studio is that it’s a lot slower than eclipse which I really cannot put the blame on this IDE because I’m an old dog and I’m still using my Windows XP which installed on 2002! I really should move to a newer computer some day soon 🙂
Basics of an Android IDE required for building an ANE
To build an Adobe air ANE for Android, you will need to use FlashRuntimeExtensions.jar found in AdobeAir SDK and after building your logic based on this bridge, you will need to output your code in a .jar file which will later use it in your ant script to build the final .ane. (we use .bat files to put the final .ane together but the logic is the same if you are using ant) when starting with Android Studio, you may have heard that it does not export .jar files anymore! but the good thing is that it does export something called .aar which stands for Android Archive.
What is .aar?
.aar is a new project archive that Android uses for making libraries. its usage is exactly like a .jar file with a very small difference that is you can package resources along with your java classes. you may find this feature very helpful when trying to build extensions from third-party projects. anyway, you can think of an .aar file as a zip file which you can open with any zip extractor software and if you look inside, you will find a file named “classes.jar” this file is exactly the jar file you used to generate in eclipse.
What you need to accomplish is to create a new Android Studio project and embed FlashRuntimeExtensions.jar from adobe into it, write your extension logic and then export the .aar file. then go inside the .aar and grab the “classes.jar” file and use it to build your ANE like old times.
Getting to know Android studio?
after downloading and installing Android Studio, I suggest you to read some tutorials about how to run a hello-world project in Android. This will help you feel a bit more at home with this IDE. I tried part 1 of this tutorial written by Darryl Bayliss and that was good enough for me to know the IDE in general.
Next, I started digging in to find how to make an air extension with it. here we go.
When you run Android Studio, you will find a welcome screen and you need to create a new android studio project. every project you create can have one or more modules. every module has a job. for example, when you create a new project, the first default module named “app” will be created for you automatically. This module will help you write java code and build it to output .apk files. you can also create new modules in your project. a library module for instance and the job of a library module would be to generate .aar file from your project. (I found this procedure similar to how xcode works to generate library .a files and I loved it to see it in Android Studio too)
Saying that, I decided not to create two separated modules in my project! I will show you how to create an application module first so you can test your extension logic as an .apk while developing, and then I will show you how to apply a little hack to this module to make it export the ANE required .aar file! using this approach, you will have only one module to deal with.
Creating a new project in Android Studio is very similar to how you used to do it in eclipse. while looking at the following screenshots, create your own project and we’ll continue from there… just consider the following setup. it’s not necessary, but I think it’s a good idea… 1) use API 10 as your minimum sdk version. in your project, depending on what kind of extension you are building, you may be required to change this but it’s a good idea to start with this min at the beginning. 2) Create a “Blank Activity” option. this will help you create the minimum setup you would need to run your extension as an .apk while developing the Android side.
Step 2: Add FlashRuntimeExtensions.jar to Android Studio
Right after the first step, you can start building your extension logic and do your tests in a runable .apk like any Android developer would do. but after you are finished with your java coding, it’s time to add the ANE bridge to your project. to do that, just copy the FlashRuntimeExtensions.jar file from your AdobeAir SDK and paste it into the lib folder of your Android Studio project… seems an easy and straight work, right? but make sure you are checking the below screenshots to know where to find the “libs” folder!
After adding the .jar file to your module, the Gradle build will run automatically and as you can see in the last screenshot, if everything has gone smoothly, the Gradle build log window will show no error. Alright, now that you have successfully added the FLashRuntimeExtensions.jar to your project, switch back to the “Android” view and start writing the extension bridge classes for your extension.
Step 3: Tell Gradle to export to .aar
from your Android view window double click on build.gradle (Module: app) to have it open on the right side as seen in the below screenshot.
The first thing to notice is that at the very bottom of your build.gradle file, this line has been added:
gradle script determines the rules for building your project. so if you are adding a .jar file, it will be shown here also. now, what you need to do is to tell Gradle to build your project as an .aar. change the current code to following and it’s done.
//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
compile fileTree(dir: 'libs', include: ['*.jar'])
As you see, we have commented out //applicationId “com.doitflash.myFirstANE” and have replaced ‘com.android.application’ to ‘com.android.library’
Now, from the build menu, hit Make Project or Rebuild Project and wait until Android Studio finishes its job. when it’s done, go to the folder where you saved your project and find this file: app\build\outputs\aar\app-debug.aar and simply open it with any zip software and find “classes.jar” in it. use this jar file to build your ANE with it.
I hope you have found this little note helpful.