Search Unity

Unity Android Plugin Error: java.lang.NoClassDefFoundError: Failed resolution of JAVA Class

Discussion in 'Android' started by devotionsolutions, Jul 31, 2015.

  1. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    Hello everyone, I'm trying to build an Android plugin following the instructions here: http://www.thegamecontriver.com/2015/04/android-plugin-unity-android-studio.html
    I was able to compile and integrate my plugin in Unity, and everything works as expected. But now, I'm trying to add external libraries to my plugin (so far I've tried Facebook Android SDK, Google Analytics..) and when I try to use any of the classes stored in these libraries I'm getting the following errors:

    Code (CSharp):
    1. I/Unity﹕ AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/analytics/GoogleAnalytics;
    Seems like I'm missing something here. Could you guys help me?

    Thanks,
     
  2. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    I've been inspecting the resulting jar file and I've realized that the classes included in the external libraries classes are not actually included into the compiled package.
    Check the following screenshot of my plugin (it should include both my classes + FacebookSDK classes as well but it's not):


    So, probably I'm missing something on my gradle settings:

    Code (CSharp):
    1. //indicates that this is a library
    2. apply plugin: 'com.android.library'
    3.  
    4. android {
    5.     compileSdkVersion 22
    6.     buildToolsVersion "22.0.1"
    7.     sourceSets {
    8.         main {
    9.             //Path to your source code
    10.             java {
    11.                 srcDir 'src/main/java'
    12.             }
    13.         }
    14.     }
    15.  
    16.     defaultConfig {
    17.         minSdkVersion 15
    18.         targetSdkVersion 22
    19.     }
    20.     buildTypes {
    21.         release {
    22.             minifyEnabled false
    23.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    24.         }
    25.     }
    26.     lintOptions {
    27.         abortOnError false
    28.     }
    29.  
    30. }
    31.  
    32. repositories {
    33.     mavenCentral()
    34. }
    35.  
    36. dependencies {
    37.     compile fileTree(include: ['*.jar'], dir: 'libs')
    38.     compile 'com.android.support:appcompat-v7:22.2.0'
    39.     compile files('libs/classes.jar')
    40.     compile 'com.facebook.android:facebook-android-sdk:4.4.0'
    41.     compile files('libs/AndroidPlugin.jar')
    42. }
    43. //task to delete the old jar
    44. task deleteOldJar(type: Delete) {
    45.     delete 'release/AndroidPlugin.jar'
    46. }
    47.  
    48. //task to export contents as jar
    49. task exportJar(type: Copy) {
    50.     from('build/intermediates/bundles/release/')
    51.     into('release/')
    52.     include('classes.jar')
    53.     ///Rename the jar
    54.     rename('classes.jar', 'AppInvite.jar')
    55. }
    56.  
    57. exportJar.dependsOn(deleteOldJar, build)
    58.  
    What do you guys think? Thanks,
     
    Last edited: Aug 1, 2015
  3. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
  4. DontShooot

    DontShooot

    Joined:
    Nov 27, 2012
    Posts:
    4
    I tried to follow this tutorial and had NoClassDefFoundError too.
    To solve it - don't include BlankActivity when creating a project. Make it empty and then create your java class which extends UnityPlayerActivity. Works fine for me
     
  5. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Did you ever find a solution for this? I'm having the same issue.
     
  6. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    Did you? I actually solved this. Can't remember exactly how, but if you are still stuck, I would try to remember what I've did..
     
  7. shubhamagarwal003

    shubhamagarwal003

    Joined:
    Dec 21, 2015
    Posts:
    3
    Hi blummersolutions,
    Can you help me how you solved this problem? I am also facing a similar issue.
     
  8. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    I can't remember exactly how I solved this, but I'm not including the facebook SDK in the Unity project and instead copy the aar into the Android Studio project, into its libs folder.

    This works perfectly for me.

    Hope that helps.
     
  9. devotionsolutions

    devotionsolutions

    Joined:
    Feb 9, 2013
    Posts:
    40
    Hi, please try larku suggestion (I think that's what I've did actually). And post back if you are still stuck.
     
  10. dylantrafford

    dylantrafford

    Joined:
    May 1, 2016
    Posts:
    2
    I'm struggling with this. We are working towards an Android Studio USB serial plugin for Unity but I cannot drag the packages that I need with me and get this error:

    ...
    Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/felhr/usbserial/UsbSerialDevice;
    ...

    The package is imported with maven ->

    project/build.gradle
    allprojects {
    repositories {
    jcenter()
    maven { url "https://jitpack.io" }
    }
    }

    app/build.gradle
    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'compile 'com.github.felHR85:UsbSerial:4.2'compile 'com.android.support:appcompat-v7:23.3.0'compile files('libs/classes.jar')
    compile 'com.google.android.gms:play-services-appindexing:8.1.0'}

    Any ideas?
     
  11. dylantrafford

    dylantrafford

    Joined:
    May 1, 2016
    Posts:
    2
    Got it working!!

    This seems kind of obvious after I figured it out but I found a .jar version of the library I was importing, and simply placed it in the Unity folder with the AndroidPlugin.jar.

    Then I used the usbserial.jar file in my dependencies like the classes.jar file and avoided maven all together. Happy days!
     
  12. kamran-bigdely

    kamran-bigdely

    Joined:
    Jun 19, 2014
    Posts:
    25
    I found the usbserial.jar from the author's git repo. However, I included the library into my own library (compiled it). So I wouldn't need to put usbserial.jar in the
    \Assets\Plugins\Android folder (if I do so, it would give me error saying that it's been already added).
     
  13. AbandonedCart

    AbandonedCart

    Joined:
    Mar 4, 2014
    Posts:
    72
    When including it in both locations, use "provided" instead of "compile" in your library gradle.
     
  14. ECNUnity

    ECNUnity

    Joined:
    Feb 4, 2018
    Posts:
    2
    Hello Everyone,

    I am facing the same issue. Currently what i have done is made android project as library and written android plugin which is being called in unity. Noticed below error. can anybody help me i this.
    Here is my errors.

    06-14 11:59:15.388 14515-14542/? E/Unity: AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
    java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
    06-14 12:01:12.761 14515-14542/? I/art: Rejecting re-init on previously-failed class java.lang.Class<com.nexstreaming.app.unitylibrary.AndroidPlugin>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
    (Throwable with no stack trace)Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.app.AppCompatActivity" on path: DexPathList[[zip file "/data/app/com.ltts.dialog-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ltts.dialog-2/lib/arm, /data/app/com.ltts.dialog-2/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
    (Throwable with no stack trace)
    06-14 12:01:12.781 14515-14542/? E/Unity: AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
    java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/app/AppCompatActivity;
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:400)
    at java.lang.Class.forName(Class.java:326)
    at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    at com.unity3d.player.UnityPlayer.c(Unknown Source)
    at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source)
    at android.os.MessageQueue.next(MessageQueue.java:392)
    at android.os.Looper.loop(Looper.java:136)
    at com.unity3d.player.UnityPlayer$e.run(Unknown Source)
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.app.AppCompatActivity" on path: DexPathList[[zip file "/data/app/com.ltts.dialog-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ltts.dialog-2/lib/arm, /data/app/com.ltts.dialog-2/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
    at dalvik.
    06-14 12:01:12.781 14515-14562/? E/mono: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
    at UnityEngine.AndroidJavaObject.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0
     
  15. RamboPan

    RamboPan

    Joined:
    Dec 11, 2018
    Posts:
    1
    I meet the same issue recently.
    But the reason not the same. I found the jar file I import need the other jar file。but i not import too,When I import the other files solved this。
    too careless -_-!
     
  16. kamrankhorasani

    kamrankhorasani

    Joined:
    Dec 31, 2014
    Posts:
    1
     
  17. SunSailor

    SunSailor

    Joined:
    Aug 22, 2012
    Posts:
    26
    Actually, that's the journey where I come from, so yes, it ist. My gradle.properies contain:

    android.enableJetifier=true
    android.useAndroidX=true

    Both in the libraries as well as in the app project. Invoking the AndroidX upgrader in Android Studio doesn't find any more legacy references as well, so I guess it is fine on that. But thanks for pointing out, sadly that is not the issue.
     
  18. abhimanyu-singh

    abhimanyu-singh

    Joined:
    May 24, 2016
    Posts:
    11
    I was getting this error while integrating Unity Ads with Iron Source. Just in case this helps anyone, adding my fix below.

    The original issue I was facing was that Unity wouldn't let me create Android build with Iron Source despite following their tutorial. It kept failing the build at the last moment, saying there were duplicate classes/JARs. Something similar to this:
    Execution failed for task ':launcher:checkReleaseDuplicateClasses'.
    > 1 exception was raised by workers


    Someone suggested in another thread that doing the following in Unity will fix the issue, which I did:
    Unity Menu Bar > Assets > External Dependency Manager > Android Resolver > Delete Resolved Libraries


    This allowed me to create the build but then the build kept crashing on Launch with this error:
    AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/ironsource/mediationsdk/sdk/InterstitialListener;


    After wasting an entire day trying to figure this out, I finally found the issues. I had earlier already installed "Unity Advertisement" module via Unity Package Manager, which was conflicting with Iron Source's Unity Ads SDK.

    Once I removed Unity Ads from Unity Package Manager, the original build error (i.e. checkReleaseDuplicateClasses) got fixed. In case you're facing similar issues, verify if there are multiple sources of same library (as the error suggests).

    The other NoClassDefFound error was being caused because all resolved libraries were getting deleted when I was executing the "Delete Resolved Libraries" step I mentioned above. So do not do that, and in case you already did, rerun the Resolve tool by going to:
    Unity Menu Bar > Assets > External Dependency Manager > Android Resolver > Resolve
     
    kkdaman57, inferno222, ToughV and 2 others like this.
  19. Cuisor

    Cuisor

    Joined:
    Feb 19, 2015
    Posts:
    4
    I had this problem with duplicated Firebase libs that didn't get cleaned up. Your explanation set me on the right track. Thanks.
     
  20. ToughV

    ToughV

    Joined:
    Apr 28, 2021
    Posts:
    1
    Thanks bro! It works!