build.gradle(二)

一、简介

Android Studio是采用Gradle来构建项目的。Gradle是一个非常先进的项目构建工具,它试用了一种基于Groovy的领域特定语言(DSL)来声明项目设置,摒弃了XML(如Ant和Maven)的各种烦琐配置。

二、文件位置

项目中一般会出现2个或者多个build.gradle文件,一个在最完全的目录下,一个在app目录下。如果切换到Android模式下则全部在Gradle Scripts.

三、详解

1.最外层目录下的build.gradle文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories { //闭包
jcenter() //代码托管库:设置之后可以在项目中轻松引用jcenter上的开源项目
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0' //声明gradle插件,2.2.0为插件版本号
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' //开发者个人添加ButterKnife插件

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter() //代码托管库:设置之后可以在项目中轻松引用jcenter上的开源项目

}

}


2.app目录下的build.gradle文件

apply plugin: 'com.android.application' //默认的应用程序模块(插件)
apply plugin: 'com.neenbedankt.android-apt' //开发者添加butterkinfe插件

android {
compileSdkVersion 25 //编译版本
buildToolsVersion "24.0.2" //构建工具版本
defaultConfig {
applicationId "com.example.administrator.firstlinecode" //包名
minSdkVersion 15 //最低兼容版本
targetSdkVersion 25 //充分测试过版本(建议版本)
versionCode 1 //版本号
versionName "1.0" //版本名称
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false //是否混淆(true为混淆)

//Android SDKtongyong 的混淆规则; 后面的为开发者编写的项目混淆规则

 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
//还有一个debug:测试版本
}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.1.0' //远程依赖声明
compile 'com.jakewharton:butterknife:8.4.0' //远程依赖声明
apt 'com.jakewharton:butterknife-compiler:8.4.0'
testCompile 'junit:junit:4.12' //声明测试用例库
}

---
转载:http://blog.csdn.net/true_maitian/article/details/54922768

__________________________________________________________________________________________________________________

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion '28.0.3'

defaultConfig {
applicationId "com.iflytek.voicedemo"
minSdkVersion 9
targetSdkVersion 29

}

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

sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}

allprojects {
repositories {

jcenter()
mavenCentral()
}
}

}


dependencies {
implementation files('libs/Msc.jar')
// compile project(':MscLibSrc')
// implementation 'com.android.support:support-v7:28.'
// implementation 'com.android.support:support-v7:28.'
implementation 'com.android.support:appcompat-v7:29+'
}

 _____________________________________________2010.2.26新发现_________________________________________________________

下面看https://www.jianshu.com/p/e57e8ef2321b所获

(avd manager )

compileSdkVersion :当前使用哪个版本的SDK来编译项目。
minSdkVersion :当前可在终端设备运行的最小版本;
maxSdkVersion:当前可在终端设备运行的最大版本;
targetSdkVersion :App最大可以支持的API;

在这之前,我们需要了解终端设备和API的对应关系

VersionAPI level
1.0 API level 1
1.1 API level 2
1.5 API level 3, NDK 1
1.6 API level 4, NDK 2
2.0 API level 5
2.0.1 API level 6
2.1 API level 7, NDK 3
2.2.x API level 8, NDK 4
2.3 - 2.3.2 API level 9, NDK 5
2.3.3 - 2.3.7 API level 10
3.0 API level 11
3.1 API level 12, NDK 6
3.2.x API level 13
4.0.1 - 4.0.2 API level 14, NDK 7
4.0.3 - 4.0.4 API level 15, NDK 8
4.1.x API level 16
4.2.x API level 17
4.3.x API level 18
4.4 - 4.4.4 API level 19
4.4W API level 20
5.0 API level 21
5.1 API level 22
6.0 API level 23
7.0 API level 24
7.1.1 API level 25

(1)minSdkVersion :当前可在终端设备运行的最小版本;

一张图就可以解释


图片.png

没错,我手机是Android6.0的系统,对应的API是23,项目中要求最小版本号(minSdkVersion )是27,所以不能安装。想要成功安装,必须修改minSdkVersion 的值,修改成23或低于23的数值即可。


图片.png

看到没?修改成22之后可以安装了。

(2)maxSdkVersion:当前可在终端设备运行的最大版本;
这个概念直接舍弃吧,AS项目中,设置maxSdkVersion也毫无作用。
(3)targetSdkVersion :App最大可以支持的API
作为一个Android开发者,如果不懂targetSdkVersion的概念就不是一个合格的开发者。
开始解释这个概念(尽量言简意赅):
Android每次更新版本都会对之前的版本的问题做了一些修复并且增加新特性。
1.如果APP的targetSdkVersion = 22,安装在Android6.0(API 23)的手机上,那么该APP就不支持API 23的新特性。
2.如果APP的targetSdkVersion = 23,安装在Android5.1(API 22)的手机上,那么则向前兼容。
就解释这么多,说多了也不好理解。
如果想要了解的更加详细,建议先了解一下每个版本的新特性吧。

(4)compileSdkVersion :当前使用哪个版本的SDK来编译项目。
先声明一下,compileSdkVersion只用在编译,和运行无关。
在某些特定情况下,为了满足项目的某个功能,需要用到高版本API的新特性,所以必须提高targetSdkVersion的值,这个时候有可能因为compileSdkVersion过小导致编译失败。
根据一系列的总结,个人感觉compileSdkVersion和targetSdkVersion的关系是这样的:
targetSdkVersion <= compileSdkVersion。


_____________________________________________________________________________________________________________


apply plugin: 'com.android.application'

android {
compileSdkVersion 29 //当前使用哪个版本的SDK来编译项目。 targetSdkVersion <= compileSdkVersion。 //compileSdkVersion只用在编译,和运行无关。
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "cn.edu.jssvc.dome"
//我手机是Android610.0的系统,对应的API是29,项目中要求最小版本号(minSdkVersion )是19,所以能安装。
//minSdkVersion 的值,应该为29或低于29的数值即可。
minSdkVersion 19 //当前可在终端设备运行的最小版本
targetSdkVersion 29 //App最大可以支持的API
//APP的targetSdkVersion = 29,若安装在Android9(API 28)的手机上,那么则向前兼容。
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
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'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
原文地址:https://www.cnblogs.com/xtxt1127/p/12165582.html