代码之家  ›  专栏  ›  技术社区  ›  Cactus

Android Studio找不到从布局生成的Kotlinx模块

  •  0
  • Cactus  · 技术社区  · 6 年前

    我正在使用标准的Kotlin Android扩展来引用 View 在布局XML中使用中键入的ID定义的 kotlinx.android.synthetic.main.activity_main . 我可以从android studio和cli编译我的项目,因此这些引用似乎得到了正确的解决;但是,在android studio中,import语句显示为unresolved,因此所有类型的id都不起作用。

    我的 build.gradle 如下:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-platform-android'
    apply plugin: 'kotlin-android-extensions'
    
    buildscript {
        ext.kotlin_version = '1.2.50'
    
        repositories {
            mavenCentral()
        }
    
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    androidExtensions {
        experimental = true
    }
    
    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "com.example"
            minSdkVersion 19
            targetSdkVersion 27
            versionCode 2
            versionName "0.3.0"
            vectorDrawables.useSupportLibrary = true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        implementation "org.jetbrains.anko:anko-sdk15:0.8.3"
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation 'com.android.support.constraint:constraint-layout:1.1.0'
        implementation 'com.android.support:design:27.1.1'
        implementation 'com.android.support:support-v4:27.1.1'
        implementation 'com.android.support:support-vector-drawable:27.1.1'
        implementation 'org.apache.commons:commons-io:1.3.2'
        expectedBy project(':engine')
        implementation project(':dialogue')
        implementation 'com.android.support:cardview-v7:27.1.1'
        implementation 'com.github.daniel-stoneuk:material-about-library:2.3.0'
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Cactus    6 年前

    我可以通过制作一个单独的Android库模块来解决这个问题,该模块只包含平台特定的位(而且,重要的是,没有 kotlinx 然后将其导入主应用程序模块。这意味着主应用程序模块不再需要使用 kotlin-platform-android 可能只是 kotlin-android 相反。

    总之,在包含平台特定位的模块中:

    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-platform-android'
    
    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        expectedBy project(':engine')
    }
    

    在主模块中:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            freeCompilerArgs = ["-Xmulti-platform"]
        }
    }
    
    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        implementation project(':engine-android')
    }
    

    注意我们还需要通过 -Xmulti-platform 到Kotlin编译器,以便能够链接到 :engine-android 但是我们用的是香草味的 Kotlin安卓 插件似乎可以用 kotlin-android-extensions .