美文网首页Android studio
Android Studio中Module的build.grad

Android Studio中Module的build.grad

作者: QiuJay | 来源:发表于2016-05-23 20:28 被阅读1149次

    Gradle版本

    我们在进行采用AS进行安卓项目的开发时,就必须接触gradle,它是目前最新潮的一个构建项目的一个系统,而google也与时俱进,用着最新的东西,顺便说下,eclipse之前是用Ant来构建项目的,还有一个用的比较多的就是maven。而AS的gradle说到版本时,可能有两种:第一是gradle程序包的版本,另外一个是AS中采用gradle插件的版本,这个大家要注意下

    默认内容

    apply plugin

    //老版本的gradle是直接写'application',由于太老了,此教程不考虑
    apply plugin: 'com.android.application'//这表示此module是一个可运行的应用程序,可以直接run的
    
    apply plugin: 'com.android.library'//这表示此module是一个安卓依赖库的工程,不可直接run
    
    apply plugin: 'java' //这表示此module是一个java项目,在此module中只能使用java的api
    }
    

    android

    android {//android此语法只有当你的apply plugin: 'com.android.xxx'时才可以用
        compileSdkVersion 23 //开发时采用的sdk版本
        buildToolsVersion "23.0.3" //编译时采用的编译工具版本
    }
    

    android#defaultConfig

    android {
        ......
        defaultConfig {//默认项目配置信息
            applicationId "com.qiu.jay" //应用的id,这里决定应用的唯一标识。
            minSdkVersion 9 //决定此应用最低兼容的安卓系统版本
            targetSdkVersion 23 //决定此应用最高可兼容的安卓系统版本
            versionCode 1 //应用的版本号
            versionName "1.0" //应用的版本名
        }
        ......
    }
    

    android#buildTypes

    android {
        ......
        buildTypes {//构建的类型方式
                release { // release方式,一般这种是定义为正式包渠道
                    minifyEnabled false //编译时是否混淆的开关
                    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//混淆文件的路径,注意这里用的是proguardFiles,是后面带s的,所以可以添加多个混淆文件路径,每个之间用“,“号隔开
        }
        ......
    }
    

    dependencies

    dependencies {
        compile fileTree(xxx) //编译的文件树
        testCompile 'xxxx' //测试项目需要引用的
        compile 'xxxx' //正常项目需要引用的
    }
    

    拓展写法

    android#signingConfigs

    android {
        ......
        signingConfigs {//签名配置
            config { //此 config 名称是自定义的
                keyAlias '指定签名文件的别名'
                keyPassword '指定签名文件的别名配对密码'
                storeFile file('签名文件路径')
                storePassword '签名文件的密码'
            }
        }
        ......
    }
    

    android#defaultConfig

    android{
        ......
        defaultConfig {
            ......
            testApplicationId '测试工程的application id'
            testInstrumentationRunner '测试工程'
            signingConfig signingConfigs.config //配置默认签名配置
            proguardFile '默认混淆文件配置'
            proguardFiles '默认多个混淆文件配置'
        }
        ......
    }
    

    ### android#buildTypes

    android{
        ......
        buildTypes{
            release{
                ......
            }
            custom{
                ......
            }
            debug{//debug,默认包含此类型,参数都是默认的
            //这里就以debug为例,这里的语法在release和自定义中也是支持的
                debuggable true //默认false,是否开启断点调试
                jniDebuggable true //默认false,是否开启jni的断点调试
                signingConfig signingConfigs.config //配置签名方式,这里配置会覆盖defaultConfig中配置的签名方式
                renderscriptDebuggable true //默认false,是否开启 renderscript 断点调试
                renderscriptOptimLevel 2 //renderscript的版本号
                minifyEnabled true //默认false,是否混淆的开关
                pseudoLocalesEnabled true //默认false,是否支持本地化整理
                applicationIdSuffix '.test' //applicationId的后缀,相当于更改了applicationId,可以认为是一个新的应用
                versionNameSuffix '.520' //版本名的后缀
                zipAlignEnabled false //默认true,是否进行zip align优化
                
                //--------------------
                buildConfigField "boolean", "IS_DEBUG", "true"//为BuildConfig类创建一个常量,类型为boolean,变量名为IS_DEBUG,值为true
                resValue "string", "name", "value" //在strings.xml中创建一个名为name,值为value的一个字符串资源
            }
        }
        ......
    }
    

    以上是一些基本的写法,如果大家想知道其它的一些实现,可直接留言,知道或不知道我都会回答大家的

    相关文章

      网友评论

      本文标题:Android Studio中Module的build.grad

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