• Oct


    安卓 ANE 制作资料备忘录(三):使用 Android Studio 制作 ANE

    • Java
    • 发布:古树悬叶
    • 引用:0
    • 浏览:

    安卓 ANE 制作资料备忘录(一):安卓开发环境搭建》贴子中写到了 ADT-Bundle,但现在安卓官方已经去掉了它的支持和链接,所以必须使用迅雷才能下载。

    安卓官方现在只支持 Android Studio 并提供了下载,但这个东西并不友好,甚至可以说是极不友好。一篇如何使用 Android Studio 制作 ANE 的教程说明:《How to build ANE in Android Studio》(可能需要 VPN 链接才能查看完整的版本)。



    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.

    Step 1: you need a new project

    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! 

    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:

    compile files('libs/FlashRuntimeExtensions.jar')

    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'
    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1"
        defaultConfig {
            //applicationId "com.doitflash.myFirstANE"
            minSdkVersion 10
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.1.1'
        compile files('libs/FlashRuntimeExtensions.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.

    MyFlashLab Team.


    • Tags: ANE  
    • 2015 / 10 / 5