AndroidStudio 引入項目代碼報錯(R文件紅色)
一、自帶清除緩存
點擊AndroidStudio菜單File -> Invalidate Caches/Restar… ,在彈出的對話框中點擊Invalidate and Restar,等待AS清理並自動重啓。
二、刪除user目錄下.AndroidStudio中的文件
關閉AndroidStudio,進入C:\Users\xUser.AndroidStudio3.1(3.1爲當前AS版本號),刪除除了config以外的文件。重新打開AndroidStudio會自動Sync,等待同步完成。
三、刪除user目錄下.gradle
關閉AndroidStudio,刪除C:\Users\xUser.gradle。重新打開AndroidStudio會自動Sync,等待同步完成。
注意:刪除該文件夾所有的classpath、gradle、compile、implementation等,都需要重新從網上同步。
四、刪除工程目錄下.gradle、.idea
關閉AndroidStudio,進入工程根目錄,刪除.gradle、.idea兩個文件夾。重新打開AndroidStudio會自動Sync,等待同步完成。
五、R文件爆紅但是項目可以運行
這種原因是你的R文件太大了(超過了Android Studio的上限25.6M了)
解決辦法如下:
Help -> Edit Custom Properties -> create custom properties?(yes) ->添加 idea.max.intellisense.filesize=5000
最後重啓Android Studio即可!!!
六、gradle和gradle插件版本要對應
1.gradle版本配置位置
在gradle/wrapper/gradle-wrapper.properties 中:
distributionUrl = https://services.gradle.org/distributions/gradle-4.10.1-all.zip
2.gradle插件版本配置位置
在項目的build.gradle文件裏
buildscript {
repositories {
// Gradle 4.1 and higher include support for Google's Maven repo using
// the google() method. And you need to include this repo to download
// Android Gradle plugin 3.0.0 or higher.
google()
...
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
}
}
3.具體的版本對應關係
七、studio版本爲3.2以下升級爲3.4版本或更高
以上幾種方式都不能解決問題的話,要看下studio版本升級可以解決問題