Gradle in Android Studio

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

Gradle in Android Studio (3) - 项目中的Gradle_第1张图片

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 in Android Studio (3) - 项目中的Gradle_第2张图片

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

打包多个APK

利用Gradle的灵活性,你可以为同一个项目来配置,创建不同的版本, 默认有debugrelease两种编译类型
build variants
Gradle in Android Studio (3) - 项目中的Gradle_第3张图片

product flavors
Gradle in Android Studio (3) - 项目中的Gradle_第4张图片

代码
Gradle in Android Studio (3) - 项目中的Gradle_第5张图片

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下载报同样的错, 以下解决方案
Gradle in Android Studio (3) - 项目中的Gradle_第6张图片

settings.gradle

include ':app'

当我们创建一个library时

移动mylibrary目录后
Gradle in Android Studio (3) - 项目中的Gradle_第7张图片

更多相关文章

  1. Android分享图片
  2. android JNI处理图片的例子
  3. Gradle 修改生成apk时的文件名
  4. Android 实现apk文件下载并自动安装
  5. Android客户端上传文件到服务器端
  6. Android根据电量变化为不同图片的方法【电池电量提示】
  7. Android 图片旋转(使用Matrix.setRotate方法)
  8. Android Lib Project与Android Project中R文件的区别

随机推荐

  1. Android图形合成和显示系统---基于高通MS
  2. Android(安卓)安全机制
  3. android studio 1.5.1 创建java项目
  4. Android(安卓)布局属性全解(初学者必学)
  5. android 7和android8在user版本下能应用
  6. Android(安卓)ListView 去除底色、选中色
  7. Android系统中设置TextView的行间距(非行
  8. 如何发布你的Android应用程序
  9. Android和Linux kernel发展史
  10. Android开机执行指定shell脚本