Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException with moshi in Unity

I made a library that uses Square's moshi to handle it's JSON serializations and a few other libraries like RxJava, dagger, etc.
Since I want to use it on Unity I have to export an AAR from my own library and also add moshi in the unity project.

I use the Unity-jar-resolver tool to add my libraries and dependencies in the project. Using the Gradle as the build system to make project I get some errors.

Error build failed.

Here are more details about the error:

Error details.

And it's plain text:

stderr[
Exception in thread "main" java.lang.IllegalStateException: Couldn't desugar invokedynamic for dagger/shaded/auto/common/GeneratedAnnotationSpecs.apply using java/lang/invoke/LambdaMetafactory.metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; (6) with arguments [(Ljava/lang/Object;)Ljava/lang/Object;, com/squareup/javapoet/AnnotationSpec$Builder.build()Lcom/squareup/javapoet/AnnotationSpec; (5), (Lcom/squareup/javapoet/AnnotationSpec$Builder;)Lcom/squareup/javapoet/AnnotationSpec;]
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.visitInvokeDynamicInsn(LambdaDesugaring.java:467)
    at org.objectweb.asm.MethodVisitor.visitInvokeDynamicInsn(Unknown Source)
    at org.objectweb.asm.MethodVisitor.visitInvokeDynamicInsn(Unknown Source)
    at org.objectweb.asm.ClassReader.a(Unknown Source)
    at org.objectweb.asm.ClassReader.b(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at com.google.devtools.build.android.desugar.Desugar.desugarClassesInInput(Desugar.java:401)
    at com.google.devtools.build.android.desugar.Desugar.desugarOneInput(Desugar.java:326)
    at com.google.devtools.build.android.desugar.Desugar.desugar(Desugar.java:280)
    at com.google.devtools.build.android.desugar.Desugar.main(Desugar.java:584)
Caused by: java.lang.ClassNotFoundException: Class com.squareup.javapoet.AnnotationSpec$Builder not found
    at com.google.devtools.build.android.desugar.HeaderClassLoader.findClass(HeaderClassLoader.java:53)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.google.devtools.build.android.desugar.LambdaDesugaring.loadFromInternal(LambdaDesugaring.java:330)
    at com.google.devtools.build.android.desugar.LambdaDesugaring.access$500(LambdaDesugaring.java:53)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.toMethodHandle(LambdaDesugaring.java:597)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.toJvmMetatype(LambdaDesugaring.java:586)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.visitInvokeDynamicInsn(LambdaDesugaring.java:401)
    ... 10 more


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':transformClassesWithDesugarForRelease'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: Error while executing java process with main class com.google.devtools.build.android.desugar.Desugar with arguments {@C:\Users\Mahdi\Documents\UnityPrj2\Pushe21\Temp\gradleOut\build\intermediates\tmp\desugar_args432898573736991074}

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 12s
]

I used Gradle because I needed to apply multidex. Here's my configuration:

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
        flatDir {
            dirs 'libs'
        }
    }
}

apply plugin: 'com.android.application'

dependencies {
    annotationProcessor "com.squareup.moshi:moshi-kotlin-codegen:1.7.0"
**DEPS**}

android {
    compileSdkVersion **APIVERSION**
    buildToolsVersion '**BUILDTOOLS**'

    defaultConfig {
        multiDexEnabled true
        minSdkVersion **MINSDKVERSION**
        targetSdkVersion **TARGETSDKVERSION**
        applicationId '**APPLICATIONID**'
        ndk {
            abiFilters **ABIFILTERS**
        }
        versionCode **VERSIONCODE**
        versionName '**VERSIONNAME**'
    }

    lintOptions {
        abortOnError false
    }

    aaptOptions {
        noCompress '.unity3d', '.ress', '.resource', '.obb'**STREAMING_ASSETS**
    }**SIGN**

    buildTypes {
        debug {
            minifyEnabled **MINIFY_DEBUG**
            useProguard **PROGUARD_DEBUG**
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD**
            jniDebuggable true
        }
        release {
            minifyEnabled **MINIFY_RELEASE**
            useProguard **PROGUARD_RELEASE**
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'**USER_PROGUARD****SIGNCONFIG**
        }
    }**PACKAGING_OPTIONS****SPLITS**

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    } 

**BUILT_APK_LOCATION**
}**SPLITS_VERSION_CODE****SOURCE_BUILD_SETUP**

I would be happy if anyone had any solutions for this issue.

like image 408
Mahdi-Malv Avatar asked Jan 29 '19 13:01

Mahdi-Malv


1 Answers

You don't need annotation processors to be added as library dependencies, so as testCompile s.

Simply add everything that was in the gradle as implementation in your library build.gradle, then try resolving dependencies.

Annotation processors and Codegen libraries are only used when compiling and that's all. So you don't need them when you are trying to use an already compiled one.

like image 78
Mo Meshkani Avatar answered Nov 15 '22 08:11

Mo Meshkani