JVM第四天-Class三大声明周期之Linking和Initializing

本文深入探讨JVM中Class的加载、链接和初始化过程,分析静态变量赋值的细节,并通过具体示例解释对象变量的赋值流程。了解这些核心机制对于掌握Java运行时行为至关重要。

在这里插入图片描述
一般来说,class的加载过程比较繁琐,因此我们之前比较大篇幅的进行了描述,但相对而言,class之后的2个步骤Linking和 Initializing就相对简单了一些,这次我们就来把剩下的两个讲完。

Linking

把class链接的过程,它又分为三小步:

1、Verification

验证文件是否符合JVM规定(例如是否包含cafe babe之类的规范内容)

2、Preparation

静态成员变量赋默认值

3、Resolution

将类、方法、属性等符号引用解析为直接引用。
常量池中的各种符号引用解析为指针、偏移量等内存地址的直接引用。
还记得我们在class文件格式分析的时候,看到的各种属性和常量值里各种的引用指向吗
在这里插入图片描述
可以认为,这个阶段,就是把这种字面量的值转换成内存里的实际地址。
在ClassLoader类中的loadClass方法中我们之前应该看到过一个方法:

/**
 * Links the specified class.  This (misleadingly named) method may be
 * used by a class loader to link a class.  If the class <tt>c</tt> has
 * already been linked, then this method simply returns. Otherwise, the
 * class is linked as described in the "Execution" chapter of
 * <cite>The Java™ Language Specification</cite>.
 *
 * @param  c
 *         The class to link
 *
 * @throws  NullPointerException
 *          If <tt>c</tt> is <tt>null</tt>.
 *
 * @see  #defineClass(String, byte[], int, int)
 */
protected final void resolveClass(Class<?> c) {
    resolveClass0(c);
}

这个方法实际内部就完成了这个工作。

Initializing

调用类初始化代码 ,给静态成员变量赋初始值

通过面试题理解静态变量赋值过程

以上我们就把class的三大步骤讲述完了,接下来我们结合这个过程,来分析一道面试题。

package com.peng.jvm.c2_classloader;

public class T001_ClassLoadingProcedure {
    public static void main(String[] args) {
        System.out.println(T.count); //这个count是几?
    }
}

class T {
    public static T t = new T(); 
    public static int count = 2;
    private T() {
        count ++;
    }
}

来看一下上面的代码,分析一下最后打印的count应该是多少呢?
结果是2,和你想的一样吗? 我们来分析下为什么:
T.count这种调用静态属性在JVM规范中是一种必须要初始化类的行为,因此会触发T的初始化过程,而我们现在终于知道一个Class要经过三大步才算整体完成装载。
第一步,把T这个Class通过Classloader装载到了内存中。
第二步,Linking环节,校验class文件的格式,然后对静态变量从上到下的赋予默认值(引用类型是null,基本类型是0),最后完成符号引用的替换。于是这一步执行之后,T里的成员值应该是这样的:

public static T t = null;
public static int count = 0;

第三步,进入Initializing环节,开始对静态变量从上到下的赋予初始值,调用静态方法。这里执行之后,T里的成员值是这样的:

public static T t = new T(); 

这里会需要调用到T的构造方法中,然后把count++,然后count一开始是0,此时就变成1了:

public static int count = 1;

接下来,对count赋予初始值:

public static int count = 2;

所以count最终的值是2。

那这道题换一下呢,把count和t的顺序变一下,这样的话呢:

package com.peng.jvm.c2_classloader;

public class T001_ClassLoadingProcedure {
    public static void main(String[] args) {
        System.out.println(T.count); //这个count是几?
    }
}
class T {
    public static int count = 2;
    public static T t = new T(); 
    
    private T() {
        count ++;
    }
}

结果是几? 是3
我们再走一遍流程:
第一步装载class不说了。
第二步,从上到下赋予静态变量默认值,变成:

public static int count = 0;
public static T t = null; 

第三步,从上到下赋予静态变量的初始值,变成:

public static int count = 2;
public static T t = new T(); 

这里会去调用T的构造方法,count++,count变成3.
因此最后count的值是3.

对象变量的赋值

上面面试题剖析了静态变量的赋值过程,分为三部:load - 默认值 - 初始值
而其实对象变量的赋值,也不是一步完成的,也是分为了2步:
1、当一个对象通过new,首先需要向内存申请空间,申请完内存后会首先把对象变量都赋予默认值
2、接下来再给对象里的属性赋予初始值。

Duplicate class androidx.annotation.AnimRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.AnimatorRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.AnyRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.AnyThread found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.ArrayRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.AttrRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.BinderThread found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.BoolRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.CallSuper found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.CheckResult found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.ChecksSdkIntAtLeast found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.ColorInt found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.ColorLong found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.ColorRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.ContentView found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.DimenRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.Dimension found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.Discouraged found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.DisplayContext found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.DoNotInline found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.DrawableRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.FloatRange found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.FontRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.FractionRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.GravityInt found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.GuardedBy found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.HalfFloat found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.IdRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.InspectableProperty found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.InspectableProperty$EnumEntry found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.InspectableProperty$FlagEntry found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.InspectableProperty$ValueType found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.IntDef found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.IntRange found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.IntegerRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.InterpolatorRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.Keep found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.LayoutRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.LongDef found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.MainThread found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.MenuRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.NavigationRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.NonNull found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.NonUiContext found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.Nullable found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.PluralsRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.Px found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.RawRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.RequiresApi found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.RequiresFeature found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.RequiresPermission found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.RequiresPermission$Read found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.RequiresPermission$Write found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.RestrictTo found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.RestrictTo$Scope found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.Size found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.StringDef found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.StringRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.StyleRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.StyleableRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.TransitionRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.UiContext found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.UiThread found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.VisibleForTesting found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.WorkerThread found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Duplicate class androidx.annotation.XmlRes found in modules annotation-1.3.0.jar -> annotation-1.3.0 (androidx.annotation:annotation:1.3.0) and annotation-jvm-1.7.0.jar -> annotation-jvm-1.7.0 (androidx.annotation:annotation-jvm:1.7.0) Go to the documentation to learn how to Fix dependency resolution errors. 怎么解决
最新发布
10-18
<think>首先,用户的问题是:解决Android开发中出现的Duplicate class androidx.annotation相关类重复问题,具体是annotation-1.3.0.jarannotation-jvm-1.7.0.jar中的类重复问题。 用户提供了几条引用,但这些引用并不直接相关。引用[1]是关于强制依赖版本的解决方法,引用[2]、[3]是关于Scala编译器错误,引用[4]是关于插件未找到的错误。用户说这些引用仅供参考,不是真正的需求,我需要参考用户上一次的问题我的回答。但我没有之前的上下文,所以基于当前问题回答。 关键点是:Duplicate class错误,涉及两个库:annotation-1.3.0.jarannotation-jvm-1.7.0.jar。这些库可能有相同的类,导致冲突。 在Android开发中,常见的解决方案是: 1. **排除冲突的依赖**:使用Gradle的exclude语句排除特定的依赖。 2. **强制依赖版本**:就像引用[1]中提到的那样,强制使用一个特定的版本。 3. **检查依赖树**:找出为什么会引入这两个库,并解决冲突。 用户提到的库:annotation-1.3.0.jar可能是androidx.annotation:annotation:1.3.0,而annotation-jvm-1.7.0.jar可能是kotlin-stdlib的一部分,因为kotlin-stdlib包含kotlin-annotation-jvm。 在Kotlin项目中,kotlin-stdlib-jdk8或类似依赖可能包含kotlin-annotation-jvm,而Android项目可能直接依赖androidx.annotation。如果版本不匹配,可能导致重复类错误。 解决方案步骤: 1. **检查依赖树**:运行命令查看依赖树,找出冲突的来源。 - 在终端运行:`./gradlew :app:dependencies` 或针对特定模块。 2. **排除冲突模块**:在Gradle文件中排除重复的依赖。 - 例如:exclude group: 'org.jetbrains.kotlin', module: 'kotlin-annotation-jvm' 3. **强制统一版本**:使用resolutionStrategy强制使用一个版本。 - 类似引用[1],但针对具体的库。 用户的问题是关于androidx.annotationannotation-jvm。annotation-jvm可能是kotlin.annotation.jvm或类似的。 一个常见的原因是Kotlin版本AndroidX库的版本不兼容。例如,Kotlin插件版本可能引入了annotation-jvm,而Android项目依赖了androidx.annotation。 具体步骤- 确认项目使用的Kotlin版本。如果使用Kotlin,确保kotlin-stdlib的版本与AndroidX兼容。 - 在build.gradle中添加排除规则。 例如: ```groovy dependencies { implementation('some-library') { exclude group: 'org.jetbrains.kotlin', module: 'kotlin-annotation-jvm' } } ``` 或者,全局强制版本: ```groovy configurations.all { resolutionStrategy { force 'androidx.annotation:annotation:1.7.0' // 或所需的版本 // 或者 force 'org.jetbrains.kotlin:kotlin-annotation-jvm:某个版本' } } ``` 但需要确保版本兼容。 用户提到annotation-1.3.0.jarannotation-jvm-1.7.0.jar,所以androidx.annotation可能是1.3.0,而kotlin.annotation-jvm是1.7.0。建议升级androidx.annotation到1.7.0或更高版本以匹配,或者降级Kotlin版本。 最佳实践是更新到兼容版本。 在回答结构中,需要清晰、逐步解决。使用中文回答。 生成相关问题:在结束时生成相关问题。 引用标识:在回答中自然添加引用标识。用户提供的引用[1]是关于强制依赖的,可以引用它。 回答结构: 1. 解释问题:简述Duplicate class错误的原因。 2. 解决方案步骤- 步骤1:检查依赖树。 - 步骤2:排除冲突依赖或强制版本。 - 步骤3:同步项目。 3. 代码示例:提供Gradle配置代码。 4. 生成相关问题。 使用系统级指令: - 行内表达式用$...$,例如依赖版本。 - 独立公式不需要,因为没有数学公式。 - LaTeX语法正确。 - 中文回答。 最终回答草稿: ### 解决Duplicate class错误 在Android开发中,当遇到Duplicate class错误,尤其是涉及androidx.annotationannotation-jvm时,通常是由于依赖冲突造成的。annotation-1.3.0.jar对应androidx.annotation库,而annotation-jvm-1.7.0.jar可能来自Kotlin标准库的kotlin-annotation-jvm模块。由于使用了不同的命名空间但相同的类名,导致冲突。 #### 解决方法 1. **检查依赖树**: 首先,查看项目的依赖树,找出哪些库引入了冲突的模块。 - 在终端运行命令: ``` ./gradlew :app:dependencies ``` 这会显示所有依赖项,搜索androidx.annotationkotlin-annotation-jvm。 2. **排除冲突模块**: 在build.gradle文件中,排除引起冲突的依赖。 - 例如,如果kotlin-stdlib引入了kotlin-annotation-jvm,可以修改依赖: ```groovy dependencies { implementation('org.jetbrains.kotlin:kotlin-stdlib') { exclude group: 'org.jetbrains.kotlin', module: 'kotlin-annotation-jvm' } // 其他依赖 } ``` 3. **强制依赖版本**: 或者,使用resolutionStrategy强制使用统一的版本,就像引用中提到的[^1]。 - 在app的build.gradle中添加: ```groovy android { // 配置 } configurations.all { resolutionStrategy { force 'androidx.annotation:annotation:1.7.0' // 强制使用1.7.0版本 // 确保版本兼容 } } ``` 这样确保所有依赖都使用相同的androidx.annotation版本。注意:如果annotation-jvm是必需的,可能需要排除或匹配其版本。 4. **更新依赖版本**: 确保所有依赖使用兼容的版本。建议更新androidx.annotation到最新的稳定版,如1.7.0或更高。 5. **同步项目**: 修改后,同步Gradle并清理构建(如运行./gradlew clean build)。 #### 注意事项 - 如果项目使用Kotlin,检查Kotlin插件版本,确保与AndroidX库兼容。 - 强制版本可能会导致其他兼容性问题,测试应用以确保功能正常。 §§相关问题§§ 1. 如何解决Android开发中的其他依赖冲突问题? 2. Kotlin与AndroidX库的版本兼容性如何管理? 3. 有哪些工具可以帮助分析Gradle依赖树? </think>### 解决Android开发中Duplicate class androidx.annotation类重复问题 在Android开发中,当遇到`Duplicate class`错误(涉及`annotation-1.3.0.jar``annotation-jvm-1.7.0.jar`时),通常是因为Kotlin标准库的`kotlin-annotation-jvm`模块与AndroidX的`androidx.annotation`库存在冲突。两者都包含`androidx.annotation`命名空间下的相同类(如`NonNull`、`Nullable`),但版本或实现不同,导致Gradle构建失败。以下是系统化的解决方案: #### 解决步骤 1. **检查依赖树,定位冲突源** 运行Gradle命令分析依赖关系: ```bash ./gradlew :app:dependencies --configuration releaseRuntimeClasspath ``` 搜索输出中的`androidx.annotation``org.jetbrains.kotlin:kotlin-annotation-jvm`,确认哪个库引入了冲突版本[^1]。 2. **排除冲突模块(推荐方法)** 在`app/build.gradle`的`dependencies`块中,对引入`kotlin-annotation-jvm`的库添加排除规则: ```groovy dependencies { implementation('org.jetbrains.kotlin:kotlin-stdlib') { exclude group: 'org.jetbrains.kotlin', module: 'kotlin-annotation-jvm' } // 其他依赖项,例如: implementation 'androidx.annotation:annotation:1.7.0' // 统一使用较新版本 } ``` 此操作显式移除`kotlin-annotation-jvm`,保留AndroidX的注解库。 3. **强制统一依赖版本** 如果排除无效,使用全局版本强制策略(参考引用[^1]): ```groovy configurations.all { resolutionStrategy { force 'androidx.annotation:annotation:1.7.0', // 强制AndroidX版本 'org.jetbrains.kotlin:kotlin-annotations-jvm:1.7.0' // 对齐Kotlin版本 // 检查兼容性:确保Kotlin插件版本 ≥ 1.7.0 } } ``` 4. **更新KotlinAndroidX版本** 在项目根目录的`build.gradle`中,确保使用兼容版本: ```groovy buildscript { ext.kotlin_version = '1.7.0' // 建议 ≥ 1.7.0 dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } ``` 同步后,清理构建缓存: ```bash ./gradlew clean ``` #### 注意事项 - **版本兼容性**:AndroidX Annotation 1.7.0 需搭配 Kotlin ≥ 1.7.0,否则可能引发新问题[^1]。 - **验证修复**:构建后检查日志,确认错误是否变为`warning`或消失。 - **备选方案**:若冲突持续,临时移除`kotlin-stdlib`依赖,仅保留`androidx.annotation`。
评论 6
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值