Android build.gradle 配置

Posted 静待花开*^_^*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android build.gradle 配置相关的知识,希望对你有一定的参考价值。

一、初始配置:(新建项目自动配置好的):

1.build.gradle(app)

apply plugin: ‘com.android.application‘

//下边apk重命名使用

static def
buildTime() {
def date = new Date()
def formattedDate = date.format(‘yyyyMMdd-HHmm‘)
def iml = new File(‘app/app.iml‘)
if (iml.exists()) {
return ‘time‘
} else {
return formattedDate
}
}

static def getGitRevision() {
def iml = new File(‘app/app.iml‘)
if (iml.exists()) {
return ‘git_xxxx‘
} else {
return ‘git rev-parse --short HEAD‘.execute().text.trim()
}
}
//配置
android {
compileSdkVersion 26
buildToolsVersion "25.0.3"
defaultConfig {//此里边是默认配置,下边buildTypes里边可以根据平台不同修改
applicationId "wangjing.testandroid"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

//后期加入
    // 设置支持的 SO 库构架,注意这里要根据你的实际情况来设置
    ndk { abiFilters ‘armeabi-v7a‘ } //‘armeabi‘, ‘armeabi-v7a‘, ‘arm64-v8a‘, ‘x86‘, ‘x86_64‘, ‘mips‘, ‘mips64‘
    
    //使用manifestPlaceholders替换变量,主要替换AndroidManifest.xml中的值:<meta-data android:name="UMENG_APPKEY" // android:value="${umeng_app_key}"/>
    manifestPlaceholders = [GETUI_APP_ID : "P5rfdfytdftghgujghi6", //推送
                            GETUI_APP_KEY   : "xcftdctyfytfyt687ty8",
   GETUI_APP_SECRET: "dte654r6r65trrftftgf",
   PACKAGE_NAME : applicationId,
   APPLICATION_ID : applicationId,
   INTENT_HOME : ‘intent.Placeholders1‘,
   INTENT_DEFAULT : ‘intent.Placeholders2‘,
   SHARED_USER_ID : ‘‘]

   //buildConfigField代码中值的应用:BuildConfig.GIT_VERSION
   buildConfigField ‘String‘, ‘GIT_VERSION‘, ‘".‘ + getGitRevision() + ‘"‘ //当前git版本号
   buildConfigField ‘String‘, ‘TARGET_URL‘, ‘"wangjing.com"‘                   //服务器
   buildConfigField ‘String‘, ‘ROOT_DIR‘, ‘"WangJingDir"‘ //根文件夹
   buildConfigField ‘String‘, ‘TARGET‘, ‘"测试"‘ //平台

   buildConfigField ‘String‘, ‘LOG_TAG‘, ‘"WangJingDir"‘ //日志Tag
   buildConfigField ‘int‘, ‘LOG_LEVEL‘, ‘1‘ //日志级别

   buildConfigField ‘String‘, ‘UMENG_APPKEY‘, ‘"dssscfdeffder343534tefrfre"‘ //友盟APPKEY
   buildConfigField ‘String‘, ‘UMENG_CHANNEL‘, ‘"wangjing"‘ //友盟渠道
   buildConfigField ‘boolean‘, ‘UMENG_ENCRYPT‘, ‘false‘ //友盟日志加密
   buildConfigField ‘String‘, ‘APP_MODEL‘, ‘"WANGJING"‘
   buildConfigField ‘String‘, ‘INVITATIONCODE‘, ‘"fdvedrfg"‘ //默认班级码
   buildConfigField ‘int‘, ‘MESSAGE_REFRESH_TIME‘, ‘180‘ //消息中心刷新时间间隔(秒)
   buildConfigField ‘String‘, ‘SHARE_URL‘, ‘"http://d.wangjing.com"‘ //分享url

   //文件系统参数
   buildConfigField ‘String‘, ‘SERVER_URL‘, ‘"http://fs.wangjing.com"‘
   buildConfigField ‘String‘, ‘APPKEY‘, ‘"filewj"‘
   buildConfigField ‘String‘, ‘SECRET‘, ‘"fvbfdvbfdvbfgvbfer4t4t54tg"‘
   buildConfigField ‘String‘, ‘BUTCKET1‘, ‘"vfdvgrgrtgr4g4eg4r5g4g4eg4gg"‘ // 作业活动问答
   buildConfigField ‘String‘, ‘BUTCKET2‘, ‘"f4rtfgr4g4gt45y45ytygftrttfg"‘ // 头像课程封面

   buildConfigField ‘boolean‘, ‘isPad‘, ‘false‘ // 是不是pad
   buildConfigField ‘boolean‘, ‘isTest‘, ‘false‘ // 是不是测试平台
   buildConfigField ‘boolean‘, ‘isHuaweiPad‘, ‘false‘ // 是不是定制华为pad
   buildConfigField ‘String‘, ‘DWONURLPRE‘, ‘"http://dvdfvdfv.aliyuncs.com/"‘ //阿里云上传默认前缀
  proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘ //默认的混淆文件
    // 重命名APK
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
    def targetName = variant.buildType.name;
    targetName = targetName.replace("debug", "test")
    def apkName = "mobile-${targetName}-v${variant.versionName}-${buildTime()}-${getGitRevision()}.apk"
     def dir = output.outputFile.getParent()
     def apkFile = new File(dir, apkName)
    output.outputFile = apkFile
    }
    }
  }

//签名配置
signingConfigs {
release {//其中的
RELEASE_STOREFILE值可以定义在gradle.propertises中调用

storeFile file(RELEASE_STOREFILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}

//原本初始化时候的 
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
}
}
//被替换为
buildTypes {
debug {
minifyEnabled false
shrinkResources false
debuggable true
signingConfig signingConfigs.release
    
buildConfigField ‘boolean‘, ‘isTest‘, ‘true‘ // 是test/debug平台
buildConfigField ‘String‘, ‘TARGET_URL‘, ‘"wangjing.com"‘ //服务器平台
buildConfigField ‘String‘, ‘TARGET‘, ‘" 测试"‘ //在‘我’中显示平台
buildConfigField ‘int‘, ‘LOG_LEVEL‘, ‘1‘ //日志级别
buildConfigField ‘String‘, ‘SERVER_URL‘, ‘"http://fs.wangjing.com"‘ //文件服务器配置
ndk { abiFilters ‘armeabi-v7a‘, ‘x86‘ }
}

demo {
minifyEnabled false
shrinkResources false
debuggable true
signingConfig signingConfigs.release
buildConfigField ‘String‘, ‘TARGET_URL‘, ‘"wangjing.com"‘
buildConfigField ‘String‘, ‘TARGET‘, ‘" DEMO"‘
buildConfigField ‘int‘, ‘LOG_LEVEL‘, ‘1‘
buildConfigField ‘String‘, ‘UMENG_CHANNEL‘, ‘"wangjingdemo"‘
buildConfigField ‘String‘, ‘SERVER_URL‘, ‘"http://fs.wangingdemo.com"‘ //文件服务器配置
ndk { abiFilters ‘armeabi-v7a‘, ‘x86‘ }
}


release {
minifyEnabled false
debuggable false
signingConfig signingConfigs.release
buildConfigField ‘String‘, ‘TARGET_URL‘, ‘"wangjing.com"‘
buildConfigField ‘String‘, ‘TARGET‘, ‘""‘
buildConfigField ‘int‘, ‘LOG_LEVEL‘, ‘5‘
buildConfigField ‘String‘, ‘UMENG_CHANNEL‘, ‘"wangjing"‘
buildConfigField ‘boolean‘, ‘UMENG_ENCRYPT‘, ‘true‘ //友盟日志加密
buildConfigField ‘String‘, ‘SERVER_URL‘, ‘"http://fs.wangjing.cn"‘ //文件服务器配置
}

local {
initWith release
minifyEnabled false
shrinkResources false
debuggable true
buildConfigField ‘String‘, ‘TARGET‘, ‘" 本地"‘ //在‘我’中显示平台
buildConfigField ‘int‘, ‘LOG_LEVEL‘, ‘1‘ //日志级别
ndk { abiFilters ‘armeabi-v7a‘, ‘x86‘ }
}

pad_test {
initWith debug //用debug的配置

manifestPlaceholders = [INTENT_HOME : ‘android.intent.category.HOME‘,
INTENT_DEFAULT: ‘android.intent.category.DEFAULT‘,
SHARED_USER_ID: ‘‘]

buildConfigField ‘String‘, ‘APP_MODEL‘, ‘"WANGJING-PAD"‘
buildConfigField ‘boolean‘, ‘isPad‘, ‘true‘ // 是pad
}

pad_demo {
initWith demo

manifestPlaceholders = [INTENT_HOME : ‘android.intent.category.HOME‘,
INTENT_DEFAULT: ‘android.intent.category.DEFAULT‘,
SHARED_USER_ID: ‘‘]

buildConfigField ‘String‘, ‘APP_MODEL‘, ‘"WANGJING-PAD"‘
buildConfigField ‘boolean‘, ‘isPad‘, ‘true‘ // 是pad
}

pad_release {
initWith release

manifestPlaceholders = [INTENT_HOME : ‘android.intent.category.HOME‘,
INTENT_DEFAULT: ‘android.intent.category.DEFAULT‘,
SHARED_USER_ID: ‘‘]

buildConfigField ‘String‘, ‘APP_MODEL‘, ‘"WANGJING-PAD"‘
buildConfigField ‘boolean‘, ‘isPad‘, ‘true‘ // 是pad
}


ruiyipad_release {
initWith release

buildConfigField ‘String‘, ‘APP_MODEL‘, ‘"WANGJING-PAD-RUIYI"‘
buildConfigField ‘boolean‘, ‘isPad‘, ‘true‘ // 是pad
}

huaweipad_test {
initWith pad_test

buildConfigField ‘String‘, ‘APP_MODEL‘, ‘"WANGJING-PAD-HUAWEI"‘
buildConfigField ‘boolean‘, ‘isHuaweiPad‘, ‘true‘ // 是不是华为pad
}

huaweipad_demo {
initWith pad_demo

buildConfigField ‘String‘, ‘APP_MODEL‘, ‘"WANGJING-PAD-HUAWEI"‘
buildConfigField ‘boolean‘, ‘isHuaweiPad‘, ‘true‘ // 是不是华为pad
}

huaweipad_release {
initWith pad_release

buildConfigField ‘String‘, ‘APP_MODEL‘, ‘"WANGJING-PAD-HUAWEI"‘
buildConfigField ‘boolean‘, ‘isHuaweiPad‘, ‘true‘ // 是不是华为pad
}
}

packagingOptions {//打包的时候提示哪个出错了,exclude排除一下
exclude ‘META-INF/DEPENDENCIES‘
exclude ‘META-INF/NOTICE‘
exclude ‘META-INF/LICENSE‘
exclude ‘META-INF/LICENSE.txt‘
exclude ‘META-INF/NOTICE.txt‘
}

dexOptions {//将运行内存设置大一些,运行变快
preDexLibraries false
javaMaxHeapSize "4g"
}
}

dependencies {//依赖,引用
  compile project(‘:logic‘)//引入本地lib项目,记得settings.gradle里边加入引用:include ‘:app‘,‘:logic‘
compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])//引入本地jar包
androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, {
exclude group: ‘com.android.support‘, module: ‘support-annotations‘
})//两个项目中都包含support-annotations,去除掉espresso-core里边的group:‘com.android.support‘中的support-annotations,用本项目app下边的support-annotations
compile ‘com.android.support:appcompat-v7:26.+‘
compile ‘com.android.support.constraint:constraint-layout:1.0.2‘
testCompile ‘junit:junit:4.12‘ //这个没有什么用,出错可以去除掉
}

repositories {
maven {//导入外部的东西时,需要加入自己的maven外部的库
url "http://hjghujhniknukhnh/releases/"
maven { url "https://ffvytfgytgyu/maven" }
//https://github.com/PhilJay/MPAndroidChart图表依赖
maven { url "https://jitpack.io" }
}
flatDir {//将aar文件复制到app module目录下的libs文件夹中,然后打开app module目录下的build.gradle配置文件
dirs ‘libs‘
}
}

2.build.gradle(Project:wangjing)
//自动生成
buildscript {
repositories {
jcenter()
    //如果有maven库的引入,加入
    //mavenCentral()
}
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()
   //如果有maven库的引入,加入 
   //mavenCentral()
}
}

// 运行gradle clean时,执行此处定义的task // 该任务继承自Delete,删除根目录中的build目录 // 相当于执行Delete.delete(rootProject.buildDir) // gradle使用groovy语言,调用method时可以不用加()
task clean(type: Delete) {
delete rootProject.buildDir
}

3.build.gradle(Module:logic)
apply plugin: ‘com.android.library‘

android {
compileSdkVersion 25
buildToolsVersion ‘25.0.2‘

defaultConfig {
minSdkVersion 11
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
}
}
}

repositories {
flatDir {
dirs ‘libs‘
}
}

dependencies {
compile project(‘:pay‘)

compile fileTree(include: [‘*.jar‘], dir: ‘libs‘)
testCompile ‘junit:junit:4.12‘
// 友盟统计
compile ‘com.umeng.analytics:analytics:latest.integration‘
}

注:首次上传项目到git上需要配置gitignore文件
1.(app最外部项目)
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
build
/captures
.svn
/.idea/
2.(内部)只需要去除
build

基本配置完成!
二、配置讲解
  1. apply plugin: ‘com.android.application‘//说明module的类型,com.android.application为程序,com.android.library为库  
  2. android {  
  3.     compileSdkVersion 22//编译的SDK版本  
  4.     buildToolsVersion "22.0.1"//编译的Tools版本  
  5.     defaultConfig {//默认配置  
  6.         applicationId "com.nd.famlink"//应用程序的包名  
  7.         minSdkVersion 8//支持的最低版本  
  8.         targetSdkVersion 19//支持的目标版本  
  9.         versionCode 52//版本号  
  10.         versionName "3.0.1"//版本名  
  11.         manifestPlaceholders = [ UMENG_CHANNEL_VALUE:"hsq" ]//声明友盟渠道名是可变的  
  12.     }  
  13.     sourceSets {//目录指向配置  
  14.         main {  
  15.             manifest.srcFile ‘AndroidManifest.xml‘//指定AndroidManifest文件  
  16.             java.srcDirs = [‘src‘]//指定source目录  
  17.             resources.srcDirs = [‘src‘]//指定source目录  
  18.             aidl.srcDirs = [‘src‘]//指定source目录  
  19.             renderscript.srcDirs = [‘src‘]//指定source目录  
  20.             res.srcDirs = [‘res‘]//指定资源目录  
  21.             assets.srcDirs = [‘assets‘]//指定assets目录  
  22.             jniLibs.srcDirs = [‘libs‘]//指定lib库目录  
  23.         }  
  24.         debug.setRoot(‘build-types/debug‘)//指定debug模式的路径  
  25.         release.setRoot(‘build-types/release‘)//指定release模式的路径  
  26.     }  
  27.     signingConfigs {//签名配置  
  28.         release {//发布版签名配置  
  29.             storeFile file("fk.keystore")//密钥文件路径  
  30.             storePassword "123"//密钥文件密码  
  31.             keyAlias "fk"//key别名  
  32.             keyPassword "123"//key密码  
  33.         }  
  34.         debug {//debug版签名配置  
  35.             storeFile file("fk.keystore")  
  36.             storePassword "123"  
  37.             keyAlias "fk"  
  38.             keyPassword "123"  
  39.         }  
  40.     }  
  41.   
  42.     buildTypes {//build类型  
  43.         release {//发布  
  44.             minifyEnabled true//混淆开启  
  45.             proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-project.txt‘//指定混淆规则文件  
  46.             signingConfig signingConfigs.release//设置签名信息  
  47.         }  
  48.         debug {//调试  
  49.             signingConfig signingConfigs.release  
  50.         }  
  51.     }  
  52.     packagingOptions {  
  53.         exclude ‘META-INF/ASL2.0‘  
  54.         exclude ‘META-INF/LICENSE‘  
  55.         exclude ‘META-INF/NOTICE‘  
  56.         exclude ‘META-INF/MANIFEST.MF‘  
  57.     }  
  58.   
  59.     lintOptions {  
  60.         abortOnError false//lint时候终止错误上报,防止编译的时候莫名的失败  
  61.     }  
  62.       
  63.     //打包-渠道  
  64.     productFlavors {  
  65.         hsq{}  
  66.         hsq_dx{}  
  67.         hsq_wx{}  
  68.         baidu{}  
  69.         yingyongbao{}  
  70.         ppzhushou{}  
  71.         anzhi{}  
  72.         zhushou360{}  
  73.         huawei{}  
  74.         lenovomm{}  
  75.         wandoujia{}  
  76.         mumayi{}  
  77.         meizu{}  
  78.         youyi{}  
  79.         sougou{}  
  80.     }  
  81.       
  82.     //打包-防渠道代码重复处理  
  83.     productFlavors.all { flavor ->  
  84.         flavor.manifestPlaceholders = [ UMENG_CHANNEL_VALUE:name ]  
  85.     }  
  86. }  
  87.   
  88. dependencies {//依赖  
  89.     compile fileTree(dir: ‘libs‘, exclude: [‘android-support*.jar‘], include: [‘*.jar‘])   //编译lib目录下的.jar文件  
  90.     compile project(‘:Easylink‘)//编译附加的项目  
  91.     compile project(‘:ImageLibrary‘)  
  92.     compile project(‘:ImageResLibrary‘)  
  93.     compile project(‘:Ofdmtransport‘)  
  94.     compile project(‘:PullToRefreshLibrary‘)  
  95.     compile project(‘:RecorderLibrary‘)  
  96.     compile project(‘:WebSocket‘)  
  97.     compile project(‘:WidgetLibrary‘)  
  98.     compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.3‘//编译来自Jcenter的第三方开源库  
  99. }  


































































































































































































































































































































以上是关于Android build.gradle 配置的主要内容,如果未能解决你的问题,请参考以下文章

build.gradle代码

Android Gradle 插件build.gradle 中的 android 配置 ( 配置项 | compileSdkVersion 配置 | buildToolsVersion 配置 )

Android Gradle 插件Module 目录下 build.gradle 配置文件 ( android 闭包块配置 | AppExtension 扩展类型参考文档 )

看不懂的 build.gradle 代码

Android studio中2种build.gradle文件介绍

Android Gradle 插件Gradle 基础配置 ② ( Gradle 空白项目构建示例演示 )