【问题标题】:Applying the plugin 'kotlin-android' in an Instant App results in "null cannot be cast to non-null type com.android.build.gradleBasePlugin"在 Instant App 中应用插件 'kotlin-android' 会导致“null cannot be cast to non-null type com.android.build.gradleBasePlugin”
【发布时间】:2017-10-20 19:03:38
【问题描述】:

我一直在尝试将新发布的 Android Instant Apps 与 Kotlin 编程语言结合起来。使用以下(标准?)设置创建我的项目后,我在尝试时收到错误消息 "null cannot be cast to non-null type com.android.build.gradle.BasePlugin"构建应用程序。使用 Kotlin 可以与标准 'com.android.application' 模块配合使用;只有当我尝试在 Instant App 模块中使用它时才会引发错误。

顶级build.gradle

buildscript {

    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:3.0.0-alpha1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-4"
    }
}

// ...

app 模块 build.gradle,Kotlin在其中工作:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' // This will work.

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"


    defaultConfig {
        // ...
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation project(':feature')
    implementation project(':base')
}

base 模块 build.gradle,其中 Kotlin 工作:

apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android' // This won't work.

android {

    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    baseFeature true
    defaultConfig {
        // ...
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    feature project(':tracker')
    compile 'com.android.support:appcompat-v7:25.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

    // Kotlin standard library.
    compile "org.jetbrains.kotlin:kotlin-stdlib:${gradleKotlinVersion}"
}

instantapp 模块 build.gradle

apply plugin: 'com.android.instantapp'

dependencies {
    implementation project(':feature')
    implementation project(':base')
}

功能模块build.gradle

apply plugin: 'com.android.feature'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        // ...
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation project(':base')
    testCompile 'junit:junit:4.12'
}

同样,app 模块在此配置下编译时没有问题;另一方面,Android Studio / Gradle 给了我这个奇怪的 "null cannot be cast to non-null type com.android.build.gradle.BasePlugin" 错误消息,并建议重新下载依赖项并同步项目(完成)或重启 Android Studio。

Instant Apps 实际上与 Kotlin 编程语言兼容吗?我期待着你的回答:)

PS:我使用 Android Studio 3.0 Canary 1,并从 Canary 频道安装了用于构建工具等的最新更新。我的 Kotlin 插件也应该是最新的。

【问题讨论】:

  • 我不会太担心这现在不起作用。我现在什至无法构建一个普通的应用程序(也是 kotlin),因为由于 com.android.build 部门的一些异常,transformClassesWithDexBuilderForDebug 不断失败。我通过恢复到 gradle 插件 2.3.2 解决了这些问题,但遗憾的是这样你就失去了高级分析器支持
  • 您找到解决该问题的方法了吗?
  • 绝对不是。我已经在 Google 的问题跟踪器上订阅了该问题,我正在等待他们对问题的更新。

标签: android android-studio android-gradle-plugin kotlin android-instant-apps


【解决方案1】:

这在Kotlin plugin v1.1.2-5 中得到解决。请更新您的版本,然后重试。

作为参考,这里跟踪以前插件版本的问题:

38393607 | Issue building Instant apps with Kotlin enabled

【讨论】:

    【解决方案2】:

    参考Google issue tracker,已修复。 同时,您可以手动更新到 1.1.3 kotlin 插件。

    如果任何问题仍然存在,请通过Google issue tracker 报告,他们将重新打开进行检查。

    【讨论】:

      猜你喜欢
      • 2020-05-14
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多