Kotlin Activity不包含在APK中

我将我的第一个Kotlin代码(一个新的活动)介绍到已建立的Android应用程序中。 使用Android Studio 2.3.3,Gradle 3.3。 我能够构建和运行应用程序,但是在启动新的Kotlin活动时,应用程序崩溃,但没有发现异常。 分析APK表明Kotlin类没有被包括在dex(和它的multi-dex,已经检查了两个dex文件)。

build.gradle(项目)

buildscript { ext.kotlin_version = '1.1.4' repositories { jcenter() maven { url 'https://maven.fabric.io/public' } mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.google.gms:google-services:3.0.0' classpath 'com.github.triplet.gradle:play-publisher:1.1.4' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'io.fabric.tools:gradle:1.+' classpath 'me.tatarka:gradle-retrolambda:3.2.5' classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.2.1' // For automated ui testing classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.2.2' // For Amazon Device Farm classpath 'com.amazonaws:aws-devicefarm-gradle-plugin:1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } jcenter() maven { url 'http://dl.bintray.com/optimizely/optimizely' } maven { url "https://jitpack.io" } maven { url "http://salesforce-marketingcloud.github.io/JB4A-SDK-Android/repository" } flatDir { dirs 'libs' } } } 

build.gradle(应用程序模块)

 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'io.fabric' apply plugin: 'me.tatarka.retrolambda' apply plugin: 'jacoco' apply plugin: 'spoon' repositories { mavenCentral() maven { url 'https://maven.fabric.io/public' } maven { url 'http://salesforce-marketingcloud.github.io/JB4A-SDK-Android/repository' } maven { url "https://maven.google.com" } maven { url "http://maven.tealiumiq.com/android/releases/" } } android { compileSdkVersion 25 buildToolsVersion '25.0.0' sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { targetSdkVersion 25 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" // Enabling multidex support. :( multiDexEnabled true } dexOptions { javaMaxHeapSize "8g" dexInProcess true // the magic line } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 compileOptions.incremental = false } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) //include our modules compile project(':core') compile project(':data') //android final APP_COMPAT_VERSION = '25.4.0' compile "com.android.support:appcompat-v7:$APP_COMPAT_VERSION" compile "com.android.support:recyclerview-v7:$APP_COMPAT_VERSION" compile "com.android.support:design:$APP_COMPAT_VERSION" compile "com.android.support:multidex:1.0.0" compile "com.android.support:cardview-v7:$APP_COMPAT_VERSION" // google final PLAY_SERVICES_VERSION = '10.2.4' compile "com.google.android.gms:play-services-wallet:$PLAY_SERVICES_VERSION" compile "com.google.android.gms:play-services-location:$PLAY_SERVICES_VERSION" compile "com.google.android.gms:play-services-gcm:$PLAY_SERVICES_VERSION" compile "com.google.android.gms:play-services-plus:$PLAY_SERVICES_VERSION" compile "com.google.android.gms:play-services-identity:$PLAY_SERVICES_VERSION" compile "com.google.android.gms:play-services-analytics:$PLAY_SERVICES_VERSION" compile "com.google.android.gms:play-services-auth:$PLAY_SERVICES_VERSION" compile "com.google.android.gms:play-services-maps:$PLAY_SERVICES_VERSION" compile "org.jetbrains.kotlin:kotlin-stdlib" compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" androidTestCompile jUnit // Unit tests dependencies testCompile jUnit testCompile "org.hamcrest:hamcrest-core:$HAMCREST_VERSION" testCompile "org.hamcrest:hamcrest-library:$HAMCREST_VERSION" testCompile "org.hamcrest:hamcrest-integration:$HAMCREST_VERSION" testCompile 'org.robolectric:robolectric:3.1' testCompile 'org.mockito:mockito-core:1.+' testCompile 'com.google.guava:guava:19.0' testCompile("com.android.support:support-v4:$APP_COMPAT_VERSION") { exclude module: 'support-annotations' } testCompile('org.powermock:powermock-api-mockito:1.6.4') { exclude module: 'objenesis' } testCompile('org.powermock:powermock-module-junit4:1.6.4') { exclude module: 'objenesis' } testCompile 'io.reactivex:rxandroid:1.0.1' testCompile 'io.reactivex:rxjava:1.1.0' // Espresso androidTestCompile('com.android.support.test:runner:0.5') { exclude module: 'support-annotations' } androidTestCompile('com.android.support.test:rules:0.5') { exclude module: 'support-annotations' } androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') { exclude module: 'support-annotations' } androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.2') { exclude module: 'support-annotations' } androidTestCompile('com.android.support.test.espresso:espresso-web:2.2.2') { exclude module: 'support-annotations' } androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') { exclude module: 'support-annotations' exclude module: 'recyclerview-v7' exclude module: 'appcompat-v7' exclude module: 'design' exclude module: 'support-v4' } // allows java 8 compile compile 'com.annimon:stream:1.1.2' uitestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' developmentCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" } 

而这个活动很简单:

 class HyperFlashActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(hyperflash_activity_layout) } } 

和启动代码:

 public static void launchHyperflashActivity(Activity launchingActivity) { Intent intent = new Intent(launchingActivity, HyperFlashActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); launchingActivity.startActivity(intent); launchingActivity.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); } 

我曾经有过同样的问题1 ,基本上,解决的办法是代替Android Studio 2.x下载Android Studio 3.0 Beta 。 即使代码突出显示在使用2.x版本时仍然有效,这应该可以解决您的问题。

当你看到新功能选项卡,你可以看到它支持Kotlin语言 , 这种类型的表明它缺乏之前(我认为在这种情况下,它只是内置到Android Studio中,并专门针对Android Studio的要求进行了增强(如包括类到dex文件))。

1 :除了multidex,因为我不需要它