




已阅读5页,还剩15页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android 开发必备知识:我和 Gradle 有个约会 腾讯 Bugly 特约作者:霍丙乾 0、讲个故事 0.1 Ant,我还真以为你是只蚂蚁 真正开始近距离接触编程其实是在 2012 年,年底的时候带我的大哥说,咱们这个 app 发 布的时候手动构建耗时太久,研究一下 ant 脚本吧。 那个时候连 HashMap 都不知道是啥,可想开发经验几乎为零,一个小小的 ant 脚本看 得我真是深深地感受到了这个世界充满的恶意。好在后来硬着头皮搞明白了什么 target 之类的鬼东西,不然就没有然后了。 0.2 Maven,你们真的会读这个单词么 Maven /meivn/ 接触 Maven,完全是因为读陈雄华的Spring 实战,他的源码居然是用 Maven 构建 的,结果 Spring 学得一塌糊涂, Maven 我倒是用顺手了。 跟 Ant 一样, Maven 可以用来构建 Java 工程;跟 Ant 一样,Maven 的配置用 xml 来描述;但,Maven 可以管理依赖,它可以让你做到“想要什么,就是一句话的事儿”。比 如我想要个 gson,Maven 说可以,你记下来我带会儿构建的时候给你去取。 com.google.code.gson gson 2.4 真是让你当大爷呢。不过,Maven 这家伙学起来有点儿费劲,很多初学的时候在搭建环境 的时候就被搞死了 你以为是因为 Maven 的学习曲线陡峭吗?当然不是,是因为当初 Maven 的中央仓库被 x 了,所以你就天天看着 cannot resovle dependencies 玩就好了。 后来 OSChina 傍上了阿里这 个爸爸,就有了 。我去年找工作落定之 后,想着做点儿什么的时候,发现 估计被阿里爸爸关禁闭,死了几天, 现在又活过来了。那又怎样呢,反正中央仓库被 x 的事情也已经成为过去。 0.3 Gradle,你爹是不是 Google! 13 年的时候,我兴奋地跟前面提到的大哥说 Maven 是个好同志的时候,大哥说, Google 推荐用 Gradle。所以,我想 Gradle,你爹是不是 Google。或者至少是个干 爹吧。 其实这都不重要了,毕竟 Gradle 实在是好用。比起前面两位的 xml 配置的手段,直接 用代码的方式上阵必然是灵活得多。不仅如此,Gradle 居然可以使用 Maven 仓库来管理 依赖,就像是一个简易版的 Maven 一样,如果不是看不到 pom 文件,你都还以为你仍 然在使用 Maven(当然,由于你在用 Maven 的仓库,所以你自然也是离不开 Maven 的) 。哦,你是 Ant 用户啊,那也没关系啊,不信你看: task helloTAS properties.load(instr) def sdkDirProp = properties.getProperty(sdk.dir) if (sdkDirProp != null) androidSdkDir = new File(sdkDirProp) else sdkDirProp = properties.getProperty(android.dir) if (sdkDirProp != null) androidSdkDir = new File(rootDir, sdkDirProp) isPlatformSdk = true else throw new RuntimeException( “No sdk.dir property defined in perties file.“) def ndkDirProp = properties.getProperty(ndk.dir) if (ndkDirProp != null) androidNdkDir = new File(ndkDirProp) else String envVar = System.getenv(“ANDROID_HOME“) if (envVar != null) androidSdkDir = new File(envVar) else String property = System.getProperty(“android.home“) if (property != null) androidSdkDir = new File(property) envVar = System.getenv(“ANDROID_NDK_HOME“) if (envVar != null) androidNdkDir = new File(envVar) BasePlugin.groovy,通过这 两个方法,我们可以在 gradle 脚本当中获取 sdk 和 ndk 的路径 File getSdkDirectory() return sdk.sdkDirectory File getNdkDirectory() return sdk.ndkDirectory 例如: task hello com.google.code.gson gson 2.4 Maven 就凭这么几句配置就可以帮你搞定 gson-2.4.jar,不仅如此,它还会按照你的设置 帮你把 javadoc 和 source 搞定。妈妈再也不用担心我看不到构件的源码了。 那么这个神奇的 Maven 仓库在哪儿呢? Maven Central,中央仓库,是 Maven 仓库的 鼻祖,其他的大多数仓库都会对它进行代理,同时根据需求添加自己的特色库房。简单说 几个概念: 代理仓库:要租房,去搜房网啊。你要去驾校报名,我是驾校代理,你找我,我去 找驾校。具体到这里,还有点儿不一样,一旦有人从代理仓库下载过一次特定得构 件,那么这个构件会被代理仓库缓存起来,以后就不需要找被代理的仓库下载了。 私有仓库:中国特色社会主义。走自己的路,你管我啊?公司内部的仓库里面有几 个 hosted 的仓库,这些仓库就是我们公司内部特有的,里面的构件也是我们自己 内部的同事上传以后供团队开发使用的。 本地仓库:大隐隐于市。跟代理仓库的道理很像,只不过,这个仓库是存放在你自 己的硬盘上的。 说起来,Andoid sdk 下面有个 extra 目录,里面的很多依 赖也是以 Maven 仓库的形式组 织的。不过这是 Google 特色嘛,人家牛到不往 Maven 的中央仓库上传,真是没辙。 1.2.2 SourceSets 源码集,这里面主要包含你的各种类型的代码的路径,比如 src/main/java 等等。 1.2.3 Properties 前面我们其实也稍稍有提到,这个 properties 其实是 gradle 的属性,在 gradle 源码当 中,我们找到 Project.java 这个接口,可以看到: /* * Determines if this project has the given property. See here for details of the * properties which are available for a project. * * param propertyName The name of the property to locate. * return True if this project has the given property, false otherwise. */ boolean hasProperty(String propertyName); /* * Returns the properties of this project. See here for details of the properties which * are available for a project. * * return A map from property name to value. */ Map getProperties(); /* * Returns the value of the given property. This method locates a property as follows: * * * * If this project object has a property with the given name, return the value of the property. * * If this project has an extension with the given name, return the extension. * * If this projects convention object has a property with the given name, return the value of the * property. * * If this project has an extra property with the given name, return the value of the property. * * If this project has a task with the given name, return the task. * * Search up through this projects ancestor projects for a convention property or extra property with the * given name. * * If not found, a link MissingPropertyException is thrown. * * * * param propertyName The name of the property. * return The value of the property, possibly null. * throws MissingPropertyException When the given property is unknown. */ Object property(String propertyName) throws MissingPropertyException; /* * Sets a property of this project. This method searches for a property with the given name in the following * locations, and sets the property on the first location where it finds the property. * * * * The project object itself. For example, the rootDir project property. * * The projects link Convention object. For example, the srcRootName java plugin * property. * * The projects extra properties. * * * * If the property is not found, a link groovy.lang.MissingPropertyException is thrown. * * param name The name of the property * param value The value of the property */ void setProperty(String name, Object value) throws MissingPropertyException; 不难知道,properties 其实就是一个 map,我们可以在 perties 当中定义属性, 也可以通过 gradle 脚本来定义: setProperty(hello, Hello Tas again!) 使用方法我们前面已经提到,这里就不多说了。 1.2.4 Project 和 Task 如果你用过 ant,那么 project 基本上类似于 ant 的 project 标签,task 则类似于 ant 的 target 标签。我们在 build.gradle 当中编写的 task hello Application DataGnuPGsecring.gpg # Windows Vista and Windows 7 # signing.secretKeyRingFile=C:UsersAppDataRoaminggnupgsecring.g pg projectName=你的构件名称 group=你的构件 groupid artifactId=你的构件 artifactid # 版本号,采用三位数字的形式,如果是非稳定版本,请务必添加 SNAPSHOT version=0.0.1-SNAPSHOT build.gradle apply plugin: com.android.library apply plugin: maven apply plugin: signing android compileSdkVersion 21 buildToolsVersion “21.1.2“ defaultConfig minSdkVersion 17 targetSdkVersion 21 versionCode 1 versionName “0.2“ buildTypes release minifyEnabled false proguardFiles getDefaultProguardFile(proguard-android.txt), proguard- dependencies compile fileTree(include: *.jar, dir: libs) def isSnapshot = version.endsWith(-SNAPSHOT) def sonatypeRepositoryUrl if(isSnapshot) sonatypeRepositoryUrl = “/nexus/content/repositories/thirdparty-snapshots/“ else sonatypeRepositoryUrl = “/nexus/content/repositories/thirdparty/“ sourceSets main java srcDir src/main/java task sourcesJar(type: Jar) from sourceSets.main.allSource classifier = sources artifacts /archives javadocJar archives sourcesJar signing if(project.hasProperty(signing.keyId) & project.hasProperty(signing.password) & project.hasProperty(signing.secretKeyRingFile) sign configurations.archives else println “Signing information missing/incomplete for $“ uploadArchives repositories mavenDeployer if(project.hasProperty(preferedRepo) & project.hasProperty(preferedUsername) & project.hasProperty(preferedPassword) configuration = configurations.archives repository(url: preferedRepo) authentication(userName: preferedUsername, password: preferedPassword) else if(project.hasProperty(sonatypeUsername) & project.hasProperty(sonatypePassword) beforeDeployment MavenDeployment deployment - signing.signPom(deployment) repository(url: sonatypeRepositoryUrl) authentication(userName: sonatypeUsername, password: sonatypePassword) else println “Settings sonatypeUsername/sonatypePassword missing/incomplete for $“ pom.artifactId = artifactId ject name projectName packaging aar developers developer id wecar name wecar 然后运行 gradle uploadArchives 就可以将打包的 aar 发布到公司的 Maven 仓库当中了。 jar 包的方式类似,这里就不在列出了。 2.3 使用 Maven 命令 这个可以通过 mvn 在 cmdline 直接发布构件,命令使用说明: mvn deploy:deploy-file -Durl=file:/C:m2-repo -DrepositoryId=some.id -Dfile=your-artifact-1.0.jar -DpomFile=your-pom.xml -DgroupId=org.some.group -DartifactId=your-artifact -Dversion=1.0 -Dpackaging=jar -Dclassifier=test -DgeneratePom=true -DgeneratePom.description=“My Project Description“ -DrepositoryLayout=legacy -DuniqueVersion=false 当然这里仍然有个认证的问题,我们需要首先在 maven 的 settings 配置当中加入: M rdm rdm 然后我们就可以使用命令上传了: mvn deploy:deploy-file -DgroupId=com.tencent.test -DartifactId=test -Dversion=1.0.0 -Dpackaging=aar -Dfile=test.aar - Durl=/nexus/content/repositories/thirdparty - DrepositoryId=M 3、插件 3.1 什么是插件 插件其实就是用来让我们偷懒的。如果没有插件,我们想要构建一个 Java 工程,就要自 己定义 sourceSets,自己定义 classpath,自己定义构建步骤等等。 简单地说,插件其实就是一组配置和任务的合集。 gradle 插件的存在形式主要由三种, gradle 文件中直接编写,你可以在你的 build.gradle 当中写一个插件来直接引入: apply plugin: GreetingPlugin class GreetingPlugin implements Plugin void apply(ProjectInternal project) project.task(hello) println hello 在 src/main/resources 创建 META-INF/gradle-plugins 目录,创建 perties 文件: implementation-class=com.tencent.wecar.plugin.GreetingPlugin 其中 greettings 就是你的插件 id。 build.gradle group com.tencent.wecar.plugin version 1.1-SNAPSHOT buildscript repositories mavenLocal() apply plugin: groovy apply plugin: java repositories mavenCentral() sourceSets main groovy srcDirs = src/main/groovy, src/main/java / compile everything in src/ with groovy java srcDirs = / no source dirs for the java compiler dependencies /tasks.withType(Compile) options.encoding = “UTF-8“ compile gradleApi() / custom tasks for creating source jars task sourcesJar(type: Jar, dependsOn:classes) classifier = sources from sourceSets.main.allSource / add source jar tasks as artifacts artifacts archives sourcesJar / upload to local uploadArchives repositories mavenLocal() 运行 uploadArchives 发布到本地仓库,那么就可以找到我们自己的插件了,由于当中没 有指定 artifactId,那么我们的插件的 artifactId 就是我们的工程名称,比如这里是 deployplugin。 那么我们要怎么引入这个插件呢? 首先要再 buildScript 增加依赖: buildscript repositories mavenLocal() depe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高中化学美育课件
- 高一政治课件
- 债务处理与离婚协议书结合的财产分配方案
- 离婚协议中财产分配、子女抚养及赡养费参考范本
- 创新型企业劳动合同终止与股权激励协议
- 企业违规使用假合同进行借款债务清偿协议
- 离婚协议中少数民族权益保障法律适用合同
- 汽车美容店租赁租金调整及支付方式合同
- 骶骨解剖课件
- 发展职业教育与技术创新方案
- 中学2025年“迎国庆、庆中秋”主题班会
- 垃圾的危害教学课件
- 寻找闪闪发光的自己(主题班会)课件
- 卷烟送货员安全培训课件
- 2025年电子乐器行业研究报告及未来行业发展趋势预测
- 2025至2030年中国招投标行业发展潜力分析及投资战略咨询报告
- 2025至2030中国矿山机械行业发展趋势分析与未来投资战略咨询研究报告
- 2025年北师大版新教材数学二年级上册教学计划(含进度表)
- 2025年广东省中考语文试卷真题(含答案)
- 2024中级出版专业资格考试真题带答案分析
- 2024年社区警务规范考试题库
评论
0/150
提交评论