Gradle in Android Studio

转载请注明出处 : http://blog.csdn.net/hpu_zyh/article/details/48447539
博客主页 | 简书 | 知乎 | 微博 | github

Gradle (谷瑞豆) 官网, 点击上面图片看Google官方视频 Introducing Gradle (Ep 2, Android Studio) in Youtube
来自Gradle的hello world

Android Studio中的Gradle

当创建一个项目后,Android studio 会自动创建以下的目录

一般情况下,我们只修改app moudle下的build.gradle即可满足使用

android studio 目录结构

库管理机制

当创建一个项目时,生成以下的基本信息: app/build.gradle

apply plugin: 'com.android.application'android {    compileSdkVersion 22             // 这里需要按照自己本机下载的版本填写, 如果本机没有下载,会报错    buildToolsVersion "23.0.0 rc2"   // Error:failed to find target android-22 : E:\android-sdk     defaultConfig {        applicationId "hanks.com.myapplication" //应用包名        minSdkVersion 15                                targetSdkVersion 22                             versionCode 1        versionName "1.0"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}dependencies { //库依赖    compile fileTree(dir: 'libs', include: ['*.jar']) //引用本地的libs目录下的所以jar包    compile 'com.android.support:appcompat-v7:22.2.0'}

当需要添加一些第三方库时, 直接在dependencies添加引用

dependencies {    // Google Play Services    compile 'com.google.android.gms:play-services:7.8.0'    // Support Libraries    compile 'com.android.support:appcompat-v7:23.0.0'    compile 'com.android.support:cardview-v7:23.0.0'    compile 'com.android.support:design:23.0.0'    compile 'com.android.support:gridlayout-v7:23.0.0'    compile 'com.android.support:leanback-v17:23.0.0'    compile 'com.android.support:mediarouter-v7:23.0.0'    compile 'com.android.support:palette-v7:23.0.0'    compile 'com.android.support:recyclerview-v7:23.0.0'    compile 'com.android.support:support-annotations:23.0.0'    compile 'com.android.support:support-v13:23.0.0'    compile 'com.android.support:support-v4:23.0.0'    // Note: these libraries require the "Google Repository" and "Android Repository"    // to be installed via the SDK manager.}

添加引用后,Android Studio会提示我们Sync, 然后会自动从远程库中下载我们引用的库文件,本地已经缓存过的话会直接引用本地的,而不用下载

compile files('libs/gson-2.3.1.jar')                //引用单个jar-------------------------------------------------------------------------------compile fileTree(include: ['*.jar'], dir: 'libs')   // 引用libs下的全部jar-------------------------------------------------------------------------------compile project(':library:mylibrary')               // 引用library目录下的mylibrary

查找Gradle依赖库的网站

可以通过Android Studio来添加引用, 比如引用 recyclerview-v7

使用Gradle来管理库,比以前引用.jar(只包含Java Code)方便的多,也不用到处复制库文件, 并且直接使用Gradle可以直接引用aar(包含库文件的Java Code, Resource, Assets, AndroidMenifest.xml)

打包多个APK

利用Gradle的灵活性,你可以为同一个项目来配置,创建不同的版本, 默认有debugrelease两种编译类型
build variants

product flavors

代码

MyApplication/build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:1.2.3'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}allprojects {    repositories {        jcenter()    }}

jcenter远程仓库默认是https的,当使用代理时可能会在下载远程库时出现Error:Cause: peer not authenticated, 解决方案是 使用http代替 https

buildscript {    repositories {        jcenter{            url "http://jcenter.bintray.com"        }    }    dependencies {        classpath 'com.android.tools.build:gradle:1.2.3'        // NOTE: Do not place your application dependencies here; they belong        // in the individual module build.gradle files    }}

gradle下载报同样的错, 以下解决方案

settings.gradle

include ':app'

当我们创建一个library时

移动mylibrary目录后

更多相关文章

  1. Android日语输入法Simeji使用示例
  2. Android(安卓)使用HTTP(get和post)方式登陆服务器
  3. android自动更新软件版本
  4. Android(安卓)实现apk文件下载并自动安装
  5. android使用Dialog跳转到Activity
  6. android 重力传感器的使用
  7. 箭头函数的基础使用
  8. NPM 和webpack 的基础使用
  9. Python list sort方法的具体使用

随机推荐

  1. Android(安卓)3D 游戏学习笔记(4)-光源
  2. Android:退出程序后保持Serivce开启不关
  3. 从HandlerThread 的使用来分析HandlerThr
  4. Android(安卓)JNI中记录log
  5. windows 下 android 使用ant自动打包
  6. Android(安卓)Studio无法单点调试Connect
  7. 在Ubuntu(Linux)上安装Android(安卓)Studio
  8. .NET跨平台开发之Xamarin.Android介绍与
  9. 解决国内android sdk无法更新,google不能
  10. 【Android】数据存储之Shared Preference