前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【错误记录】Android 编译时技术版本警告 ( 注解处理器与主应用支持的 Java 版本不匹配 )

【错误记录】Android 编译时技术版本警告 ( 注解处理器与主应用支持的 Java 版本不匹配 )

作者头像
韩曙亮
发布2023-03-29 12:51:49
9640
发布2023-03-29 12:51:49
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、报错信息


在使用 Android 编译时技术 , 涉及 编译时注解 , 注解处理器 ;

开发注解处理器后 , 编译报如下警告 ;

该警告不会影响编译 , 也不会中断编译的进行 , 编译依然能成功 ;

代码语言:javascript
复制
警告: 来自注释处理程序 'org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor' 的受支持 source 版本 'RELEASE_7' 低于 -source '1.8'
注: SupportedAnnotationTypes : kim.hsl.router_annotation.Route
1 个警告
在这里插入图片描述
在这里插入图片描述

二、问题分析


在 Android 主应用的 build.gradle 构建脚本中 , 支持的 Java 版本是 1.8 ;

代码语言:javascript
复制
android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

在 编译时注解 依赖库 中的 build.gradle 构建脚本如下 :

代码语言:javascript
复制
plugins {
    id 'java-library'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

在注解处理器依赖库 中的 build.gradle 构建脚本如下 :

代码语言:javascript
复制
plugins {
    id 'java-library'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(path: ':router-annotation')
    annotationProcessor 'com.google.auto.service:auto-service:1.0-rc4'
    compileOnly 'com.google.auto.service:auto-service:1.0-rc4'
}

注解处理器上使用 @SupportedSourceVersion 注解设置的支持的 Java 版本号也是 1.7 ;

代码语言:javascript
复制
// 自动注册注解处理器
@AutoService(Processor.class)
// 支持的注解类型
@SupportedAnnotationTypes({"kim.hsl.router_annotation.Route"})
// 支持的 Java 版本
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class RouterProcessor extends AbstractProcessor {
}

三、解决方案


将上述的 Java 版本号都设置为 1.8 ;

编译时注解 依赖库 的 build.gradle :

代码语言:javascript
复制
plugins {
    id 'java-library'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

注解处理器 依赖库 的 build.gradle :

代码语言:javascript
复制
plugins {
    id 'java-library'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(path: ':router-annotation')
    annotationProcessor 'com.google.auto.service:auto-service:1.0-rc4'
    compileOnly 'com.google.auto.service:auto-service:1.0-rc4'
}

注解处理器 支持的 Java 版本号 : @SupportedSourceVersion(SourceVersion.RELEASE_8) 支持到 1.8 ;

代码语言:javascript
复制
// 自动注册注解处理器
@AutoService(Processor.class)
// 支持的注解类型
@SupportedAnnotationTypes({"kim.hsl.router_annotation.Route"})
// 支持的 Java 版本
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class RouterProcessor extends AbstractProcessor {
}

修改后 , 编译时不再报上述警告 ;

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-05-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、报错信息
  • 二、问题分析
  • 三、解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档