赞
踩
gradle语法是基于Groovy语言,Groovy是一种敏捷的动态语言,既可以面向对象编程,又可以用作纯粹的脚本语言,其运行在JVM上,能够与java代码很好的结合,也能扩展现有代码。
工程中的位置:
#Tue Dec 10 09:19:23 CST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
修改上面的版本号,同步一下即可更改工程使用的gradle版本。
ext { //预定义
}
buildscript { //编译设置
repositories { 库地址
}
dependencies { 依赖库
}
}
allprojects {
repositories { 总的依赖地址
maven { } maven库
}
}
task
举例文件:
// 预定义 ext { supportLibraryVersion = '28.0.0' buildToolsVersion = '28.0.3' minSdkVersion = 19 compileSdkVersion = 28 targetSdkVersion = 23 versionCode = 683000007 versionName = "6.8.3.000007" ext.kotlin_version = '1.3.50' } buildscript { repositories { maven { url "http://localhost:8081/repository/maven-public/" } google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50" } } allprojects { repositories { maven { url "http://localhost:8081/repository/maven-public/" } google() jcenter() //maven { url "https://jitpack.io" } } } task clean(type: Delete) { delete rootProject.buildDir }
在root下的build.gradle的allprojects节点下:
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion rootProject.ext.supportVersion
}
}
}
resolutionStrategy{
force 'com.google.code.gson:gson:2.8.5'
}
}
ext {
supportLibraryVersion = '28.0.0'
buildToolsVersion = '28.0.3'
minSdkVersion = 19
compileSdkVersion = 28
targetSdkVersion = 23
versionCode = 183000007
versionName = "1.8.3.000007"
ext.kotlin_version = '1.3.50'
}
在其他地方可以这么引用rootProject
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
文件结构
android { defaultConfig { multiDexEnabled true //多dex配置 ndk { //ndk打包的架构 } lintOptions { } packagingOptions { //打包选项 } } configurations { } compileOptions { //Android O 以上需要用java 8 } buildTypes { //编译类型 release { } debug { } } sourceSets { //源码设置 main { } } dataBinding { //dataBinding设置 } repositories { flatDir { } } } dependencies { } task
举个例子
apply plugin: 'com.android.application' apply plugin: 'com.android.library' //kotlin配置 apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' //引入其他文件 apply from: 'upload.gradle' android { compileSdkVersion rootProject.compileSdkVersion buildToolsVersion rootProject.buildToolsVersion defaultConfig { applicationId "com.demo" minSdkVersion rootProject.minSdkVersion targetSdkVersion rootProject.targetSdkVersion versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" //多dex配置 multiDexEnabled true //ndk打包的架构 ndk { abiFilters "armeabi" } //连接 lintOptions { quiet true abortOnError false } //打包选项 packagingOptions { exclude 'lib/x86_64/libjingle_peerconnection_so.so' exclude 'lib/arm64-v8a/libjingle_peerconnection_so.so' exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' } } // configurations { cleanedAnnotations compile.exclude group: 'org.jetbrains' , module:'annotations' } //Android O 以上需要用java 8 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } //编译类型 buildTypes { release { minifyEnabled false zipAlignEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } debug { minifyEnabled false zipAlignEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } //源码设置 sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } } //dataBinding设置 dataBinding { enabled true } repositories { flatDir { dirs 'libs' //这样可以使用libs里面的aar } } } dependencies { //导入文件夹 implementation fileTree(dir: 'libs', include: ['*.jar']) //导入某某库 implementation 'androidx.appcompat:appcompat:1.0.0' //导入子库 implementation project(path: ':Location') }
编译成应用
apply plugin: ‘com.android.application’
编译成模块
apply plugin: ‘com.android.library’
sourceSets
指明文件的路径,是在src同级目录开始索引。
sourceSets{
main{
res.srcDirs = ['src/custom/res','src/main/res']
java.srcDirs = ['src/main/java']
manifest.srcFile 'src/main/AndroidManifest.xml'
assets.srcDirs=['src/custom/assets']
}
}
引入kotlin
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
引入其他文件
apply from: 'upload.gradle'
编译模块的命令一般为: gradlew :模块名:taskname
uploadArchives 上传模块
AndroidDependencies 查看依赖关系
siningReport 显示渠道包得签名信息
sourceSets 查看项目java ,res ,jni所在得资源位置
assembleDebug 打包所有应用程序debug包
assembleRelease 打包所有应用程序release包
build 打包所有,包括测试项内容
clean 删除构建build目录
cleanBuildCache 删除构建的缓存目录
buildEnvironment app module中声明的所有buildscript依赖项。
installDebug 安装Debug版本
uninstallDebug - 卸载Debug版本
assemble:对所有的 buildType 生成 apk 包。
clean:移除所有的编译输出文件,比如apk
check:执行lint检测编译。
build:同时执行assemble和check命令
虚拟机大小
org.gradle.jvmargs=-Xmx1536m
串行编译
org.gradle.parallel=true
启用 androidx
android.useAndroidX=true
将依赖包也迁移到androidx
android.enableJetifier=true
主要保存配置的地址
ndk.dir=C:\Users\Administrator\AppData\Local\Android\Sdk\ndk\20.0.5594570
sdk.dir=C:\Users\Administrator\AppData\Local\Android\Sdk
include ‘:app’ //包含模块
rootProject.name=‘Demo’ //工程的名字
编译aar
task makeAAR(type: Jar, dependsOn: [‘build’]) {}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。