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命令的路径有可能不太一样。

 

希望以上能够帮助大家解决问题。

 

相关文章:

  • 2021-12-06
  • 2022-01-16
  • 2021-05-15
  • 2021-05-28
  • 2022-01-12
  • 2022-02-09
  • 2021-10-05
猜你喜欢
  • 2021-12-23
  • 2021-07-26
  • 2021-07-11
  • 2021-06-26
  • 2022-12-23
  • 2021-05-22
  • 2021-11-02
相关资源
相似解决方案