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

Posted 韩曙亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误记录Android 编译时技术版本警告 ( 注解处理器与主应用支持的 Java 版本不匹配 )相关的知识,希望对你有一定的参考价值。





一、报错信息



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

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

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

警告: 来自注释处理程序 '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 ;

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

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

plugins {
    id 'java-library'
}

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

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

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 ;

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




三、解决方案



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

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

plugins {
    id 'java-library'
}

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

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

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 ;

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

修改后 , 编译时不再报上述警告 ;
在这里插入图片描述

以上是关于错误记录Android 编译时技术版本警告 ( 注解处理器与主应用支持的 Java 版本不匹配 )的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio编译OsmAnd出现警告:GeoPointParserUtil.java使用或覆盖了已过时的 API。有关详细信息请使用-Xlint:deprecation重新编译(代码片

错误记录编译 Android 版本的 ijkplayer 报错 ( ./init-android.sh: 第 37 行: cd: android/contrib/: 没有那个文件或目录 )

错误记录AS 编译报错 ( Android Support plugin 版本太高 | 升级 Android Studio 到最新版本 )

错误记录编译 Android 版本的 ijkplayer 报错 ( You must define ANDROID_NDK before starting. | 下载指定版本 NDK )

错误记录Android Studio 4.2.1 编译报错 ( Kotlin 版本推荐设置 1.5.0 )

mex 编译时出现 GCC 版本警告