Thursday, February 19, 2015

Export as JAR for Unity3d using Android Studio

Standard
Hello All:

In development of games, we often need to call native code. In case of Android games we need to make call to AndroidJNI. In this post I will share the script which can be used to create JAR files using Android Studio.

Google is pushing Android Studio as main development IDE for all android application. Android Studio provides better interface for Android Application development. In my earlier post, I explained how to create a plugin using Eclipse. Now I will share how the same thing can be done in Android studio.

Let us start.

First create an Android Studio project. Android Studio will generate build.gradle in app folder.
Open this file and replace the code with the code given below. I have given comments to tell what is being done.


/// This is to change the output from apk to jar
apply plugin: 'com.android.library'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"
    sourceSets {
        main {
            // Here is the path to your source code
            java {
                srcDir 'src/main/java'
            }
        }
    }
    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 18
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile('com.google.android.gms:play-services-base:6.5.87')

    compile('com.google.android.gms:play-services-ads:6.5.87')
            {
                exclude module: 'support-v4'
            }
    compile('com.google.code.gson:gson:2.3')

  }

task deleteOldJar(type: Delete) {
    delete 'build/libs/AndroidPlugin.jar'
}
task exportJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('release/')
    include('classes.jar')
    ///Give whatever name you want to give
    rename('classes.jar', 'AndroidPlugin.jar')
}

exportJar.dependsOn(deleteOldJar, build)

Now click on "Gradle", this will open the list of all the available tasks.


 Double click on exportJar, wait for some time and the Jar will be generated in "build/intermediates/bundles/release/" (if there is no compilation error :D).

Take this jar file and put in the Plugins/Android folder of Unity3d to get started.

Hope this helps.
Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.

8 comments :

  1. Hi and thanks for this! nobody else seems to be dealing with unity plugins within android studio. Can you clarify some of the functionality of the exportJar for me. Is the include('classes.jar') call there to add the Unity classes.jar or is this just a default? I guess default due to the following: rename('classes.jar', 'AndroidPlugin.jar') just a bit confused :)

    ReplyDelete
    Replies
    1. Thanks for reading the post. Yes you guessed it correct. Here classes.jar refers to the classes of the current project.

      Delete
  2. Dear Can you explain me why
    apply plugin: 'com.android.library'
    is written, because I am getting error here

    buildscript {
    repositories {
    jcenter()
    }
    dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
    }

    allprojects {
    repositories {
    jcenter()
    }
    }

    ReplyDelete
  3. how can this method be used to make plugins for windows speech for android in unity?

    ReplyDelete