Android Studio 3无法建立kotlin片段

我试图使用Android Studio 3.0与我现有的项目Kotlin。 我用kotlin创建了一个片段。 我试图在我的Java活动中使用kotlin片段。 但每次我尝试运行它,我都会得到

Error:(209, 5) error: cannot find symbol class BlankFragment 

BlankFragment.kt

 import android.content.Context import android.net.Uri import android.os.Bundle import android.support.v4.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup class BlankFragment : Fragment() { // TODO: Rename and change types of parameters private var mParam1: String? = null private var mParam2: String? = null private var mListener: OnFragmentInteractionListener? = null public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1) mParam2 = getArguments().getString(ARG_PARAM2) } } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View = FragmentUi<Fragment>().createView(AnkoContext.create(ctx, this)) } class FragmentUi<T>: AnkoComponent<T> { override fun createView(ui: AnkoContext<T>) = with(ui) { verticalLayout { textView { text = R.string.app_name } } } } 

项目 – build.gradle

 // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.1.1' ext.anko_version = '0.10.1' repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-alpha5' classpath 'com.google.gms:google-services:3.0.0' classpath "io.realm:realm-gradle-plugin:3.0.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } 

应用程序build.gradle

 apply plugin: 'com.android.application' apply plugin: 'realm-android' android { signingConfigs { splits { abi { enable true //enables the ABIs split mechanism reset() //reset the list of ABIs to be included to an empty string include 'x86', 'armeabi', 'armeabi-v7a', 'mips', 'x86_64', 'arm64-v8a' universalApk true } } project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9] android.applicationVariants.all { variant -> variant.outputs.each { output -> output.versionCodeOverride = project.ext.versionCodes.get(output.getFilter( com.android.build.OutputFile.ABI), 0) * 10000000 + android.defaultConfig.versionCode } } } compileSdkVersion 25 buildToolsVersion '26' defaultConfig { applicationId "com.example" minSdkVersion 15 targetSdkVersion 23 versionCode 20 versionName "v.32" testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.releaseConfig } } packagingOptions { exclude 'LICENSE.txt' } repositories { maven { url "https://jitpack.io" } } lintOptions { disable "ResourceType" } dexOptions { javaMaxHeapSize "4g" } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.google.android.gms:play-services:10.2.6' compile 'com.android.support:design:25.3.1' compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.google.code.gson:gson:2.6.2' // compile 'io.realm:realm-android:0.87.5' compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.github.jkwiecien:EasyImage:1.3.1' compile 'com.mcxiaoke.volley:library:1.0.19' compile 'com.yarolegovich:lovely-dialog:1.0.4' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.github.ParkSangGwon:TedPermission:v1.0.12' compile 'com.github.lovetuzitong:MultiImageSelector:1.2' compile 'com.github.piotrek1543:CustomSpinner:0.1' compile 'com.github.ganfra:material-spinner:1.1.1' compile 'com.getbase:floatingactionbutton:1.10.1' compile 'com.appyvet:materialrangebar:1.3' compile 'com.afollestad.material-dialogs:core:0.9.4.5' compile 'com.mixpanel.android:mixpanel-android:4.+' compile 'com.github.ericwlange:AndroidJSCore:3.0.1' compile 'com.android.support:multidex:1.0.0' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' } compile "org.jetbrains.anko:anko-sdk25:$anko_version" compile "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version" compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version" compile files('libs/bcpkix-jdk15on-1.56.0.0.jar') compile files('libs/core-1.56.0.0.jar') compile files('libs/prov-1.56.0.0.jar') } apply plugin: 'com.google.gms.google-services' 

除了声明版本之外,您似乎错过了所有Kotlin依赖关系(您应该更新到1.1.3 ,这是迄今为止最新的版本)。

您可以通过两种方式配置您的项目:

  • 删除已声明的Kotlin版本,然后转到工程Tools -> Kotlin -> Configure Kotlin in Project ,然后按照其中提供的步骤操作。
  • 或者,您可以通过按照此处所述的步骤设置Kotlin Gradle插件,以更手动的方式执行此操作,其中包括跟随链接并设置标准库依赖关系。

最后能够得到它的工作。 对于正在经历类似问题的任何人。

项目build.gradle

 // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.1.3' ext.anko_version = '0.10.1' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.android.tools.build:gradle:3.0.0-alpha5' classpath 'com.google.gms:google-services:3.0.0' classpath "io.realm:realm-gradle-plugin:3.0.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() mavenCentral() } } task clean(type: Delete) { delete rootProject.buildDir } 

应用程序build.gradle

 apply plugin: 'com.android.application' apply plugin: 'realm-android' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { signingConfigs { splits { abi { enable true //enables the ABIs split mechanism reset() //reset the list of ABIs to be included to an empty string include 'x86', 'armeabi', 'armeabi-v7a', 'mips', 'x86_64', 'arm64-v8a' universalApk true } } project.ext.versionCodes = ['armeabi': 1, 'armeabi-v7a': 2, 'arm64-v8a': 3, 'mips': 5, 'mips64': 6, 'x86': 8, 'x86_64': 9] android.applicationVariants.all { variant -> variant.outputs.each { output -> output.versionCodeOverride = project.ext.versionCodes.get(output.getFilter( com.android.build.OutputFile.ABI), 0) * 10000000 + android.defaultConfig.versionCode } } } compileSdkVersion 25 buildToolsVersion '26' defaultConfig { applicationId "com.example" minSdkVersion 15 targetSdkVersion 23 versionCode 20 versionName "v.32" testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.releaseConfig } } packagingOptions { exclude 'LICENSE.txt' } repositories { maven { url "https://jitpack.io" } } lintOptions { disable "ResourceType" } dexOptions { javaMaxHeapSize "4g" } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.google.android.gms:play-services:10.2.6' compile 'com.android.support:design:25.3.1' compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.google.code.gson:gson:2.6.2' // compile 'io.realm:realm-android:0.87.5' compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.github.jkwiecien:EasyImage:1.3.1' compile 'com.mcxiaoke.volley:library:1.0.19' compile 'com.yarolegovich:lovely-dialog:1.0.4' compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' compile 'com.github.ParkSangGwon:TedPermission:v1.0.12' compile 'com.github.lovetuzitong:MultiImageSelector:1.2' compile 'com.github.piotrek1543:CustomSpinner:0.1' compile 'com.github.ganfra:material-spinner:1.1.1' compile 'com.getbase:floatingactionbutton:1.10.1' compile 'com.appyvet:materialrangebar:1.3' compile 'com.afollestad.material-dialogs:core:0.9.4.5' compile 'com.mixpanel.android:mixpanel-android:4.+' compile 'com.github.ericwlange:AndroidJSCore:3.0.1' compile 'com.android.support:multidex:1.0.0' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' } compile "org.jetbrains.anko:anko-sdk25:$anko_version" compile "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version" compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version" implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" compile files('libs/bcpkix-jdk15on-1.56.0.0.jar') compile files('libs/core-1.56.0.0.jar') compile files('libs/prov-1.56.0.0.jar') } apply plugin: 'com.google.gms.google-services'