javaandroidunity-game-enginegradleaar

AndroidJavaException NoClassDefFoundError on Landroidx/core/content/ContextCompat in AAR file for Unity Plugin


We have an android bluetooth java plugin that has specific functionality for our project. The plugin otherwise works, but we're failing on the permissions request (during testing, we were manually enabling permissions).

Below is our build.gradle in the Android Studio project:

plugins {
    id 'com.android.library'
}

android {
    compileSdk 33
    namespace 'com.{ourcompanyremovedforstackoverflow}.{projectremovedforstackoverflow}'

    defaultConfig {
        minSdk 31
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    ndkVersion '25.2.9519653'
}

dependencies {
    implementation 'com.google.code.gson:gson:2.10.1'

    implementation 'androidx.core:core:1.12.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.2.0'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

android.useAndroidX=true android.enableJetifier=true

are both enabled in the gradle.properties and we are using Java 11. All other features work, though we have had to implement the gson plugin directly via implementation files('libs/gson-2.10.1.jar') as this was also failing. Doing the same for androidx.core and androidx.appcompat did not resolve the issue.

Using gradle 7.1.2 for Unity 2022.3 as per the Unity documentation.

Does anyone have any idea what may be misconfigured here?

We tried changing plugin versions, setting manual build.gradle files in Unity, and ensured proguard was not culling any dependencies.


Solution

  • I figured it out. If anyone experiences the same problem, you actually need to update the mainGradle on the Unity project too. There was nothing wrong with the plugin, but the gradle build phase in Unity simply did not know what to do.

    Updated dependencies in the Unity Project mainTemplate.gradle (be sure to tick custom MainGradleTemplate in your build settings)

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    
    def core_version = "1.10.0"
    def appcompat_version = "1.6.0"
    
    implementation 'com.google.code.gson:gson:2.10.1'
    implementation "androidx.core:core:$core_version"
    

    Additionally, be sure to tick Custom Gradle Properties Template and add the

    android.useAndroidX=true android.enableJetifier=true
    

    flags too.

    I guess the next step would be getting the plugin to automatically inject that into the build gradle for convenience.