How to Include Unity's classes.jar in Android Studio without it being in the AAR

I’m trying to build a simple AAR plugin which needs to use Unity’s “UnitySendMessage” function to communicate with Unity.

I’ve tried the standard method, not putting the .jar in the libs folder and marking it as provided instead of compile in the gradle build file. That doesn’t work. It includes it in the AAR even when it’s marked as provided. I’ve read that this is just something in Gradle that’s broken.

So I tried the method below, outlined in another thread by “Freezy”.

This doesn’t work either. It still ends up with classes.jar being bundled into the AAR.

Surely there’s some way to do this? Manually removing the file from the AAR every time is ridiculous.

Marking it as “provided” should do the trick. not sure what’s wrong with your project… maybe there’s something else that you did not mention or aware of.

A google search returns a number of results indicating that “provided” does not work in Android Studio as Gradle does not properly implement it.

Including this thread on these very forums where the other solution I mentioned was suggested, specifically because “provided” does not work.

Is the reason for not including classes.jar because it interferes with other plugins?

I think it’s more that Unity already includes the contents of classes.jar when it builds anyway so even one plugin including it again means you have two copies and Unity’s build tools cannot resolve that. But possibly I’m wrong and there’s another plugin including it.

I’m not sure about that because most of the tutorials I have been looking at for building plugins have you build the plugin with classes.jar. I’m having a lot of trouble making a plugin because as soon as I include classes.jar and add the plugin to my unity project I can no longer build the unity project. I have facebook and firebase plugins so I don’t know if they’re interfering. Having a lot of trouble learning plugins…

We have our own .aar project that gets built, and it certainly doesn’t include any its dependencies.

This is how the build.gradle file is defined:

We just throw the classes.jar file under libs and it works. Let me know if this does not work for you.

1 Like

Thanks but I’m afraid this definitely does not work for me.

I removed everything else from the dependencies section of the build.gradle file as you suggested, cleaned and built. I open the resulting AAR file with a zip utility and sure enough classes.jar is still sitting inside the lib folder within the root of the plugin.

Does it help to show the build.gradle file?

apply plugin: 'com.android.library'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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

dependencies {
    provided fileTree(include: ['*.jar'], dir: 'libs')
}

EDIT: Fixed a typo.

1 Like

Unity’s library is named classes.jar or unity-classes.jar ?

I think that .aar files will always contain the compiled code as a classes.jar file (inside the zip). This is not to be mistaken with Unity’s classes.jar of course :slight_smile:

You can peek into the jar as well just to verify: either unzip it, or use a decompiler like jd-gui (that’s what i use).

1 Like

Yes, but the compiled code from the library is in the root of the AAR. This is in a folder called libs. And yes, I have opened the jar to confirm that it’s the unity classes.

I was having the same issue until I came across the link below. What ended up being the fix was adding the jar as a file dependency under the Project Structure > Dependency window.

2 Likes

Instead of putting Unity’s classes.jar inside the ‘‘libs’’ folder, I put it inside the ‘‘build’’ folder. If you use implementation it will still get the jar to your libs folder in the aar build, however if you replace implementation with ‘‘provided’’ it won’t. Easiest fix I could find.

2 Likes

For me it worket when I put the Unity’s classes.jar into the build folder in Android Studio instead of the libs-folder. Then I right-clicked the library-root (when android is selected) in the small upper left window and clicked open module settings. I added classes.jar as a JAR-dependency to the library project from the build folder root. Also set it as compile only there. Now it no longer copies the unitys classes.jar into the generated aar-files lib-folder (now there was none for me) and unitys build just works onto the device with the plugin. Happy Happy – Joy Joy.

After much faffing around, adding the classes.jar to the libs folder and adding this line to our dependencies worked for us:

compileOnly fileTree(dir: ‘libs’, include: [‘*.jar’])

3 Likes

This right here! Great answer. If you happen to be using build.gradle.kts (whatever that is), the syntax looks like this:

compileOnly(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
1 Like