Android之gradle。

申明此文系掘金小路飞引进,多为雷同处。

从github或者网上搜索到自己需要的案例,下载下来并且快速运行起来是Android开发过程中最基本的生活动作,这牵扯到Android studio中gradle的相关认识了。

AS项目中有两种gradle文件:

1.根目录的 build.gradle 文件

[创建项目自动生成] [ 主要配置插件和默认依赖库的仓库]

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

2.moudle 中根目录的 build.gradle 文件

Android Studio这个IDE不同于eclipse,eclipse启动打开的是工作空间,一个工作空间下可以包括零个多个工程项目。而Android Studio则是单个工程项目启动,一个工程项目中包括一个或多个module,其中有一个build.gradle文件中头部为apply plugin:'com.android.application'标识的为应用程序module,其余以apply plugin:'com.android.library'标识为Android Library。在编译的时候,所有module的build.gradle依赖的类库都会合并为一个。

【主程序中的】

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion '23.0.2'

    defaultConfig {
        multiDexEnabled true
        applicationId "com.sgcc.pda"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 4
        versionName "01.00.00.01"
    }
    buildTypes {
        debug{
//            buildConfigField "boolean", "UseOkHttp", "true"
            buildConfigField "boolean", "ISDEBUG", "true"
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//            buildConfigField "boolean", "UseOkHttp", "false"
            buildConfigField "boolean", "ISDEBUG", "false"
        }
    }

    //LoadingProgress中的 ivProgressSpinner.setImageResource(R.anim.loading_anim);修改好打包时,要移除
    lintOptions {
        disable "ResourceType"
    }

    //指定JDK1.7
//    compileOptions {
//        sourceCompatibility org.gradle.api.JavaVersion.VERSION_1_7
//        targetCompatibility org.gradle.api.JavaVersion.VERSION_1_7
//    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.0.1'
    //googlesupport    compile 'com.android.support:design:25.0.1'
    //数据库框架
    compile project(':xutils')
    //sdk框架
    compile project(':sdk')
    //日志
    //    compile files('libs/android-logging-log4j-1.0.3.jar')
    //日志
    //    compile files('libs/log4j-1.2.17.jar')
    //注解框架
    compile files('libs/butterknife-7.0.1.jar')
    //拍照
    compile project(':imageselect')
    //凯立德地图
    compile files('libs/cldnavimapsdk_v1.3.5.jar')
    //凯立德地图依赖讯飞包
    compile files('libs/Msc.jar')
    //photoview
    compile 'com.uwetrottmann.photoview:library:1.2.4'
    //硬件设备
    compile project(':hardware')
    //aidl
    compile project(':cepriaidl')
    //andfix
    //    compile 'com.alipay.euler:andfix:0.5.0@aar'
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:design:25.0.1'
}
【Android Library中的】

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile project(':sdk')
//    compile 'org.greenrobot:eventbus:3.0.0'
}
【另一个

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile project(':sdk')
}

sdk版本设置规则

minSdkVersion <= targetSdkVersion <= compileSdkVersion
minSdkVersion <= buildToolsVersion <= compileSdkVersion版本这块恰到好处是最好的。

gradle中的规章制度

1.全局变量的使用

不知道你有没有注意到,在多个module的情况下,不同module的build.gradle文件中有部分配置项类似,或者依赖的类库,有部分是相同的,在维护上不是很方便,这个时候就可以考虑统一配置。

项目根目录的build.gradle文件中添加以下代码和android{}同级

ext {
    //全局变量控制,可在module中的build.gradle文件通过rootProject.ext.xxx开头来使用
    compileSdkVersion = 24
    buildToolsVersion = '24.0.3'
    supportVersion = '24.2.1'
    //主程序版本
    targetSdkVersion = 24
    minSdkVersion = 15
    versionCode = 1
    versionName = "v1.0.0"
    //library版本
    jjdxm_minSdkVersion = 9
    jjdxm_versionCode = 1
    jjdxm_versionName = "v1.0.0"
    jjdxm_v4 = 'com.android.support:support-v4:'+supportVersion
    jjdxm_v7 = 'com.android.support:appcompat-v7:'+supportVersion
    jjdxm_design = 'com.android.support:design:'+supportVersion
    jjdxm_cardview = 'com.android.support:cardview:'+supportVersion
    jjdxm_recyclerview = 'com.android.support:recyclerview-v7:'+supportVersion
}

全局变量控制,可在module中的build.gradle文件通过rootProject.ext.xxx开头来使用,在所有module的build.gradle文件中配置以上变量,以后在维护项目的时候,只需要在根目录这个文件中修改对应的配置项即可。

通过上面的全局配置,只需要在所有的module中配置上rootProject.ext.jjdxm_v7就都统一了不仅解决了冲突问题,而且可以随意切换不同版本的v7类库。

2.配置打包用的签名

在分享、授权登录功能方面,如微信或微博的分享和授权登录提供SDK,只有在指定的签名下才能生效。而我们平是开发都习惯默认的Androidkeystore打包签名。

这个时候要是测试分享或授权登录的功能就要手动去打包指定keystore的签名。

为提升效率可通过配置gradle,根据release或者是debug打包指定签名。

根目录新建一个签名用到的密码管理文件 signing.properties

signing.alias=dou361    #release
signing.password=dou361    #release
signing.jjdxm_alias=dou361   #debug
signing.jjdxm_password=dou361  #debug

在主程序build.gradle的apply plugin: 'com.android.application'下面添加

Properties props = new Properties()
props.load(new FileInputStream(file(rootProject.file("signing.properties"))))

在android{}节点里面添加

signingConfigs {
    release {
        keyAlias props['signing.alias']
        keyPassword props['signing.password']
        storeFile file(rootProject.file("debug.keystore"))
        storePassword props['signing.password']
    }

    debug {
        keyAlias props['signing.jjdxm_alias']
        keyPassword props['signing.jjdxm_password']
        storeFile file(rootProject.file("debug.keystore"))
        storePassword props['signing.jjdxm_password']
    }
}
buildTypes {
    debug {
        signingConfig signingConfigs.debug
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    release {
        signingConfig signingConfigs.release
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

最后所有文件如下
根目录build.gradle

根目录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:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

ext {
    //全局变量控制,可在module中的build.gradle文件通过rootProject.ext.xxx开头来使用
    compileSdkVersion = 24
    buildToolsVersion = '24.0.3'
    supportVersion = '24.2.1'
    //主程序版本
    targetSdkVersion = 24
    minSdkVersion = 15
    versionCode = 1
    versionName = "v1.0.0"
    //library版本
    jjdxm_minSdkVersion = 9
    jjdxm_versionCode = 1
    jjdxm_versionName = "v1.0.0"
    jjdxm_v4 = 'com.android.support:support-v4:'+supportVersion
    jjdxm_v7 = 'com.android.support:appcompat-v7:'+supportVersion
    jjdxm_design = 'com.android.support:design:'+supportVersion
    jjdxm_cardview = 'com.android.support:cardview:'+supportVersion
    jjdxm_recyclerview = 'com.android.support:recyclerview-v7:'+supportVersion
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

主程序的build.gradle

apply plugin: 'com.android.application'

Properties props = new Properties()
props.load(new FileInputStream(file(rootProject.file("signing.properties"))))

android {
    signingConfigs {
        release {
            keyAlias props['signing.alias']
            keyPassword props['signing.password']
            storeFile file(rootProject.file("debug.keystore"))
            storePassword props['signing.password']
        }

        debug {
            keyAlias props['signing.jjdxm_alias']
            keyPassword props['signing.jjdxm_password']
            storeFile file(rootProject.file("debug.keystore"))
            storePassword props['signing.jjdxm_password']
        }
    }
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "com.dou361.demogradle"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        multiDexEnabled true
        ndk {
            // 指定要ndk需要兼容的架构(这样其他依赖包里mips,x86,armeabi,arm-v8之类的so会被过滤掉)
            abiFilters "armeabi-v7a", "armeabi"
        }
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories { flatDir { dirs 'libs' } }

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile rootProject.ext.jjdxm_v7
    compile rootProject.ext.jjdxm_design
}

其他module的build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        minSdkVersion rootProject.ext.jjdxm_minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.jjdxm_versionCode
        versionName rootProject.ext.jjdxm_versionName

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testCompile 'junit:junit:4.12'
    compile rootProject.ext.jjdxm_v7
    compile rootProject.ext.jjdxm_design
}

keystore密码管理文件signing.properties

signing.alias=dou361
signing.password=dou361
signing.jjdxm_alias=dou361
signing.jjdxm_password=dou361

github上项目下载运行

之所以先介绍gradle的一些配置,是因为一个AS项目的启动基本上都是在根据gradle文件里面的配置去联网同步内容下来的。先了解gradle的一些配置和具体的用法以后会比较容易理解。其中类库内容同步失败、架包冲突、sdk版本等问题都可能会导致项目运行不起来。所以从github上面下载下来的项目,不要着急立马打开项目,要是网络不好你会发现一只卡在同步中的界面,这个是因为不同开发者环境不尽相同导致的。


既然开发环境不同,那就可以考虑修改为自己已有的环境,可以省去大部分联网同步操作,在网络差的情况是很明智的一个举动,主要有以下几个步骤:

1.先修改根目录的build.gradle配置

根目录里面无非就一个gradle版本的差异,修改为你当前已有的版本,例如下载的demo是

classpath 'com.android.tools.build:gradle:2.2.3'

版本号2.2.3,而你的环境只有2.1.0的,那就可以修改为

classpath 'com.android.tools.build:gradle:2.1.0'

版本跨越不大的情况下,可以采用以上做法,如果有部分方法不可用,则还是建议联网下载,一劳永逸。

2.module目录中的build.gradle配置

大部分module都会依赖到v4 v7等一些support家族的类库,这个时候有两种情况会导致出问题,一是module之间的v4包版本号不一致,二是v4版本你的环境没有。
针对第一种情况,导入别人的module到你的项目中经常会遇到,解决办法是参考上面的配置全局的依赖或者移除重复的;
针对第二种情况,打开别人的项目经常会遇到,解决办法是修为你环境已有的版本或者联网同步下载。

修改完所有module的build.gradle文件以后,这个时候在打开项目,有个别不存在类库会同步去下载,完成以后基本上都是可以运行起来的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值