前言:很久没有写博文了,百无聊赖,突然发现想做点事情,又无从下手,想来想去,还是决定总结一下这些年的经验,梳理一些知识,以至于不让自己颓废。既然是知识的梳理,就想用一个完整的项目来体现,个人觉得这样做了,思路会比较清晰,也想把之前一直想做的一个项目做完,不让内心留有遗憾。好了废话不多了,从创建一个项目开始吧!
俗话说的好,使用好开发工具是创建项目的一个重要的开端,从下载studio开始,有些公司不用翻墙也能浏览国外的一些网站,这样就比较翻遍了,如果没有也没关系,那就翻墙吧,也可以花钱翻,个人觉得这是必需的。下载完studio后(前期准备安装JDK什么的就不描述了),就需要进行安装了,安装完会自动下载Gradle,不翻墙会很慢哦!
好了,下载完Gradle之后,就可以打开studio了,这时候可以创建第一个项目,我这里创建的项目名称是 nrest ,至于studio工具的一些基本使用我会另外创建一篇博文,就不啰嗦了。创建完见下图
然后我们切换到工程目录,切换过程很简单点击小三角,选择Project就可以了
从上图可以看到这个总工程下有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.3.3' // 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 }
内容主要包含了两方面:一个是声明仓库的源 jcenter(),可以理解成一个中央远程长宽,兼容maven中心仓库,性能更优,另一个是声明了android gradle plugin版本,这个版本是跟studio的版本相对应的,可以向下兼容。
然后我们来看看settings.gradle
include ':app'这个文件是全局的项目配置文件,里面主要加入一些gradle的module,这里只有一个module ,如果还有其他的也需要加入用逗号隔开
最后我们看看主工程中最重要的一个配置,展开nrest工程下app目录,就会发现一个build.gradle
apply plugin: 'com.android.application' android { //编译SDK的版本 compileSdkVersion 26 //buildtools 的版本 buildToolsVersion "27.0.3" defaultConfig { //应用的包名 applicationId "com.android.nrest.nrest" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { debug{ //debug模式 } release { //release模式,是否进行混淆,下面是混淆文件的位置 minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { //编译libs目录下的所有jar包 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' }) compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' }
这是项目中最重要的一个配置。
plugin: com.android.application 代表主工程,其他的modle的plugin: com.android.library表示
如果有其他的 modle存在,那么在这个配置文件中的 dependencies目录下,需要添加一下的内容
compile project(':extras:model2')
这些只是最基本的配置,还有很多自定义的部分,如自动打包 debug , release beta 等环境,签名,多渠道打包,有用到的时候,会单独列出来。
好了创建一个新项目的第一步就完成了!!