Gradle in Android Studio (3) - 项目中的Gradle

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目录后
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值