1

Please help me how to call activity from a jar file in android:

My steps:

  • I had a AndroidLibrary project with a MainActivity.java

  • I had a AndroidApp project with a SplashActivity.java.

  • I imported AndroidLibrary.jar to libs folder of AndroidApp project and add build path
  • I added path MainActivity.java in AndroidManifest.xml file of AndroidApp project
  • In class SplashActivity.java i used code like this to call MainActivity.java from AndroidLibrary.jar

    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
    startActivity(intent);
    

However, when I call an activity from jar file , it immediately fails with ClassNotFound exception.

TryinHard
  • 4,078
  • 3
  • 28
  • 54
ngocquynh_183
  • 125
  • 2
  • 6
  • What do u mean by `when I call an activity from jar file` – Pankaj Kumar Jun 18 '13 at 10:07
  • 1
    Post your manifest and logcat – Charan Pai Jun 18 '13 at 10:10
  • have you added the MainActivity with correct package prefix to it? – Adil Soomro Jun 18 '13 at 10:11
  • 1
    your Android App should be the part which keep track of the objects .etc. So, your app should call any libraries to get something done.. Not the other way around.. Pleas explain the intention of doing this. There should be a better way. Libraries are there so you can use that code fairly independently. – Madushan Jun 18 '13 at 10:12
  • look also: http://stackoverflow.com/questions/8612429/calling-activity-within-jar-file – Seraphim's Jun 18 '13 at 10:19
  • First, adding the JAR is pointless, in part because that JAR isn't designed for use independently from the rest of the library project. Attach the *entire* library project. Second, if you are using Eclipse with the R22 tools, be sure to fix the "Order & Export" sections of both projects: http://stackoverflow.com/questions/16596969/libraries-do-not-get-added-to-apk-anymore-after-upgrade-to-adt-22/16596990#16596990 – CommonsWare Jun 18 '13 at 10:58
  • Please post a complete, **small** example that recreates the issue you are having and post the **complete** error message. If you set up your main Android app project correctly, the library project will be compiled into your APK. – Code-Apprentice Aug 17 '13 at 19:47

4 Answers4

3

To call just an Activity from an external .jar, you need to:

  1. Create an Intent
  2. Call a setClassName with two String parameters: The package containing your class, and the entire path of the class that you need to call
  3. Start the activity.

For example:

final Intent it = new Intent();
it.setClassName("com.example.test", "com.example.test.NameOfYourActivity");
startActivity(it);

If you want to know how to convert Java bytecode to Dalvik bytecode, visit my blog

Thanks.

0

Alernate piece of code that works

final Intent it = new Intent(android.content.Intent.ACTION_VIEW);
it.setClassName(getApplicationContext(), "<complete path of the Activity class>");
startActivity(it);
Varun Bhatia
  • 4,326
  • 32
  • 46
0

Android Calling Library activity in another new project

Intent callIntent = new Intent(MainActivity.this, com.example.testinglib.Testing.class);
startActivityForResult(callIntent, 0);

Note: startActivity(intent); is not working.

Vivek
  • 343
  • 1
  • 5
  • 12
0

follow steps,

  • Create an Intent.
  • Call a setClassName with two String parameters: The package containing your class, and the entire path of the class that you need to call.

  • Start the activity.

That's it.

Ganpat Kaliya
  • 888
  • 2
  • 9
  • 16