Android Gradle权威指南

Android Gradle权威指南,第1张

概述1.AndroidGradle插件介绍  我们知道Android起始就是Gradle的一个第三方插件,它是由Google的Android开发团队开发的。但从Android的角度来看,Android插件是基于Gradle构建的,和AndroidStudio完美无缝搭配的新一代构建系统。2.AndroidGradle插件分类在Android中有3类工程   1.AndroID Gradle插件介绍

  我们知道AndroID起始就是Gradle的一个第三方插件,它是由Google的AndroID开发团队开发的。但从AndroID的角度来看,AndroID插件是基于Gradle构建的,和AndroIDStudio完美无缝搭配的新一代构建系统。

2.AndroID Gradle插件分类

在AndroID中有3类工程
  1.App应用工程,他可以生成一个可运行的apk应用。
   App插件ID :com.androID.application

  2.library库工程,他可以生成AAR包给其他App工程公用,就和我们的Jar一样,但是它包含了AndroID的资源等信息。
   library插件ID :com.androID.library

  3.Test测试工程,用于对App工程或者library库工程进行单元测试
   Test插件ID :com.androID.test

3.AndroID Gradle工程配置介绍

 

apply plugin: 'com.androID.application'androID {    compileSdkVersion 29              //配置我们编译AndroID工程的SDK,29是AndroIDSDK的API Level    buildToolsversion "29.0.2"        //AndroID构建工具的版本    //默认配置    defaultConfig {        applicationID "com.ych.androIDgradle"  //包名        minSdkVersion 15                       //最低支持的AndroID系统API        targetSdkVersion 29                    //我们基于那个AndroID版本开发的        versionCode 1                          //App应用内部版本号        versionname "1.0"                      //App应用版本名称        testInstrumentationRunner "androIDx.test.runner.AndroIDJUnitRunner"  //用于配置单元测试时使用的Runner        singingConfig singingConfigs.deBUG     //默认配置签名信息        multIDexEnabled true      //是否用于差分多个Dex的功能,也可以单独在buildTypes中进行配置。    }    buildTypes {        release {            MinifyEnabled false                //是否启用混淆  false不启用   true启用            zipAlignEnabled false              //整理优化apk文件工具,提高应用运行效率 false不启用   true启用            shrinkResources false              //是否自动移除未使用的资源  false不启用  true启用            proguardfiles getDefaultProguardfile('proguard-androID-optimize.txt'), 'proguard-rules.pro'    //混淆,所使用的proguard配置文件。            singingConfig singingConfigs.deBUG  //打包时        }    }}dependencIEs {    implementation filetree(dir: 'libs', include: ['*.jar'])    implementation 'androIDx.appcompat:appcompat:1.1.0'    implementation 'androIDx.constraintlayout:constraintlayout:1.1.3'    testImplementation 'junit:junit:4.12'    androIDTestImplementation 'androIDx.test:runner:1.2.0'    androIDTestImplementation 'androIDx.test.espresso:espresso-core:3.2.0'}

AndroID Gradler工程配置都是在androID{}中,这是唯一的一个入口,通过它,可以对AndroID Gradle工程进行自定义配置。

4.自定义AndroID Gradle工程

4.1 singingConfigs:配置默认的签名信息,对生成的APP签名。

 

  signingConfigs {        release {            storefile file('../cartoonapp.jks')    //签名证书文件            storePassword "xxxxxx"               //签名证书文件的密码            keyAlias "xxxxxx"                          //签名证书中密钥别名            keyPassword "xxxxxx"                 //签名证书中密钥密码        }        // 默认情况下deBUG模式签名已经被配置好;自动生成的deBUG证书,一般在$HOME/.androID/deBUG.keystore        deBUG {            storefile file('../cartoonapp.jks')    //签名证书文件            storePassword "xxxxxx"               //签名证书文件的密码            keyAlias "xxxxxx"                          //签名证书中密钥别名            keyPassword "xxxxxx"                 //签名证书中密钥密码        }  }
5.高级自定义AndroID Gradle(动态生成版本信息)

如果只有一个application module还好,如果我们有多个module怎么办呢?每次改版本号累不累?

解决方案:就是在root里申明全局变量,可以在单独的gradle里(比如新建一个config.gradle)
注意:申明然后使用apply from引用进来,或者直接定义在root的build.gradle中。

 

  apply plugin: 'com.androID.application'  apply from: '../config.gradle'    //引入进来  ../表示上一级  androID {      ...  }

5.1 config.gradle文件配置

 

rootProject.ext {        androID = [            compileSdkVersion       : 29,            buildToolsversion       : "29.0.2",            applicationID       : "com.ych.mvpadvance",            minSdkVersion           : 15,            targetSdkVersion        : 29,            versionCode             : 1,            versionname             : "1.0"    ]        //版本号    version = [            androIDSupportSdkVersion: "28.0.0",            retrofitSdkVersion      : "2.6.0",            dagger2SdkVersion       : "2.23.2",            glIDeSdkVersion         : "4.9.0",            butterknifeSdkVersion   : "9.0.0",            rxlifecycleSdkVersion   : "1.0",            rxlifecycle2SdkVersion  : "2.2.2",            espressoSdkVersion      : "3.0.1",            canarySdkVersion        : "1.6.3"    ]    dependencIEs = [            //support            "appcompat-v7"             : "com.androID.support:appcompat-v7:${version["androIDSupportSdkVersion"]}",            "design"                   : "com.androID.support:design:${version["androIDSupportSdkVersion"]}",            "support-v4"               : "com.androID.support:support-v4:${version["androIDSupportSdkVersion"]}",            "cardvIEw-v7"              : "com.androID.support:cardvIEw-v7:${version["androIDSupportSdkVersion"]}",            "annotations"              : "com.androID.support:support-annotations:${version["androIDSupportSdkVersion"]}",            "recyclervIEw-v7"          : "com.androID.support:recyclervIEw-v7:${version["androIDSupportSdkVersion"]}",            //network            "retrofit"                 : "com.squareup.retrofit2:retrofit:${version["retrofitSdkVersion"]}",            "retrofit-converter-gson"  : "com.squareup.retrofit2:converter-gson:${version["retrofitSdkVersion"]}",            "retrofit-adapter-rxjava"  : "com.squareup.retrofit2:adapter-rxjava:${version["retrofitSdkVersion"]}",            "retrofit-adapter-rxjava2" : "com.squareup.retrofit2:adapter-rxjava2:${version["retrofitSdkVersion"]}",            "okhttp3"                  : "com.squareup.okhttp3:okhttp:3.5.0",            "okhttp4"                  : "com.squareup.okhttp3:okhttp:4.0.0",            "okhttp-urlconnection"     : "com.squareup.okhttp:okhttp-urlconnection:2.0.0",            "glIDe"                    : "com.github.bumptech.glIDe:glIDe:${version["glIDeSdkVersion"]}",            "glIDe-compiler"           : "com.github.bumptech.glIDe:compiler:${version["glIDeSdkVersion"]}",            "glIDe-loader-okhttp3"     : "com.github.bumptech.glIDe:okhttp3-integration:${version["glIDeSdkVersion"]}",            "picasso"                  : "com.squareup.picasso:picasso:2.5.2",            //vIEw            "autolayout"               : "com.zhy:autolayout:1.4.5",            "butterknife"              : "com.jakewharton:butterknife:${version["butterknifeSdkVersion"]}",            "butterknife-compiler"     : "com.jakewharton:butterknife-compiler:${version["butterknifeSdkVersion"]}",            "pickervIEw"               : "com.contrarywind:AndroID-PickerVIEw:3.2.5",            "photovIEw"                : "com.github.chrisbanes.photovIEw:library:1.2.3",            "numberprogressbar"        : "com.daimajia.numberprogressbar:library:1.2@aar",            "nineoldandroIDs"          : "com.nineoldandroIDs:library:2.4.0",            "paginate"                 : "com.github.markomilos:paginate:0.5.1",            "vlayout"                  : "com.alibaba.androID:vlayout:1.1.0@aar",            "autosize"                 : "me.jessyan:autosize:1.1.2",            //rx1            "rxandroID"                : "io.reactivex:rxandroID:1.2.1",            "rxjava"                   : "io.reactivex:rxjava:1.3.0",            "rxlifecycle"              : "com.trello:rxlifecycle:${version["rxlifecycleSdkVersion"]}",            "rxlifecycle-components"   : "com.trello:rxlifecycle-components:${version["rxlifecycleSdkVersion"]}",            "rxcache"                  : "com.github.VictorAlbertos.RxCache:runtime:1.7.0-1.x",            "rxcache-jolyglot-gson"    : "com.github.VictorAlbertos.Jolyglot:gson:0.0.4",            "rxbinding-recyclervIEw-v7": "com.jakewharton.rxbinding:rxbinding-recyclervIEw-v7:1.0.1",            "rxpermissions"            : "com.tbruyelle.rxpermissions:rxpermissions:0.9.4@aar",            "rxerrorhandler"           : "me.jessyan:rxerrorhandler:1.0.1",            //rx2            "rxandroID2"               : "io.reactivex.rxjava2:rxandroID:2.1.1",            "rxjava2"                  : "io.reactivex.rxjava2:rxjava:2.2.10",            "rxlifecycle2"             : "com.trello.rxlifecycle2:rxlifecycle:${version["rxlifecycle2SdkVersion"]}",            "rxlifecycle2-androID"     : "com.trello.rxlifecycle2:rxlifecycle-androID:${version["rxlifecycle2SdkVersion"]}",            "rxlifecycle2-components"  : "com.trello.rxlifecycle2:rxlifecycle-components:${version["rxlifecycle2SdkVersion"]}",            "rxcache2"                 : "com.github.VictorAlbertos.RxCache:runtime:1.8.3-2.x",            "rxpermissions2"           : "com.github.tbruyelle:rxpermissions:0.10.2",            "rxerrorhandler2"          : "me.jessyan:rxerrorhandler:2.1.1",            //tools            "dagger2"                  : "com.Google.dagger:dagger:${version["dagger2SdkVersion"]}",            "dagger2-androID"          : "com.Google.dagger:dagger-androID:${version["dagger2SdkVersion"]}",            "dagger2-androID-support"  : "com.Google.dagger:dagger-androID-support:${version["dagger2SdkVersion"]}",            "dagger2-compiler"         : "com.Google.dagger:dagger-compiler:${version["dagger2SdkVersion"]}",            "dagger2-androID-processor": "com.Google.dagger:dagger-androID-processor:${version["dagger2SdkVersion"]}",            "androIDeventbus"          : "org.simple:androIDeventbus:1.0.5.1",            "eventbus"                 : "org.greenrobot:eventbus:3.1.1",            "otto"                     : "com.squareup:otto:1.3.8",            "gson"                     : "com.Google.code.gson:gson:2.8.5",            "multIDex"                 : "com.androID.support:multIDex:1.0.3",            "javax.annotation"         : "javax.annotation:Jsr250-API:1.0",            "arouter"                  : "com.alibaba:arouter-API:1.3.1",            "arouter-compiler"         : "com.alibaba:arouter-compiler:1.1.4",            "progressmanager"          : "me.jessyan:progressmanager:1.5.0",            "retrofit-url-manager"     : "me.jessyan:retrofit-url-manager:1.4.0",            "lifecyclemodel"           : "me.jessyan:lifecyclemodel:1.0.1",            //test            "junit"                    : "junit:junit:4.12",            "androIDJUnitRunner"       : "androID.support.test.runner.AndroIDJUnitRunner",            "runner"                   : "com.androID.support.test:runner:1.0.1",            "espresso-core"            : "com.androID.support.test.espresso:espresso-core:${version["espressoSdkVersion"]}",            "espresso-contrib"         : "com.androID.support.test.espresso:espresso-contrib:${version["espressoSdkVersion"]}",            "espresso-intents"         : "com.androID.support.test.espresso:espresso-intents:${version["espressoSdkVersion"]}",            "mockito-core"             : "org.mockito:mockito-core:1.+",            "timber"                   : "com.jakewharton.timber:timber:4.7.1",            "logger"                   : "com.orhanobut:logger:2.2.0",            "canary-deBUG"             : "com.squareup.leakcanary:leakcanary-androID:${version["canarySdkVersion"]}",            "canary-release"           : "com.squareup.leakcanary:leakcanary-androID-no-op:${version["canarySdkVersion"]}",            "umeng-analytics"          : "com.umeng.analytics:analytics:6.0.1"    ]}

5.2 引用config.gradle文件中配置信息

 

apply plugin: 'com.androID.application'apply from: '../config.gradle'androID {    compileSdkVersion rootProject.ext.androID["compileSdkVersion"]    buildToolsversion rootProject.ext.androID["buildToolsversion"]    defaultConfig {        applicationID rootProject.ext.androID["applicationID"]        minSdkVersion rootProject.ext.androID["minSdkVersion"]        targetSdkVersion rootProject.ext.androID["targetSdkVersion"]        versionCode rootProject.ext.androID["versionCode"]        versionname rootProject.ext.androID["versionname"]        testInstrumentationRunner rootProject.ext.dependencIEs["androIDJUnitRunner"]          //设置支持的SO库架构          ndk {            abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a' ,'x86' , 'x86_64'          }    }    buildTypes {        release {            MinifyEnabled false            proguardfiles getDefaultProguardfile('proguard-androID-optimize.txt'), 'proguard-rules.pro'        }        deBUG {            MinifyEnabled false            proguardfiles getDefaultProguardfile('proguard-androID-optimize.txt'), 'proguard-rules.pro'        }    }}dependencIEs {    implementation filetree(dir: 'libs', include: ['*.jar'])    implementation 'androIDx.appcompat:appcompat:1.1.0'    implementation 'androIDx.constraintlayout:constraintlayout:1.1.3'    testImplementation 'junit:junit:4.12'    androIDTestImplementation 'androIDx.test:runner:1.2.0'    androIDTestImplementation 'androIDx.test.espresso:espresso-core:3.2.0'    //引用    implementation rootProject.ext.dependencIEs.okhttp3}
6.动态配置AndroIDManifest文件

AndroID Gradle中提供了非常便捷的方法让我们来替换AndroIDManifest文件内容,他就是manifestPlaceholder,Manifest占位符,也可以在defaultConfig中配置。

 

  productFlavors {        _xiaomi {           ...           manifestPlaceholders = [                JPUSH_PKGname: applicationID,                JPUSH_APPKEY : rootProject.ext.APIenv == 0 ? rootProject.ext.jpushReleaseKey : rootProject.ext.APIenv == 4 ? rootProject.ext.jpushReleaseKey : rootProject.ext.jpushTestKey, //JPush上注册的包名对应的appkey.  正式                JPUSH_CHANNEL: "developer-default", //暂时填写默认值即可.                JPUSH_APPname: rootProject.ext.APIenv == 0 ? "安路通正式2.1" : rootProject.ext.APIenv == 4 ? "天天快车" : "安路通测试2.1",                BAIDU_APPKEY : rootProject.ext.APIenv == 0 ? rootProject.ext.baIDuReleaseKey : rootProject.ext.APIenv == 4 ? rootProject.ext.baIDuReleaseKey : rootProject.ext.baIDuTestKey,                JPUSH_ICON : rootProject.ext.APIenv == 0 ? "@mipmap/ic_launcher": rootProject.ext.APIenv == 1 ? "@mipmap/ic_launcher": "@drawable/tianhong",         ]     }    }

AndroIDManifest中使用:

 

<application    androID:name=".app.SampleApplication"    androID:allowBackup="true"    androID:icon="${JPUSH_ICON}"    androID:label="${JPUSH_APPname}"    androID:largeHeap="true"    androID:supportsRtl="true"    androID:theme="@style/Apptheme">        <!-- required. For publish channel feature -->    <!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。 -->    <!-- 例如: -->    <!-- 发到 Google Play 的APK可以设置为 Google-play; -->    <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->    <!-- 目前这个渠道统计功能的报表还未开放。 -->    <Meta-data        androID:name="JPUSH_CHANNEL"        androID:value="${JPUSH_CHANNEL}" />    <!-- required. AppKey copIEd from Portal -->    <Meta-data        androID:name="JPUSH_APPKEY"        androID:value="${JPUSH_APPKEY}" /></application>
7.自定义你的BuildConfig

 

  //默认的BuildConfig  public final class BuildConfig {      public static final boolean DEBUG = Boolean.parseBoolean("true");      public static final String APPliCATION_ID = "com.ych.androIDgradle";      public static final String BUILD_TYPE = "deBUG";      public static final String FLAVOR = "";      public static final int VERSION_CODE = 1;      public static final String VERSION_name = "1.0";  }

假设我们有 BaIDu 和 Google 两个渠道,发布的时候也会有两个渠道。当我们安装BaIDu渠道包的时候打开的 是BaIDu首页,当安装Google渠道包的时候,打开的是Google的首页。我们只需要添加一个字段(WEB_URL),字段名自己定义。

 

androID {    compileSdkVersion 29    buildToolsversion "29.0.2"    defaultConfig {        applicationID "com.ych.androIDgradle"        minSdkVersion 15        targetSdkVersion 29        versionCode 1        versionname "1.0"        testInstrumentationRunner "androIDx.test.runner.AndroIDJUnitRunner"    }        buildTypes {        release {            MinifyEnabled false            proguardfiles getDefaultProguardfile('proguard-androID-optimize.txt'), 'proguard-rules.pro'        }    }    productFlavors {        Google {            buildConfigFIEld "String", "WEB_URL", ' "http://www.Google.com " '            buildConfigFIEld "int", "API_ENV", ' "1 " '            buildConfigFIEld "boole", "API_ENV", ' "1 " '        }        baIDu {            buildConfigFIEld "String", "WEB_URL",  ' "http://www.baIDu.com " '            buildConfigFIEld "int", "API_ENV", ' "2" '        }    }}
8.签名管理(非常重要)

签名是一个很敏感的东西,只要有了签名文件和对应的密码信息,就能轻易反编译修改源码然后再签名进行发布,因此如何保存这些敏感信息是很重要的。

在我的个人实践中,主要做了这么几点:

8.1 在local.propertIEs文件下定义keystore信息文件路径:

 

sdk.dir=D\:\Develop\SDKkeystore.props.file=../keystore.propertIEs

8.2 keystore.propertIEs保存keystore信息:
store=../buildsystem/alt.keystore
alias=xxx
pass=xxx
storePass=xxx

8.3 buildsystem下保存了:
文件加下放入我们的签名文件 xxx.keystore/ xxx.jks
deBUG.keystore
release.keystore

**8.4 application module的signingConfigs:

 

  signingConfigs {        def PropertIEs localProps = new PropertIEs()        localProps.load(new fileinputStream(file('../local.propertIEs')))        def PropertIEs keyProps = new PropertIEs()        // 如果读取不到'keystore.props.file'属性,就使用deBUG keystore        if (localProps['keystore.props.file']) {            keyProps.load(new fileinputStream(file(localProps['keystore.props.file'])))        } else {            keyProps["store"] = '../buildsystem/deBUG.keystore'            keyProps["alias"] = 'androID'            keyProps["storePass"] = 'androIDdeBUGkey'            keyProps["pass"] = 'androID'        }        deBUG {            storefile file(keyProps["store"])            keyAlias keyProps["alias"]            storePassword keyProps["storePass"]            keyPassword keyProps["pass"]        }        release {            // release版本使用assert确保存在该属性否则报错,避免错误打包            assert localProps['keystore.props.file'];            storefile file(keyProps["store"])            keyAlias keyProps["alias"]            storePassword keyProps["storePass"]            keyPassword keyProps["pass"]        }    }
9.总结

  本篇主要讲了开发阶段gradle的各种实践,希望可以帮到大家。



作者:如愿以偿丶
链接:https://www.jianshu.com/p/6c1fcf251623
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

总结

以上是内存溢出为你收集整理的Android Gradle权威指南全部内容,希望文章能够帮你解决Android Gradle权威指南所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1012212.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存