Kotlin库项目失败的解决方案:[Lkotlin / reflect / KProperty

我目前正在建立一个写在Kotlin的图书馆项目。 当我导出一个.aar文件并在一个项目上编译时,我得到了一个by lazy lambda .aar的以下错误。

我在示例应用程序中有错误

 Rejecting re-init on previously-failed class java.lang.Class: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Lambda; ... java.lang.NoClassDefFoundError: Failed resolution of: [Lkotlin/reflect/KProperty; ... Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.reflect.KProperty" on path: DexPathList 

在库项目中引用的行exception

 private val view: View by lazy { LayoutInflater.from(context).inflate(R.layout.overlay_view, this, false) } 

库项目的Gradle文件

 apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: "org.jetbrains.kotlin.android.extensions" android { compileSdkVersion 27 defaultConfig { minSdkVersion 16 targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" androidExtensions { experimental = true } } sourceSets { main.java.srcDirs += 'src/main/kotlin' test.java.srcDirs += 'src/test/kotlin' androidTest.java.srcDirs += 'src/androidTest/kotlin' } buildTypes { debug { ... } release { ... } } defaultConfig { multiDexEnabled true vectorDrawables.useSupportLibrary = true } compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } testOptions { unitTests.returnDefaultValues = true } } ext.kotlin_version = '1.2.21' dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" api "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" api 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.2' api "org.jetbrains.anko:anko-coroutines:0.10.4" api 'com.android.support:appcompat-v7:27.0.2' api 'com.android.support:support-annotations:27.0.2' api 'com.squareup.okhttp3:okhttp:3.9.1' api 'com.squareup.okhttp3:logging-interceptor:3.9.1' api 'com.squareup.moshi:moshi-kotlin:1.5.0' testImplementation 'junit:junit:4.12' testCompile "org.json:json:20140107" testCompile "org.mockito:mockito-core:2.13.0" testCompile group: 'org.mockito', name: 'mockito-inline', version: '2.13.0' androidTestImplementation "org.mockito:mockito-android:2.13.0" testImplementation 'com.squareup.okhttp3:mockwebserver:3.9.1' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' } repositories { mavenCentral() } kotlin { experimental { coroutines "enable" } } 

可能是什么问题?

问题是没有项目的依赖关系构建aar文件。

我的解决方案是创建私有maven存储库,并发布我的项目与依赖。

要将你的依赖关系添加到生成的pom.xml文件中,你可以在模块级别的build.gradle withXml任务修改你的publish方法

 apply plugin: 'maven-publish' group = 'com.mapfit' archivesBaseName = 'mapfit-sdk' version = '1' publishing { repositories { maven { url myMavenRepoWriteUrl credentials { username 'myMavenRepo' password myMavenRepoReadPassword } } } publications { aar(MavenPublication) { // Artifact properties groupId group version version artifactId archivesBaseName artifact("$buildDir/outputs/aar/${archivesBaseName}-debug.aar") pom.withXml { //Creating additional node for dependencies def dependenciesNode = asNode().appendNode('dependencies') //Defining configuration names from which dependencies will be taken (debugCompile or releaseCompile and compile) def configurationNames = ["debugCompile", 'compile'] configurationNames.each { configurationName -> configurations[configurationName].allDependencies.each { if (it.group != null && it.name != null) { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) dependencyNode.appendNode('version', it.version) //If there are any exclusions in dependency if (it.excludeRules.size() > 0) { def exclusionsNode = dependencyNode.appendNode('exclusions') it.excludeRules.each { rule -> def exclusionNode = exclusionsNode.appendNode('exclusion') exclusionNode.appendNode('groupId', rule.group) exclusionNode.appendNode('artifactId', rule.module) } } } } } } } } }