Unity 中带有 moshi 的 ClassNotFoundException

ClassNotFoundException with moshi in Unity

我制作了一个库,使用 Square 的 moshi 来处理它的 JSON 序列化和一些其他库,如 RxJava、dagger 等
因为我想在 Unity 上使用它,所以我必须从我自己的库中导出一个 AAR,并在 unity 项目中添加 moshi。

我使用 Unity-jar-resolver 工具在项目中添加我的库和依赖项。 使用 Gradle 作为构建系统来制作项目时出现一些错误。

以下是有关该错误的更多详细信息:

而且是纯文本:

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.access0(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
]

我用了Gradle,因为我需要申请multidex。这是我的配置:

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**

如果有人对此问题有任何解决方案,我将很高兴。

看来您还需要 Javapoet 作为运行时依赖项。尝试将此添加到您的 dependencies 块中:

runtime 'com.squareup:javapoet:1.11.1'

您不需要将注释处理器添加为库依赖项,如 testCompile s。

只需将 gradle 中的所有内容添加为您的库 build.gradle 中的实现,然后尝试解决依赖关系。

注释处理器和Codegen库只在编译时使用,仅此而已。因此,当您尝试使用已编译的时,您不需要它们。