Android项目通过gradle方式构建过程中问题处理方法

StoneLee 发布于 文章字数: 484 阅读次数:

开发环境设置 与 问题处理方法,基于Android 3.6.x版本

1、要构建成功,首先要修改项目build.gradle文件

1
2
3
4
5
6
7
8
9
10
11
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/'}
maven { url'https://maven.aliyun.com/repository/public/' }
maven { url'https://maven.aliyun.com/repository/google/' }
maven { url'https://maven.aliyun.com/repository/jcenter/' }
maven { url'https://maven.aliyun.com/repository/central/' }
jcenter(){
url "http://jcenter.bintray.com/"
}
google()
}

如上设置,可以解决如下问题:

1、 “Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all artifacts for configuration ‘:classpath’.”

2、“Could not download protobuf-java.jar”

2、build.gradle 配置中的 dependencies部分,compile 过期失效的问题

解决办法:使用implementation或者api代替compile

api和implementation的区别
api:模块的依赖对外公开,可被依赖包所引用(完全等同于compile指令) 

implementation:依赖只作用于当前的module,将该模块的依赖隐藏在内部,而不对外部公开(使用implementation指令的依赖不会传递)

例如:
‘compile’ fileTree(dir: ‘libs’, include: [‘*.jar’])

改为:

implementation fileTree(dir: ‘libs’, include: [‘*.jar’])

解决如下问题:

“Configuration ‘compile’ is obsolete and has been replaced with ‘implementation’ and ‘api’.`”

3、Unfortunately you can’t have non-Gradle Java modules and Android-Gradle modules in one project.

解决办法:

1- close the project

2- close Android Studio IDE

3- delete the .idea directory

4- delete all .iml files

5- open Android Studio IDE and import the project

4、加快gradle的编译速度

解决方法:在当前项目中的gradle.properties文件中进行如下设置

1
2
3
4
org.gradle.daemon=true  // 开启线程守护,第一次编译时开线程,之后就不会再开了
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 // 配置编译时的虚拟机大小
org.gradle.parallel=true // 开启并行编译,相当于多条线程再走
org.gradle.configureondemand=true 启用新的孵化模式

可行的话,也可以在全局gradle.properties文件中进行设置

5、Android Studio Connection refused: connect

解决方法:删除全局gradle.properties文件中的一些代理信息就可以了