Android Realm Gradle3.0以上出错解决方法

前言

成长就是,渐渐温柔,克制,朴素,不怨不问不记,安静中渐渐体会生命的盛大——沈奇岚

想在Android Studio3.2中使用Realm,结果发现gradle3.0以上不支持APT了。需要转换下才可以使用,特意将解决的方法记录下来。

问题描述

按照正常,我们会这样子配置:

buildscript {
    repositories {
        ……
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.2.1"
        classpath "io.realm:realm-gradle-plugin:2.1.1" //添加realm插件
    }
}


allprojects {
    repositories {
        ……
    }
}

app目录下的build.gradle中的开头处加上:

apply plugin: 'realm-android' //依赖Realm数据库,插件化依赖

但是就会出现以下的错误:

android-apt plugin is incompatible with the Android Gradle plugin. 
Please use 'annotationProcessor' configuration instead. 

解决方案

首先,在项目根目录下的build.gradle中添加:

buildscript {
    repositories {
        ……
    }
    dependencies {
        classpath "com.android.tools.build:gradle:3.2.1"
         classpath "io.realm:realm-transformer:2.1.1"// 这里
    }
}


allprojects {
    repositories {
        ……
    }
}

app目录下的build.gradle中添加如下内容:

// 因为gradle3.0以上不支持apt,所以需要转换下
import io.realm.transformer.RealmTransformer
android.registerTransform(new RealmTransformer())

dependencies {
    implementation "io.realm:realm-annotations:2.1.1"
    implementation "io.realm:realm-android-library:2.1.1"
    annotationProcessor "io.realm:realm-annotations-processor:2.1.1"
}

大功告成!

附录

多线程中的坑

发布了181 篇原创文章 · 获赞 217 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/Notzuonotdied/article/details/86381114