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命令的路径有可能不太一样。
希望以上能够帮助大家解决问题。