10

I've been trying to combine the newly publicly released Android Instant Apps with the Kotlin programming language. After creating my project using the following (standard?) setup, I get an Error with the message "null cannot be cast to non-null type com.android.build.gradle.BasePlugin" when I try to build the application. Using Kotlin works fine with standard 'com.android.application' modules; the Error is thrown only when I try to use it within an Instant App module.

Top-level 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 module build.gradle, in which Kotlin does work:

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 module build.gradle, in which Kotlin does not work:

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 module build.gradle:

apply plugin: 'com.android.instantapp'

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

feature module 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'
}

Again, the app module compiles without issues with this configuration; on the other hand, Android Studio / Gradle gives me this strange "null cannot be cast to non-null type com.android.build.gradle.BasePlugin" error message, with the suggestions to re-download dependencies and sync the project (done) or restart Android Studio.

Are Instant Apps actually compatible with the Kotlin programming language ? I'm looking forward to your answers :)

PS: I use Android Studio 3.0 Canary 1, with the latest updates installed from the Canary Channel for Build tools etc. My Kotlin plugin should be up-to-date as well.

Volo
  • 28,673
  • 12
  • 97
  • 125
  • I wouldn't worry too much that this doesn't work right now. I can't even build a normal app (kotlin also) right now as transformClassesWithDexBuilderForDebug is constantly failing due to some exception in the com.android.build department. I fixed these issues by reverting to gradle plugin 2.3.2, but sadly this way you're losing advanced profiler support – Julian Os May 21 '17 at 09:02
  • Have you found workaround for the issue? – qpator May 30 '17 at 15:10
  • Absolutely not. I've subscribed to the issue on Google's Issue Tracker and I'm waiting for an update from their side on the problem. – Alexandre Piveteau May 30 '17 at 15:31

2 Answers2

8

This is addressed in Kotlin plugin v1.1.2-5. Please update your version and try again.

For reference, the issue with previous plugin versions is being tracked here:

38393607 | Issue building Instant apps with Kotlin enabled

AdamK
  • 21,199
  • 5
  • 42
  • 58
0

With reference to Google issue tracker,This has been fixed. In the meantime, you can manually update to the 1.1.3 kotlin plugin.

If any issue persists, please report at Google issue tracker they will re-open to examine.

Prags
  • 2,457
  • 2
  • 21
  • 38