Android Error:Execution failed for task ':app:compileDebugJavaWithJavac' 解決方案
最近Android專案開發中碰到一個異常,折騰了許久才找到問題的根源。
Google、百度搜索前兩頁內容瀏覽了一遍,總結了幾種方案:
1、升級更新buildToolsVersion 到最新版本
2、升級Android Studio 2.2.2版本,JDK 1.8版本
3、配置JAVA_HOME路徑
android { compileSdkVersion 24 buildToolsVersion "24.0.2" defaultConfig { applicationId "com.mycompany.appname" minSdkVersion 17 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" jackOptions { enabled true } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }
然並卵,以上方案並沒有解決Error:Execution failed for task ':app:compileDebugJavaWithJavac' 這個異常
直到不經意間看到一篇部落格以下一段內容
初始一看好像和我遇到的問題並沒有什麼關聯,不過現在沒什麼頭緒就抱著死馬當活馬醫的覺悟嘗試一下
果然,奇蹟並沒有發生,不過仔細看了錯誤資訊
Task 'compileDebug' is ambiguous in root project
分析一下:ambiguous是模稜兩可的意思 再看後面還有一大串compileDebugXXXX,於是靈光一閃
把命令gradlew compileDebug --stacktrace 改成 gradlew compileDebugJavaWithJavac
見證奇蹟的時候到了
最後終於定位了錯誤,原來是使用Android Realm資料庫時給非法資料型別添加了@PrimaryKey註解導致異常。
@PrimaryKey註解說明
①欄位必須是String、 integer、byte、short、 int、long 以及它們的封裝類Byte, Short, Integer, and Long
②使用了該註解之後可以使用copyToRealmOrUpdate()方法,通過主鍵查詢它的物件,如果查詢到了,則更新它,否則新建一個物件來代替。
③使用了該註解將預設設定@index註解
④使用了該註解之後,建立和更新資料將會慢一點,查詢資料會快一點。
@Required
資料不能為null
@Ignore
忽略,即該欄位不被儲存到本地
@Index
為這個欄位新增一個搜尋引擎,這將使插入資料變慢、資料增大,但是查詢會變快。建議在需要優化讀取效能的情況下使用。
雖然Android Studio報的是同一個異常,但是每個人造成異常的根源可能各不相同,掌握解決問題的思路比解決問題的方法更重要。
參考資料: