Android Studio java-library module classpath NoClassDefFoundError
2022-09-04 23:35:44
我的项目开始很大,所以我决定把它分成单独的模块 - 和.我的模块一定不知道它是Android项目的一部分,所以我创建了模块并在那里放了一些类。它像一个魅力一样工作,但是当我在模块中添加一些其他依赖项(如RxJava)时,存在问题。我创建了一个类来简单地测试我的代码。生成传递,但当它尝试调用 RxJava 类时,它会崩溃,出现异常:gui
domain
domain
java-library
java-library
Main
Exception in thread "main" java.lang.NoClassDefFoundError: io/reactivex/Observable
at com.example.domain.Main.main(Main.java:13)
Caused by: java.lang.ClassNotFoundException: io.reactivex.Observable
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
我的主类:
package com.example.domain;
import io.reactivex.Observable;
public class Main {
public static void main(String[] args) {
Observable.just(1)
.subscribe(integer -> System.out.println(integer));
}
}
我的文件:build.gradle
apply plugin: 'java-library'
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
api 'io.reactivex.rxjava2:rxjava:2.1.2'
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
1)如果我使用代替它的工作原理com.android.library
2)如果我将RxJava库添加为文件,它不会更改任何内容.jar
编辑:
我查看了文件,发现了一些奇怪的东西:Domain.iml
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":Domain" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
<option name="GRADLE_PROJECT_PATH" value=":Domain" />
</configuration>
</facet>
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="true" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/build/classes/java/main" />
<output-test url="file://$MODULE_DIR$/build/classes/java/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.0" level="project" />
</component>
</module>
奇怪的部分是节点中的属性scope
orderEntry
<orderEntry type="library" exported="" scope="PROVIDED" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" scope="PROVIDED" name="reactive-streams-1.0.0" level="project" />
如果我手动删除它:
<orderEntry type="library" exported="" name="rxjava-2.1.2" level="project" />
<orderEntry type="library" exported="" name="reactive-streams-1.0.0" level="project" />
它就像一个魅力。
不幸的是,如果我同步我的文件,它会覆盖我的修改。build.gradle
如何使它永久工作,为什么它会这样?