Android Studio基础之项目目录结构(四)-学习篇

转载自:http://blog.csdn.net/wx198210/article/details/50248915

开始前先说下什么是Gradle

这是我在网上找到的对Gradle的描述:

At the heart of Gradle lies a rich extensible Domain Specific Language (DSL) based on Groovy. Gradle pushes declarative builds to the next level by providing declarative language elements that you can assemble as you like. Those elements also provide build-by-convention support for Java, Groovy, OSGi, Web and Scala projects. Even more, this declarative language is extensible. Add your own new language elements or enhance the existing ones, thus providing concise, maintainable and comprehensible builds. 
<翻译>Gradle的核心是一个丰富的可扩展的基于Groovy的领域特定语言(DSL)。Gradle通过提供说明性语言元素将说明性构建推到下一层,您可以组装。这些元素也提供build-by-convention支持Java、Groovy、OSGi、Web和Scala项目。更多,这个说明性语言是可扩展的。添加自己的新语言元素或提高现有的,从而提供简洁、易于维护和理解构建。

并且它是一个开源的工具,你可以在它的官网去下载Gradle官网。总的来说Gradle是一种构建工具Android Studio使用它来构建你的Android项目。Andrd Studio在创建项目的时候会自动去下载,你也可以去官网手动下载后在Android Studio中配置。现在在Android StudioV2.0中使用的Gradle版本是V2.8, 官网中最新的版本本是V2.9。

那么我们在Android Studio中自动下载的Gradle在哪里呢? 
- Mac上会默认下载到 /Users/<用户名>/.gradle/wrapper/dists 目录 
- Win平台会默认下载到 C:\Documents and Settings\<用户名>.gradle\wrapper\dists 目录 

Project目录结构

先来看下之前新建的项目 
项目

目录文件 作用
.gradle gradle项目产生文件夹(自动编译工具产生的文件)
.idea IDEA项目文件夹(开发工具产生的文件)
app module模块
build 构建时生成文件的地方
captures 捕获系统信息的日志目录
gradle gradle环境支持文件夹
.gitignore git源码管理文件
build.gradle gradle项目自动编译的配置文件
gradle.properties gradle运行环境配置文件
gradlew 自动完成 gradle 环境的linux mac 脚本,配合gradle 文件夹使用
gradlew.bat 自动完成 gradle 环境的windows 脚本,配合gradle 文件夹使用
local.properties Android SDK NDK 环境路径配置
setting.gradle gradle 项目的子项目包含文件
Test.iml IDEA 项目文件

.gradle目录

.gradle 
gradle 运行时自动生成的目录,一般情况不做修改,不需要纳入项目源代码管理中。

.idea目录

.idea 
Intellij IDEA 运行时候生成的文件目录,一般情况不做修改,不需要纳入项目源代码管理中。 
Intellij IDEA是JetBrains公司推出的Java集成开发环境,Android Studio是基于IDEA Commutity Edition开发的,Community 版本不仅是免费的而且是开源的。

module(app)

app 
每一个module可看成在Eclipse中的一个Project,里面的文件结构与父类差不多。里面也能包含build.gradle、gradle.properties、setting.gradle 等相关gradle文件,若没有定义,则在项目中使用父类的设置。更多的内容,后面再详细介绍了。

build目录

build 
编译时产生文件,不需要修改,也不需要纳入项目源代码管理中。

captures目录

captures 
如右图中,使用改功能会抓取相应的信息并保存在captures目录中,不需要纳入项目源代码管理中。

gradle目录

gradle 
可以看到里面wrapper目录下有两个文件,里面有一些项目对gradle的配置信息,我们来看下gradle-wrapper.properties里的内容

#Wed Oct 21 11:34:03 PDT 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip

可以看到里面声明了gradle的目录与下载路径以及当前项目使用的gradle版本。默认的路径我们一般不会更改,还记得之前介绍导入Eclipse项目时,对项目使用的gradle版本做的修改吗?

.gitignore文件

git对项目文件管理,可以在里面添加你不希望纳入git管理的文件。来看一下里面的内容。

 *.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

Android Studio已经自动帮我们添加了一些不需要的文件,我们可以按照同样的格式来加入自己的文件。

build.gradle文件

项目的编译环境配置,可以说是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.0.0-alpha2'

        // 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
}

这里是对Project的配置,是最顶层的配置,在module中同样有一个build.gradle文件,是对module的配置,如果module里没有进行配置,将使用这里的配置信息。一个是声明仓库的源,这里可以看到是指明的jcenter(), jcenter可以理解成是一个新的中央远程仓库,兼容maven中心仓库,而且性能更优。另一个是声明了android gradle plugin的版本。再说一下这里,各位是否还有映像。

dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha2'
    }

gradle.properties文件

配置gradle运行环境的文件,比如配置gradle运行模式,运行时jvm虚拟机的大小。在创建的项目中,暂时是没有内容的。各位参考下面的链接: 
https://docs.gradle.org/current/userguide/build_environment.html 
https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:decoupled_projects

gradlew & gradlew.bat 文件

分别是linux下的shell脚本和windows下的批处理文件,它们的作用是根据gradle-wrapper.properties文件中的distributionUrl下载对应的gradle版本。这样就可以保证在不同的环境下构建时都是使用的统一版本的gradle,即使该环境没有安装gradle也可以,因为gradle wrapper会自动下载对应的gradle版本。更多内容可以看下下面的链接,这里没有做更多的研究了: 
http://blog.csdn.net/maserkinger/article/details/36011235

local.properties文件

配置android NDK,SDK的地方,非android项目可能没有这个文件,这个路径根据不同想电脑不同,一般也不会纳入源代码管理之中。

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Tue Dec 08 19:51:24 CST 2015
ndk.dir=D\:\\Tools\\android-ndk-r10
sdk.dir=D\:\\Tools\\Android-SDK

setting.gradle文件

声明当前项目中含有什么module。如果有多个module会以‘,’分开,如:include ‘:app’, ‘:app2’

include ':app'

Test.iml文件

编译项目时生成的文件,其名为‘项目名.iml’,iml文件是Android Studio识别项目的配置文件,跟Eclipse里面的.project文件作用类似。

<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="Test" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="java-gradle" name="Java-Gradle">
      <configuration>
        <option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
        <option name="BUILDABLE" value="false" />
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <excludeFolder url="file://$MODULE_DIR$/.gradle" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>

Module目录结构

再看下Module里面的结构 
Module

目录文件 作用
build module编译时所生成文件的目录
lib 第三方依赖库所在目录
src module源码所在目录
src\andrroidTest android Studio生成的测试模块,可删除
src\main module代码目录,结构和Eclipse中的差不多了
src\test 单元测试模块,可删除
.gitignore module中的git管理文件
app.iml module中的IDEA 项目文件
build.gradle module自动编译的配置文件
proguard-rules.pro module代码混淆配置文件

build目录

module的编译文件目录,编译中生成的中间文件就在这里。 
build目录 
我们编译最终生成的apk就在build/outputs/apk目录下,里面包含了app-debug.apk, app-debug-unaligned.apk,app-release-unaligned.apk三种apk, 另外app-release.apk是生成在module的根目录下。

lib目录

第三方库存放目录,我们可以把需要的第三方库jar文件放到这里,*.so也同样放在这里。可以在Project Structure中管理它的依赖关系,也可以在build.gradle中直接修改。

src目录

module源码目录,我们工作的主要地方,没什么好多说的了。

.gitignore文件

module模块的git管理文件,可对module单独管理。

app.iml文件

同Project中的*.iml一样。管理module的配置。

build.gradle文件

这个文件是Module的gradle配置文件,也可以算是整个项目最主要的gradle配置文件。它的内容比较复杂,我们先来看一下

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "eric.test"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}


其中

// 这句是gradle版本的写法,声明是这是一个Android程序。
apply plugin: 'com.android.application'

Android项目中的配置:

// 编译android的sdk版本
compileSdkVersion 23
// build tools的版本
buildToolsVersion "23.0.2"
defaultConfig {
        //  应用的包名
        applicationId "eric.test"
        // 允许的最少版本
        minSdkVersion 17
        // 目标版本
        targetSdkVersion 23
        // apk的版本
        versionCode 1
        // apk的显示版本
        versionName "1.0"
    }
buildTypes {
        // 表明是在release版本中使用的配置
        release {
            // 是否运行混淆
            minifyEnabled false
            // 混淆的配置文件路径,默认给了我们两个配置文件。
            // proguard-android.txt在<sdk目录>/tools/proguard下面,里面已经包含了基本的混淆声明,有兴趣的可以去看下。
            // proguard-rules.pro是在module根目录下面,我们可以根据项目去添加。
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

依赖的配置:

// 表明是依赖库配置
dependencies {
    // 表明依赖libs目录下的所有jar包
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // 表明在编译项目的测试代码时依赖
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}


关于依赖,在后面章节中还会做更多的介绍。

proguard-rules.pro文件

module中的代码混淆配置文件,使用方式和Eclipse中一样。我们在build.gradle中可以看到如下片断用来启用混淆。

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

项目目录结构大概就分析完了,这里只是针对项目中的文件做了一些简单的说明,更说的使用在后面继续介绍了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值