Kotlin中的Android项目编译出现 Unresolved reference: R 或者context、it 、entity解决

一.直接看看如何快速的让项目可以使用Kotlin步骤:

1.在app的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'
//    编辑个人信息,使用户可以拖动和排列图像顺序
    implementation 'com.github.SwiftyWang.android-drag-square:dragsquareimage:1.2.3'
//    一个简单的演示应用程序,如何像滑动操作一样实现iOS Mail应用程序
    dependencies {
        compile ('com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:0.8.6@aar'){
            transitive=true
        }
    }
//    史上最简单侧滑菜单,0耦合,支持任意ViewGroup。一步集成侧滑(删除)菜单,高仿QQ、IOS
    dependencies {
        implementation ('com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'){
            transitive=true
        }
    }
    //Adapter终结者 地址: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'
//    滑动菜单库不仅用于recyclerview,还用于所有视图。
    dependencies {
        implementation ('com.github.anzaizai:EasySwipeMenuLayout:1.1.4'){
            transitive=true
        }
    }
//    //Wave,水波,Android 炫酷的多重水波纹 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'
//    粒子动画
    implementation 'me.wangyuwei:ParticleView:1.0.4'
    //下拉刷新上拉加载框架
    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-21'
    //圆形图片头像框架
    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'
    //用来检查 Activity 的生命周期,而不必强依赖 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"
}

可以看出,需要添加:

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.在项目的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
}

可以看出,添加:
 

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

二.问题解决:

检查Kotlin的版本号是否一致,不一致会报错的,改为一致即可

还可以:File -> Invalidate Caches / Restart 。重启之后,Build -> Rebuild Project。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值