一,android studio 使用 gradle 编译 android 项目,刚接触的同学,可能由于,不熟悉 gradle 和gradle plugin 的关系,在首次编译apk时,出现类似“Gradle sync failed: Unknown host ‘services.gradle.org………“等的问题:

该问题出现的原因是,不能科学上网,或者其他的原因导致不能连接到 gradle 官网或者下载超时;根本原因是,自己本地的项目使用的 gradle plugin 和 本地的 gradle 版本不一致导致的,gradle 需要去下载和 android gradle plugin 版本一致的 gradle。

gradle 官网: https://gradle.org/


二,gradle 和 gradle plugin 的关系

gradle下载链接官网


src :gradle 源码
bin :gradle 二进制安装文件
all:包含 源码,二进制文件,文档等
下载不下来的:可以使用我提供的百度网盘的 3.1 和 3.2
3.1
链接: https://pan.baidu.com/s/1pKRKozx 密码: mgc7

3.2
链接: https://pan.baidu.com/s/1qY19YMS 密码: 3sge

gradle 这么多版本,android studio 应该使用那一个版本呢?
gradle plugin 官方解释:

The Android Studio build system is based on Gradle, and the Android plugin for Gradle adds several features that are specific to building Android apps. Although the Android plugin is typically updated in lock-step with Android Studio, the plugin (and the rest of the Gradle system) can run independent of Android Studio and be updated separately.

gradle plugin 官方链接

我们把 android studio 配置为本地 gradle(离线的),而不是android studio 默认配置,每次,新建项目或者从github 等倒入的第三方项目,就不用等待长时间的 gradle sync 了。
以 gradle 3.3 为例:gradle plugin 可以设置为:2.3.0; 2.3.1; 2.3.2; 2.3.3 (这三个测试可以正常编译)

三,android studio 设置离线 gradle



gradle 设置好后,修改 gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip

里的 gradle 版本为自己本地设置的 gradle 版本。

设置 gradle plugin:



该设置也可以直接在脚本文件中修改,作用是一样的。

buildscript {    repositories {        jcenter()    }    dependencies {            classpath 'com.android.tools.build:gradle:2.3.3'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}

这样设置以后,在倒入第三方(github)项目时,手动修改一下,gradle 和 gradle plugin 就可以快速编译,不用等待 漫长的 gradle sync 了。

更多相关文章

  1. Java 字节码编译为Dex,d8比dx更好用!
  2. Android中用Spannable在TextView中给文字加上边框
  3. android中checkbox文字和复选框的间距设置问题
  4. Android(安卓)SDK 2.2 离线安装
  5. android user版本进行编译出现proguard错误混淆
  6. arm-marvell-linux-gnueabi 交叉编译c/c++程序,并移植到android
  7. Android(安卓)反编译apk 到java源码的方法
  8. Android(安卓)add prebuilt lib(*.so) to Android.mk
  9. Android中的通知—Notification

随机推荐

  1. Android新手入门2016(11)--非阻塞对话框Ale
  2. 如何把项目从github上导入到android stud
  3. android在onCreate()方法中获取View的宽
  4. 【Android 进阶】ButterKnife-黄油刀
  5. 对于Android的文件结构介绍
  6. Android 国际化 语言适配
  7. Android 科大讯飞 在线和离线语音听写
  8. android声音播放之SoundPool的应用,让你的
  9. android listview多视图嵌套多视图
  10. Android高可拓展框架Conductor源码解析(2