Kotlin RC NoClassDefFoundError

2019-03-01 03:23发布

问题:

After updating kotlin to RC-1036 i'm facing a weird issue: my code is not even compiled and packed.

When build passes and the app is launched i'm getting ClassNotFoundException: Didn't find class "example.myapplication.MainActivity".

I have tried to decompile apk with apktool and didn't find my classes inside the archive. There are no classes in build/intermediates dir either.

This is my project:

app/build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'

buildscript {
    ext.kotlin_version = '1.0.0-rc-1036'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
repositories {
    mavenCentral()
}


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "example.myapplication"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="example.myapplication"
      xmlns:android="http://schemas.android.com/apk/res/android">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

And MainActivity:

package example.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        hello.text = "New text"
    }
}

Build tools version is 2.0.0-beta2; Might be something wrong with sourseSets, though nothing works if i try to reconfigure it somehow. Appreciate any help.

回答1:

You should have both these plugins:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

The first makes sure the Kotlin code compiles, the second is for the UI imports.