简体   繁体   中英

In android create a jar inside jar and accessing the classes of first jar

My question is simple, is it possible to create a jar inside a jar in android.

Like i have a first.jar file and i am using first.jar class methods in another project class for example

In first.jar i have class TestApp1Class,

public class TestApp1Class {

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

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

In another project B i have class TestApp2Class where i used first.jar Class methods like testMethod1(); as you can see below.

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();

}

}

This project B i exported as jar file name second.jar and used this jar in another project C.

Like,

public class MainActivity extends Activity {

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

}

And i am getting exception at

java.lang.NoClassDefFoundError: com.example.testapp.TestAppMethods That it is not able to access class of first.jar. I have been bugged with this since two days. I think i have tried almost everything.

Steps which i have tried:

  • 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.

These are things which i have tried. Please mention am i doing anything wrong or missing anything.

Each time i tried all above steps i got NoClassDefFoundError exception.

Logcat:

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)

Try this Step by step:
1. Remove all library projects then Clean.
2. Go to first Project Right click Properties -> Android. Check Is Library.
3. Go to second Project Right click Properties -> Android in the Library section select Add -> Add first project as library.
4. Go to second Project Right click Properties -> Android. Also, Check Is Library.
5. Go to third project C Right click Properties -> Android. In the Library section select Add -> Add First and Second as library. (Do not make Proj C as library)
6. Now Clean everything and run Proj C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM