原文地址天意博文
Android Studio 使用 Gradle这一高级构建工具包来自动化执行和管理构建流程,同时也允许您定义灵活的自定义构建配置。每个构建配置均可自行定义一组代码和资源,同时对所有应用版本共有的部分加以重复利用。
Android Plugin for Gradle 引入了您需要的大多数 DSL 元素(也就是在xxx.gradle中的元素),请阅读 DSL 参考文档
对应上面的项目结构有下面的gradle文件相对应:
settings.gradle
文件位于项目根目录,用于指示 Gradle 在构建应用时应将哪些模块包括在内。对大多数项目而言,该文件很简单,只包括以下内容:
include ‘:app’
顶级 build.gradle
文件位于项目根目录,用于定义适用于项目中所有模块的构建配置。默认情况下,这个顶级构建文件使用 buildscript {}
代码块来定义项目中所有模块共用的 Gradle 存储区和依赖项。
buildscript {
//远程仓库的目录
repositories {
jcenter()
}
//Android Plugin for Gradle版本
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
//应用于所有的moudle
allprojects {
repositories {
jcenter()
}
}
//使得android可以使用
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "haotinayi.win.myapplication"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
// 启用多dex,如果app中的代码方法数超过65535
multiDexEnabled true
// android单元测试配置
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
storeFile file("debug.keystore") // 签名文件相对路径
storePassword "android" // 签名的密码
keyAlias "androiddebugkey" // 别名
keyPassword "android" // 别名密码
release {
// 在混淆时去除代码中无用的内容
minifyEnabled true
// 在混淆时去除无用的资源,针对res/目录中的内容,不用压缩图片的大小
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' // 配置混淆文件
}
}
//产品风味(设置不同的版本)
productFlavors {
free {
applicationId 'com.example.myapp.free'
}
paid {
applicationId 'com.example.myapp.paid'
}
}
//从一个项目代码中产生不同版本的apk
splits {
// Screen density split settings
density {
// Enable or disable the density split mechanism
enable false
// Exclude these densities from splits
exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
}
}
}
dependencies {
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:appcompat-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
testCompile 'junit:junit:4.12'
}
gradle.properties
您可以在其中配置项目范围 Gradle 设置,例如 Gradle 后台进程的最大堆大小。
local.properties
为构建系统配置本地环境属性,例如 SDK 安装路径。由于该文件的内容由 Android Studio 自动生成并且专用于本地开发者环境,因此您不应手动修改该文件,或将其纳入您的版本控制系统。
应用是按照Application ID区分的,Application ID是应用的唯一标志,只不过默情况下和包名相同,默认在defaultConfig中来设置:
android {
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
...
}
可以改变Application ID来设置不同的APP:
android {
defaultConfig {
applicationId "com.example.myapp"
}
productFlavors {
free {
applicationIdSuffix ".free"
}
pro {
applicationIdSuffix ".pro"
}
}
}
那么就变成com.example.myapp.free
和com.example.myapp.pro
mainfest文件中搞的package属性:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
它有两个作用:
指定的就是 com.example.myapp.MainActivity
文件所以不能随便的更改package属性,但是AppicationID可以改变(也就改成了另外一个APP)
原文地址天意博文