1

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)
Nav
  • 467
  • 4
  • 18

1 Answers1

1

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.

Mohammed Ali
  • 2,758
  • 5
  • 23
  • 41
  • Creating library project is not my requirement. I have to create a jar file :( . – Nav Dec 03 '14 at 16:19
  • The jar will be automatically added to your projects when you run it. Then you can access the class of first jar. Try it and tell me. – Mohammed Ali Dec 03 '14 at 16:22
  • ya i tried all your steps and it is working. But my requirement is to create a wrapper around first.jar by second.jar. But if i do your step then its not creating a wrapper. first.jar is present in android dependencies of project C. – Nav Dec 03 '14 at 17:20
  • so what you want is only import second.jar and first.jar will be inside the sencond.jar. – Mohammed Ali Dec 03 '14 at 17:38
  • Exactly.. Use classes of first.jar which is inside second.jar – Nav Dec 03 '14 at 17:45
  • Why do that when you can do the simple way? see this: http://stackoverflow.com/q/179024/3879470 ; it might solve your problem, i am not sure – Mohammed Ali Dec 03 '14 at 17:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66145/discussion-between-nav-and-mohammed-ali). – Nav Dec 03 '14 at 18:12