Untiy android plugin class not found exception

Viewed 342

I'm writing an android plugin for unity to be able to check if notifications are enabled for the game. I have one java class with a method for checking if notifications are enabled. When i build the plugin and then the .apk with unity everything works fine. But after installation, when calling the mehthod i get the following exception:

java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/app/NotificationManagerCompat;

The Java class

package com.example.plugin;

import android.app.Activity;
import androidx.core.app.NotificationManagerCompat;

public class NotificationPlugin {

    public static boolean areNotificationEnabled(Activity unityActivity) {
        return NotificationManagerCompat.from(unityActivity).areNotificationsEnabled();
    }
}

buld.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"


    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }

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

}

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

    implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

gradle.properties

org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true

Haven't found a solution anywhere else yet. Thank in advance. It's probably a super simple fix that i miss.

Update

  • Custom gradle.properties in unity set with android.enableJetifier and android.useAndroidX set to true
  • Jetifier was enabled in unity
  • Androidx.Core libary was added to the dependencies of the gradle.build file of the plugin

None of the above solved the issue

Solution

As Hamid Yusifli suggested in his answer a custom gradle build template needs to be enabled (Project Settings>Player>Publishing Settings>Custom Main Gralde Template) and the dependencies for the libary need to be added (implementation 'androidx.core:core:1.5.0' in my case). This solved the issue.

1 Answers

So, seems like in your case unity ignores your library gradle dependencies, there are many different reasons why this could happen. To force unity to include missing dependencies you must provide a custom Gradle build template and add your dependencies in that file.

Related