我使用 Android Studio 创建了一个新项目并将一个 jar 文件复制到我的应用程序的库中。然后将其添加为库,这使得 IDE 可以识别它。后来,我把它添加到了build.gradle
,现在编译得很好。但是,当我尝试在设备上运行应用程序时,它会因NoClassDefFoundError
.
附上并按顺序:
- Android Studio 上的项目树。注意
test-jar-to-import.jar
里面HelloSheepProject/HelloSheep/libs/
。 - 的内容
MainActivity.java
。它正在尝试创建一个新的MyFile
. 没有其他的。 build.gradle
里面HelloSheepProject/HelloSheep/
。_ 我添加了行compile files('libs/test-jar-to-import.jar')
。- 的内容
MyFile.java
。它在 Eclipse 中创建并导出为 jar 文件。 - 当我尝试在设备上运行它时出现错误。
关于我所缺少的任何想法?我试过了./gradlew clean
,HelloSheepProject
但没有帮助。
package top.furrylamb.example.hellosheep;
import android.os.Bundle;
import android.app.Activity;
import top.furrylamb.example.MyFile;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFile myFile = new MyFile("hi", "there");
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
//compile fileTree(dir: "libs", include: "*.jar")
compile files('libs/test-jar-to-import.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 16
}
}
package top.furrylamb.example;
public class MyFile {
public final String left;
public final String right;
public MyFile(String left, String right) {
this.left = left;
this.right = right;
}
}
07-19 19:26:33.855 13656-13656/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: top.furrylamb.example.MyFile
at top.furrylamb.example.hellosheep.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)