Kotlin Multiplatform Android模块只能访问Android API 1

时间:2018-06-12 03:58:18

标签: android kotlin kotlin-multiplatform

我正在Kotlin开展一个有Android模块的多平台项目。

在我的一个文件中,我有代码:

import android.view.ViewGroup.LayoutParams.MATCH_PARENT

其中出现以下编译器错误:

Unresolved reference: MATCH_PARENT

除了Android API级别1之外的所有引用都会产生类似的编译器错误,但不会出现Android API级别1中提供的任何引用。

Android Studio会识别所有引用,并且代码完成工作正常。只有在编译时才会出现这些错误。

我的gradle文件如下所示:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            url "https://maven.google.com"
        }
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "digital.wup:android-maven-publish:3.1.1"
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${rootProject.kotlinVersion}"
        classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:${rootProject.dokkaVersion}"
    }
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-platform-android'
apply plugin: 'org.jetbrains.dokka-android'

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        warning('InvalidPackage', 'NewApi')
    }
}

repositories {
    google()
    mavenLocal()
    mavenCentral()
    jcenter()
    maven {
        url "https://maven.google.com"
    }
}

dependencies {
    def compatVersion = '27.1.1'

    androidTestImplementation "com.android.support:support-annotations:${compatVersion}"
//    androidTestCompile 'com.android.support.test:runner:1.0.2'
    testImplementation 'junit:junit:4.12'
    testImplementation "org.jetbrains.kotlin:kotlin-test-junit:${rootProject.kotlinVersion}"


    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation "com.lightningkite.kotlinx:jvm:${rootProject.libraryVersion}"
    implementation "com.github.bigfishcat.android:svg-android:2.0.8"
    expectedBy project(':common')

    implementation "com.android.support:appcompat-v7:${compatVersion}"
    implementation "com.android.support:cardview-v7:${compatVersion}"
    implementation "com.android.support:gridlayout-v7:${compatVersion}"
    implementation "com.android.support:recyclerview-v7:${compatVersion}"
    implementation "com.android.support:design:${compatVersion}"
}

2 个答案:

答案 0 :(得分:0)

事实证明,无论内部是什么问题,它实际上是由我的build.gradle文件中的这个库触发的:

implementation "com.github.bigfishcat.android:svg-android:2.0.8"

图书馆如何造成这种情况,我不知道。现在一切都很好。

答案 1 :(得分:-1)

申请插件:' com.android.application'

申请插件:' kotlin-android'

申请插件:' kotlin-android-extensions'

android {

compileSdkVersion 26

defaultConfig {
    applicationId "com.example.admin.myapplication"
    minSdkVersion 15
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

依赖项{

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:recyclerview-v7:26.1.0'

}

我的android studio版本是3.1.1 正确地挣扎

从您的gradle中删除所有kotlin库 并把

实施" org.jetbrains.kotlin:科特林-STDLIB-jre7:$ kotlin_version"

申请插件:' kotlin-android'

申请插件:' kotlin-android-extensions

并在顶级gradle bulid.gradle(your_app_name)put

buildscript {

ext.kotlin_version = '1.2.30'
repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.1.1'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

}