AndroidStudio中gradle脚本实用配置。
Project/settings.gradle Module列表配置
1 2 3 
  | include ':app' include ':zxing' include ':Aliyunplayer:AlivcPlayer' 
  | 
 
定义Project所包含的Module
自定义统一化配置
定义 config.gradle 配置文件
1 2 3 4 5 6 7 
  | ext {     // Build Gradle Plugin     externalAndroidBuildGradlePlugin = 'com.android.tools.build:gradle:3.1.3'     // Android support Libraries     externalAndroidAppCompatV7 = 'com.android.support:appcompat-v7:27.1.0'     // ... } 
  | 
 
引入配置文件: Project/build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 
  | buildscript {     repositories {         google()         jcenter()         maven { url "..." } // 自定义maven路径     }     // 引用配置文件     apply from: 'config.gradle'     dependencies {         classpath externalAndroidBuildGradlePlugin         // NOTE: Do not place your application dependencies here; they belong         // in the individual module build.gradle files     } } allprojects {     repositories {         google()         jcenter()         maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" }     }     apply plugin: 'idea' } task wrapper(type: Wrapper) {     gradleVersion = '4.2.1' } task clean(type: Delete) {     delete rootProject.buildDir } 
  | 
 
app/build.gradle 单个Module配置
基本配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
  | apply plugin: 'com.android.application' // com.android.application 表示是一个应用程序app // apply plugin: 'com.android.library' 表示是一个 library android {     compileSdkVersion 27     defaultConfig {         applicationId "com.xxxxx"         minSdkVersion 16         targetSdkVersion 26         versionCode 1         versionName "1.0"         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"     }     // ... } dependencies {     implementation fileTree(include: ['*.jar'], dir: 'libs')     implementation project(':Aliyunplayer:AlivcPlayer')     implementation project(':zxing')     implementation externalAndroidAppCompatV7     implementation externalAndroidRecyclerView } 
  | 
 
多渠道Flavor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 
  | android {     flavorDimensions "stage"     productFlavors {         dev {             dimension "stage"             minSdkVersion 25             versionNameSuffix "-dev"             applicationIdSuffix '.dev'         }         prod {             dimension "stage"             minSdkVersion 25             versionNameSuffix "-prod"             applicationIdSuffix '.prod'             resConfigs "zh-rCN", "xxhdpi"                      }     } } 
  | 
 
签名文件sign配置
1 2 3 4 5 6 7 8 9 10 
  | android {     signingConfigs {         release {             keyAlias 'key_alias'             keyPassword 'pwd...'             storeFile file('/Users/ccfyyn/Documents/xy.keystore')             storePassword 'pwd...'         }     } } 
  | 
 
配置buildType
1 2 3 4 5 6 7 8 9 10 11 12 13 14 
  | android {     buildTypes {         debug {             minifyEnabled false             multiDexEnabled true             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'                          signingConfig signingConfigs.release         }         release {                      }     } } 
  | 
 
配置ndk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 
  | android {     defaultConfig {         applicationId "com.xxx.xxx"         minSdkVersion 16         targetSdkVersion 26         versionCode 2         versionName "1.0.0"         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"         ndk {             abiFilters "armeabi-v7a"         }     } } 
  | 
 
配置sourceSets
1 2 3 4 5 6 7 8 9 
  | android {     sourceSets.main {         jni.srcDirs = []         jniLibs.srcDir "src/main/libs"                  main.java.srcDirs += '/android.dmg/AOSP/frameworks/base/services'         main.java.srcDirs += '/android.dmg/AOSP/frameworks/base/core'     } } 
  | 
 
引用第三方plugin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 
  | buildscript {     repositories {         google()         jcenter()         maven { url "..." }      }          apply from: 'config.gradle'     dependencies {         classpath externalAndroidBuildGradlePlugin                  classpath 'com.tencent.bugly:symtabfileuploader:latest.release'                       } } allprojects {     repositories {         google()         jcenter()         maven { url "http://maven.aliyun.com/nexus/content/repositories/releases" }         flatDir { dirs 'src/main/libs' }     }     apply plugin: 'idea'          buildDir = new File(rootDir, "gradle-build/${path.replaceAll(':', '/')}") } task wrapper(type: Wrapper) {     gradleVersion = '4.2.1' } task clean(type: Delete) {     delete rootProject.buildDir } 
  | 
 
上面是在Project/build.gradle中进行配置,在Module/build.gradle中还需要进行引用。
1 2 3 4 5 6 7 
  | apply plugin: 'bugly' bugly {     appId = '...'      appKey = '...'      debug = true } 
  | 
 
使用aar文件
新建Module的时候,选择Import .jar/.aar Package,选择aar文件后会自动生成比如下面这样的一个gradle文件。
1 2 
  | configurations.maybeCreate("default") artifacts.add("default", file('AlivcPlayer-3.4.8.aar')) 
  | 
 
更多
Gradle-Link