Android-projektikokoelma Kotlinissa näyttää ratkaisemattomalta viitteeltä: R tai konteksti, se, kokonaisuusratkaisu

Android Project Compilation Kotlin Appears Unresolved Reference



1. Katso suoraan, kuinka projekti saadaan nopeasti käyttämään Kotlinin vaiheita:

1. Lisää sovelluksen build.gradle:

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { applicationId 'com.example.XXXXXX' minSdkVersion 17 targetSdkVersion 28 versionCode 1 versionName '1.0' testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support:recyclerview-v7:28.0.0' // Edit personal information so that users can drag and arrange the order of images implementation 'com.github.SwiftyWang.android-drag-square:dragsquareimage:1.2.3' // A simple demo application, how to implement iOS Mail application like a sliding operation dependencies { compile ('com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:root@xxxxx'){ transitive=true } } // The simplest side-slip menu in history, 0 coupling, supports any ViewGroup. One step integrated sliding (delete) menu, high imitation of QQ, IOS dependencies { implementation ('com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'){ transitive=true } } //Adapter Terminator Address: https://github.com/mcxtzhang/all-base-adapter implementation 'com.github.mcxtzhang:all-base-adapter:V1.5.2' implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.18' // The sliding menu library is not only used for recyclerview, but also for all views. dependencies { implementation ('com.github.anzaizai:EasySwipeMenuLayout:1.1.4'){ transitive=true } } // //Wave, water wave, Android's cool multiple water ripples MultiWaveHeader // dependencies { // implementation ('com.scwang.wave:MultiWaveHeader:1.0.0-alpha-3'){ // transitive=true // } // } //androidx // implementation 'com.scwang.wave:MultiWaveHeader:1.0.0-andx-3' implementation 'org.adw.library:discrete-seekbar:1.0.0' // Particle animation implementation 'me.wangyuwei:ParticleView:1.0.4' //Pull down to refresh and pull up to load the frame implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-21' //Round picture avatar frame implementation 'de.hdodenhof:circleimageview:2.0.0' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion' implementation'org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:support-v4:28.0.0' //Used to check the life cycle of Activity without having to rely on Activity implementation 'android.arch.lifecycle:viewmodel:$lifecycle_runtime_version' implementation 'android.arch.lifecycle:livedata-core:$lifecycle_runtime_version' implementation 'android.arch.lifecycle:extensions:$lifecycle_runtime_version' }

Kuten näette, sinun on lisättävä:



apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion' implementation'org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version'

2. Lisää projektin build.gradle:



// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { // ext.kotlin_version = '1.3.50' ext.kotlin_version = '1.2.30' ext.coroutinesVersion = '1.2.2' ext.lifecycle_runtime_version = '1.1.0' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.3' classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version' // NOTE: Do not place your application dependencies here they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir }

Kuten näette, lisää:

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version'

2. Ongelmanratkaisu:



Tarkista, ovatko Kotlin-versionumerot yhdenmukaisia. Jos ne ovat epäjohdonmukaisia, ilmoitetaan virheestä. Vaihda vain johdonmukaiseen

Voit myös: Tiedosto -> Mitätöi välimuistit / Käynnistä uudelleen. Uudelleenkäynnistyksen jälkeen, Build -> Rebuild Project.