AS更新后,许久没有新起项目。集成greendao时,遇到了些问题,主要是因为greendao需要配置calsspath和plugin,而gradle7之后有些变化。android build-tools升级到31后,AS强制要求使用gradle7,否则无法编译。本篇以配置greendao为例,示范下gradle7之后如何配置classpath和plugin,以及gradle7其他配置。
目录
一、使用gradle7
gradle-wrapper.properties中修改:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
二、适配gradle7
1、配置classpath
首先,不能在settings.gradle里面去配置,需要在外层的build.gradle去配置,配置如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'
}
}
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2、配置plugin和依赖
plugins {
id 'com.android.application'
}
apply plugin: 'org.greenrobot.greendao'
android {
compileSdk 32
defaultConfig {
applicationId "com.example.xxx"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'org.greenrobot:greendao:3.3.0'
}
3、配置maven仓库
以阿里的maven仓库为例,gradle7之前,我们这么配置就可以了:
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
但是升级到gradle7之后,不推荐使用http协议,会报错如下:
正确的使用方式:
maven {
allowInsecureProtocol = true
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
本篇介绍了如何在gradle7下配置gradle,如果实在不适应,也可以从老项目中copy原来的gradle配置,gradle是向下兼容的。但技术总是在不断进步的,还是一起拥抱变化吧。