Android Studio剛發布,相信很多朋友和我一樣,開始嘗試用其開發項目,但新東西總會遇到這樣或那樣的問題,其中最令我頭的就是引入第三方的jar包無法編譯的問題,因為是新東西,相關的信息都比較少,解決問題令我花費了相當長的時間,為了避免各位同仁再走彎路,在此將解決步驟列出來。
1、將jar包放入項目里的libs文件夾中。
2、在project選中jar包點擊右鍵"Add as library"。
3、這兩步是網上比較容易找到的,但此時項目仍然是無法正常編譯的,會出現:
Gradle: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':ABetone:compileDebug'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Could not execute build using Gradle distribution 'http://services.gradle.org/distributions/gradle-1.6-bin.zip'.
這時需要在項目的build.gradle文件里的dependencies節加入
dependencies { compile files('libs/android-support-v4.jar') compile files('libs/xxxx.jar') }
4、此時項目正常編譯並運行了,但當你的代碼中真正創建了引用jar里的類實例時,有可能系統會拋出異常NoClassDefFoundError,這個時候可以按以下步驟操作:
- 進入命令提示符窗口。
- 定位到項目的根目錄,即build.gradle所在的目錄。
- 運行 "{android studio 安裝目錄}\sdk\tools\templates\gradle\wrapper\gradlew.bat" clean
- 重新編譯運行項目
通過以上操作,應該可以解決問題。PS:我的是Windows系統,Linux及Max系統gradlew命令的路徑有可能不太一樣。
希望以上能夠幫助大家解決問題。