0

我試圖在我的現有項目中使用Android Studio 3.0的Kotlin。 我用kotlin創建了一個片段。我試圖在我的Java活動中使用kotlin片段。但每次我嘗試運行它,我得到Android Studio 3無法建立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' 
+1

你試過無效緩存/重新啓動或乾淨和項目的重建? – MichaelStoddart

回答

1

終於能夠得到它的工作。對於正在經歷類似問題的任何人。

項目的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' 
1

你似乎缺少所有Kotlin的依賴關係除了聲明版本(你應該更新到1.1.3,這是迄今爲止的最新版本)。

您可以通過兩種方式配置您的項目:

  • 取消已宣佈科特林版本,然後去Tools -> Kotlin -> Configure Kotlin in Project,並按照提出有步驟。
  • 或者,您可以通過按照here中描述的步驟以更加手動的方式來設置Kotlin Gradle插件,包括遵循那裏的鏈接並設置標準庫依賴關係。