java - 在 android 中创建一个 jar inside jar 并访问第一个 jar 的类

标签 java android maven jar noclassdeffounderror

我的问题很简单,是否可以在 android 中的 jar 中创建一个 jar。

比如我有一个 first.jar 文件,我在另一个项目类中使用 first.jar 类方法

在 first.jar 中我有类 TestApp1Class,

public class TestApp1Class {

public static void testMethod1(){
    Log.i("TEST", "Hi....");
}

public static void testMethod2(){
    Log.i("TEST", "Hello....");
}
}

在另一个项目 B 中,我有类 TestApp2Class,我在其中使用了 first.jar 类方法,如 testMethod1();,如下所示。

public class TestApp2Class {

public static void TestApp2Method() {
    Log.i("TESTAPP", "Hi TESTAPP2....");
     TestAppMethods.testMethod1();
}

public static void TestApp2Method2() {
    Log.i("TESTAPP", "Hello TESTAPP2....");
     TestAppMethods.testMethod2();

}

这个项目 B 我导出为 jar 文件名 second.jar 并在另一个项目 C 中使用这个 jar。

喜欢,

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TestApp2Class.TestApp2Method();
}

我在以下位置遇到异常

java.lang.NoClassDefFoundError: com.example.testapp.TestAppMethods 它无法访问 first.jar 的类。两天以来我一直被这个问题困扰。我想我几乎什么都试过了。

我尝试过的步骤:

  • Usually we get this error when it is not checked marked in order and export of buildpath. I have done that and also check marked on dependencies.
  • I tried just by putting second.jar in libs folder and not in buildpath and tried and it dint help.
  • I tried to edit manually MANIFEST.IN file and added one line Class-Path: libs/first.jar and kept first.jar in libs folder while exporting project B to second.jar and even that did not help. I think one shouldn't manually edit that MANIFEST.IN file.

这些都是我尝试过的。请提及我做错了什么或遗漏了什么。

每次我尝试上述所有步骤时,我都会遇到 NoClassDefFoundError 异常。

日志:

12-03 18:32:52.119: E/AndroidRuntime(29262): FATAL EXCEPTION: main
12-03 18:32:52.119: E/AndroidRuntime(29262): java.lang.NoClassDefFoundError:        com.example.testapp.TestAppMethods
12-03 18:32:52.119: E/AndroidRuntime(29262):    at com.example.testapp2.TestaApp2Class.TestApp2Method(TestaApp2Class.java:11)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at com.example.jartestapp.MainActivity.onCreate(MainActivity.java:14)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.app.Activity.performCreate(Activity.java:5133)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2311)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.os.Looper.loop(Looper.java:137)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at android.app.ActivityThread.main(ActivityThread.java:5214)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at java.lang.reflect.Method.invokeNative(Native Method)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at java.lang.reflect.Method.invoke(Method.java:525)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
12-03 18:32:52.119: E/AndroidRuntime(29262):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

逐步尝试:
1. 删除所有库项目然后清理。
2. 转到第一个项目右键单击属性-> Android。检查是库。
3. 转到第二个项目 右键单击​​“属性”->“Android”,在“库”部分中选择“添加”->“将第一个项目添加为库”。
4. 转到第二个项目右键单击属性-> Android。另外,检查是库。
5. 转到第三个项目 C 右键单击​​ Properties -> Android。在 Library 部分选择 Add -> Add First and Second as library。 (不要将 Proj C 作为库)
6. 现在清理一切并运行 Proj C。

关于java - 在 android 中创建一个 jar inside jar 并访问第一个 jar 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27272404/

相关文章:

java - 在类 block 内声明和实例化

java - SQL 中的选择语句、联接和重新调整查询用途

android - org.apache.harmony.xml.ExpatParser$ParseException(无效 token )

java - 在 Mapreduce 程序中使用 MulipleInputs 会出现错误

java - 无法在intellij idea/svn中提交pom.xml

java.lang.NoClassDefFoundError : org/hibernate/service/ServiceRegistry after building by Maven

java - 如何在 Gmail 中获取转发给外部用户的邮件的邮件附件?

java - 对 HashMap 中的值进行排序

android - 通过 Intent 向数据库发送一个字符串

java - android中定期向服务器发送数据