// 这是Moudle的gradle配置 apply plugin: 'java-library' dependencies { // Android Studio 3.0下自动配置的 implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.squareup.okhttp3:okhttp:3.8.1' } sourceCompatibility = "1.7" targetCompatibility = "1.7"
// 这是Exception信息 Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/OkHttpClient at com.bignerdranch.android.lib.myClass.main(myClass.java:12) Caused by: java.lang.ClassNotFoundException: okhttp3.OkHttpClient at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
这是代码
public static void main(String args[]) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder(). url("http://www.baidu.com").build(); try { Response response = client.newCall(request).execute(); if (response.isSuccessful()) { System.out.println(response.body().string()); } } catch (IOException e) { e.printStackTrace(); } }
代码已经在UnitTest中测试 没有问题,能够正常打印出百度的页面,但是一到Moudle中运行就出现上述Exception