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

时间:2018-02-07 23:14:16

标签: android kotlin

我目前正在构建一个用Kotlin编写的库项目。当我导出.aar文件并在项目上编译时,我得到以下错误,引用by lazy lambda。

我在示例应用

上遇到的错误
Rejecting re-init on previously-failed class java.lang.Class<com.myproject.lib.MainActivity$view$2>: 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

库项目中引用的行异常

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"
    }
}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

问题是aar文件是在没有项目依赖项的情况下构建的。

我选择的解决方案是创建私有maven存储库并使用依赖项发布我的项目。

要将依赖项添加到生成的pom.xml文件,您可以使用模块级withXml中的build.gradle任务修改发布方法

apply plugin: 'maven-publish'
group = 'com.group'
archivesBaseName = 'group-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)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}