android ARouter踩坑之‘Program type already present: com.alibaba.android.arouter.routes.ARouter

扶高歌
2023-12-01

前言

在使用阿里的路由时 由于使用不当 在打包apk的时候

弹出


Program type already present: com.alibaba.android.arouter.routes.ARouter$$Group$$MyRouter2
Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
	at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:131)
	at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:118)
	at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:102)
	at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:37)
	at java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)
	... 4 more
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
	at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:81)
	at com.android.tools.r8.utils.ExceptionUtils.withD8CompilationHandler(ExceptionUtils.java:45)
	at com.android.tools.r8.D8.run(D8.java:94)
	at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:116)
	... 7 more
Caused by: com.android.tools.r8.utils.AbortException: Error: Program type already present: com.alibaba.android.arouter.routes.ARouter$$Group$$MyRouter2
	at com.android.tools.r8.utils.Reporter.failIfPendingErrors(Reporter.java:101)
	at com.android.tools.r8.utils.Reporter.fatalError(Reporter.java:72)
	at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:66)

问题

导致这个问题的原因是 因为重复使用了group
ARouter在多个module中不能使用同一个group

我在使用阿里的基类JsonServiceImpl 创建了@Route(path = "/service/json")两次

@Route(path = "/service/json")
public class JsonServiceImpl implements SerializationService {

    @Override
    public <T> T json2Object(String input, Class<T> clazz) {
        return null;
    }

    @Override
    public String object2Json(Object instance) {
        return null;
    }

    @Override
    public <T> T parseObject(String input, Type clazz) {
        return null;
    }

    @Override
    public void init(Context context) {

    }
}

解决方法

删除一个重复类JsonServiceImpl就可以了
@Route(path = "/service/json")

 类似资料: