美文网首页
AndroidStudio中Gradle的使用(基础)|Squi

AndroidStudio中Gradle的使用(基础)|Squi

作者: 跳动的松鼠 | 来源:发表于2017-11-22 11:21 被阅读19次

    前言

    Gradle是基于jvm,基于DSL语法的自动化构建工具。是google引入,替换ant和maven的新工具,其依赖兼容maven和ivy。gradle相比ant,扩展性更好,灵活性更高。

    概述

    Gradle作为一种项目构建的工具,与其它的构建工具一样,主要的职责是将输入源转变成目标产物。相比其它构建工具,其最大优势是借助于插件可以灵活地实现功能十分复杂、用途极其广泛的项目构建。

    Gradle模型

    gradle模型中两个重要的概念:project和module,以往eclipse的ant模型,workspace代表的项目工程空间,project代表的项目。而在gradle中project代表的是一整个工程,module代表是一个应用或一个库。

    gradle结构

    image.png
    1. module下的build.gradle
      app或库的相关配置在此配置

    2. project下的build.gradle
      (1)、工程需要的gradle工具的引用版本、及下载某些引用库的仓库。
      (2)、工程所有module的相关通用配置

    buildscript {
        repositories {
            jcenter()// 告诉project 使用jcenter库
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.5.0'// 告诉project使用android提供的1.5.0版本gradle
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    // 配置所有的工程
    allprojects {
        repositories {
            jcenter()//所有的project都是使用jcenter库
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
    1. project下的settings.gradle
      项目所有的module需要在此声明
      include ':app'

    gradle配置

    //申明这个工程的属性是Android运行app
    //'com.android.library' 库工程。
    apply plugin: 'com.android.application'
    
    //工程的相关配置
    android {
        //编译的sdk版本
        compileSdkVersion 23
        //编译工具的版本
        buildToolsVersion "23.0.3"
        //解决sdk中org.apache.http.*相关包被剔除的问题
        useLibrary 'org.apache.http.legacy'
        //关闭Android Studio的PNG合法性检查的,有些.9的图片可能会出现合法性的问题。用这个可以解决
        aaptOptions.cruncherEnabled = false
        aaptOptions.useNewCruncher = false
    
        //java编译相关配置
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    
        //签名,签名可以防止app被恶意串改,只有签名相同的程序才能升级替换安装
        signingConfigs {
            //正式包需要的keystore相关信息
            myConfig {
                //keystore路径
                storeFile file("build-files/xxxxx.keystore")
                //keystore密码
                storePassword "123456"
                //keystore别名
                keyAlias "xx"
                //别名的密码
                keyPassword "223456"
            }
    
            //调试包keystore的配置
            debugConfig {
                storeFile file("build-files/debug.keystore")
                storePassword "android"
                keyAlias "androiddebugkey"
                keyPassword "android"
            }
        }
    
        //渠道配置,defaultConfig原生配置,可以多渠道 类似flavor_wandoujia {}
        defaultConfig {
            applicationId "com.gradledemo.activity" //配置包名
            minSdkVersion 15 // 最小支持sdk版本
            targetSdkVersion 23 // 目标sdk版本
            versionCode 1 //版本号
            versionName "1.0" //版本名称
    
            //支持multidex 解决65536爆包问题
            //multiDexEnabled true
        }
    
        //表示构建类型,混淆使用,一般有release debug 两种
        buildTypes {
            debug {
                signingConfig signingConfigs.debugConfig
                // 显示Log
                buildConfigField "boolean", "LOG_DEBUG", "true"
                minifyEnabled false
                zipAlignEnabled false
                shrinkResources false
            }
    
            release {
                //是否混淆
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    
        //编译的 lint 开关
        lintOptions {
            // set to true to turn off analysis progress reporting by lint
            quiet true
            // if true, stop the gradle build if errors are found
            abortOnError false
            // if true, only report errors
            ignoreWarnings true
            checkAllWarnings false
            checkReleaseBuilds false
            // lintConfig file("lint.xml")
        }
        //为所有的子项目设置一些通用配置
        subprojects {
            //配置一个新的gradle一样
        }
        //Eclipse 中迁移过来的代码都带这个设置,一般做指定目录
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                aidl.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
                //so库的引用
                jniLibs.srcDirs = ['libs']
            }
            // Move the tests to tests/java, tests/res, etc...
            instrumentTest.setRoot('tests')
            // Move the build types to build-types/<type>
            // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
            // This moves them out of them default location under src/<type>/... which would
            // conflict with src/ being used by the main source set.
            // Adding new build types or product flavors should be accompanied
            // by a similar customization.
            debug.setRoot('build-types/debug')
            release.setRoot('build-types/release')
        }
    }
    
    //编译需要的引用配置
    dependencies {
        //jar引用,fileTree(dir:'libs',include:'*.jar')所有文件目录树下的libs的jar的引用,
        // 剔除*v4的jar引用exclude:'*v4.jar'
        //compile fileTree(dir: 'libs', include: '*.jar', exclude: '*v4.jar')
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.0.1'
    
        //工程引用
        //compile project(':SmartDevice:WiFiControlLibrary')
    }
    

    依赖管理

    jar包

    1. 常规做法
     dependencies {
              compile fileTree(dir: 'libs', include: ['*.jar'])//即添加所有在libs文件夹中的jar
              compile 'com.android.support:appcompat-v7:23.0.1'//这是添加自己依赖jcenter库的jar
     }
    
    1. 引入其他远程仓库
    repositories {
          //远程仓库地址
        maven { url = 'https://dl.bintray.com/yuancloud/maven/' }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1'
        testCompile 'junit:junit:4.12'
        compile 'cn.yuancloud.app:superadapter:1.1'//远程仓库
    }
    

    so库引入

    1. 放入对应的文件夹,系统自动引入:


      image.png
    2. jinLib资源引入在libs中
      这里必须重新设置sourceSets

     sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
            }
     }
    
    1. aar文件
      aar相当于android library的输出库,其中包含资源文件,类似(module),在library工程build/output/aar/下
      如下依赖:
    dependencies {
           compile project(':library名字')
    }
    
    1. 关于sourceSets
      资源设置字段,我们看下默认的配置,开发者当然也可以自定义资源加载的位置
    //Eclipse 中迁移过来的代码都带这个设置,一般做指定目录
        sourceSets {
            main {
                manifest.srcFile 'AndroidManifest.xml'
                java.srcDirs = ['src']
                resources.srcDirs = ['src']
                aidl.srcDirs = ['src']
                renderscript.srcDirs = ['src']
                res.srcDirs = ['res']
                assets.srcDirs = ['assets']
                //so库的引用
                jniLibs.srcDirs = ['libs']
            }
            // Move the tests to tests/java, tests/res, etc...
            instrumentTest.setRoot('tests')
            // Move the build types to build-types/<type>
            // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
            // This moves them out of them default location under src/<type>/... which would
            // conflict with src/ being used by the main source set.
            // Adding new build types or product flavors should be accompanied
            // by a similar customization.
            debug.setRoot('build-types/debug')
            release.setRoot('build-types/release')
        }
    

    以上是根据我的一些理解,做的总结分享,旨在抛砖引玉,希望有更多的志同道合的朋友一起讨论学习,共同进步!

    相关文章

      网友评论

          本文标题:AndroidStudio中Gradle的使用(基础)|Squi

          本文链接:https://www.haomeiwen.com/subject/vvlnvxtx.html