44

I have an Android library project that I would like to use from within another Android project.

The library has a Activity declared in its AndroidManifest. When I try the following within the second project:

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

I get the following exception:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.digitalpages.reader.demo/br.com.digitalpages.reader.demo.ReaderDemoActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
...
 Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.digitalpages.reader.demo/br.com.digitalpages.reader.ReaderActivity}; have you declared this activity in your AndroidManifest.xml?
     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
...

How can I open the Activity from the another project?

EDIT: By users answers I added the following line into my second project

<uses-library android:name="br.com.digitalpages.reader" android:required="true" />

But it's still doesn't works

gonzobrains
  • 7,856
  • 14
  • 81
  • 132
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167

8 Answers8

42

I believe you must include the <activity> in your own AndroidManifest.xml -- I don't think it gets picked up from a library. I don't have my reference for that handy.

Update: It's official solution. From the doc:

Declaring library components in the manifest file

In the manifest file of the application project, you must add declarations of all components that the application will use that are imported from a library project. For example, you must declare any <activity>, <service>, <receiver>, <provider>, and so on, as well as <permission>, <uses-library>, and similar elements.

Declarations should reference the library components by their fully-qualified package names, where appropriate.

some.birdie
  • 2,259
  • 4
  • 24
  • 28
mah
  • 39,056
  • 9
  • 76
  • 93
  • I ended up creating a /libs directory and through the .jar in there. i wonder if doing this would replace that step. – owen gerig Aug 31 '12 at 20:09
  • 2
    Adding a jar gets code into your apk but if the manifest entry isn't there, Android won't know of any activity (or service or receiver)... just having the code isn't enough, you need to tell Android to look for it. – mah Aug 31 '12 at 23:04
  • @mah What about merging two Manifests!? – Dr.jacky May 12 '15 at 07:18
  • @Mr.Hyde I think "merging two manifests" is effectively what is being described in what I quoted. It's unfortunate that Google didn't simply provide a way for a primary manifest to import the library's manifest (or at least, when this question was posted they hadn't; I don't know what the past few years have brought). – mah May 12 '15 at 12:58
21
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("packagename//ex-com.hello", 
                                     "classname//ex-com.hello.ExampleActivity"));
startActivity(intent);

And make sure in library you have declared the activities. You don't need to declare the library activities in your current project's manifest.

sheetal
  • 3,014
  • 2
  • 31
  • 45
  • make sure in your library you have declared all the activities in Library manifest – sheetal Mar 12 '13 at 14:39
  • @sheetal made sure. declared all activities in Library manifest, but still same problem. Are there some restrictions for this solution? SDK version or something? – some.birdie Oct 18 '13 at 08:57
  • 1
    @sheetal by the way, the doc says that we must add declarations of all components in the manifest file of the application project: http://developer.android.com/tools/projects/projects-eclipse.html. – some.birdie Oct 18 '13 at 09:10
  • @Anastasia Yes In doc they have not declared the Library classes in the Main Project Manifest file but only the permissions.The Activity class declaration only in the Library's manifest should be OK..That worked for me – sheetal Oct 18 '13 at 10:44
  • @sheetal For me, it works only when all activities are declared also in app's manifest. =( Just as the doc says. I wonder how it worked for you. – some.birdie Oct 18 '13 at 10:59
  • Which App's manifest u are talking bout..In DOC it is mentioned you declare it in the Library Code Manifest file for Library Classes – sheetal Oct 18 '13 at 11:27
  • @Anastasia Here is what it is written" A library project's manifest file must declare all of the shared components that it includes, just as would a standard Android application. For more information, see the documentation for AndroidManifest.xml." – sheetal Oct 18 '13 at 11:28
  • @Anastasia Check in Creating Manifest File..Only while referencing you have to add permissions for library in current project – sheetal Oct 18 '13 at 11:30
4

did you add to the manifest?

http://developer.android.com/guide/topics/manifest/uses-library-element.html

Aleadam
  • 40,203
  • 9
  • 86
  • 108
3

This works:

In your library, put your custom Activity:

public class MyLibraryActivity extends ListActivity { ... }

No need to put it into a manifest. In your calling Android project, create an empty (dummy) wrapper:

public class MyActivity extends MyLibraryActivity { } 

and add reference to this class to your manifest:

<activity android:name="my_package.MyActivity" ... />
barnabas
  • 129
  • 2
  • 4
  • 5
    That will work however there's no need to extend MyLibraryActivity if you're not going to actually make any changes... you can simply refer to MyLibraryActivity directly in your manifest. You do, of course, need to use its fully qualified name if its package is other than that as declared in the manifest. – mah Jan 25 '12 at 23:13
1

I am aware that the question is quite old but I think this might help people like me that came up with the same problem.

Using Eclipse, the best way to share code and activities among libraries is probably the one that can be found in the android documentation here:

Managing Projects from Eclipse with ADT

qwlice
  • 556
  • 7
  • 23
1

When using an activity inside the library, the activity should be declared only inside the manifest of library. The activity can be started from the main app like this:

  Intent intent = new Intent();
        intent.setClassName(this, "com.duna.remotelocklibrary.activities.MainRemoteActivity");
        startActivity(intent);

I tried to start the library's activity like the following code, but i warn you: it doesn't work

  Intent intent = new Intent();
        intent.setClassName("com.duna.remotelocklibrary", "com.duna.remotelocklibrary.activities.MainRemoteActivity");
        startActivity(intent);
Duna
  • 1,564
  • 1
  • 16
  • 36
0

You don't need to explicitly add activity in your main project's manifest if you have already added the activity in your library's manifest by using the following code when starting library's activity.

For Kotlin

   val myIntent = Intent(activityContext, ActivityToLaunch::class.java)
    // Needed to set component to remove explicit activity entry in application's manifest
    myIntent.component = ComponentName(activityContext, PickerActivity::class.java)
    activityContext.startActivity(myIntent, PICKER_REQUEST_CODE)

For Java

    Intent myIntent = new Intent(activityContext, PickerActivity.class);
    // Needed to set component to remove explicit activity entry in application's manifest
    final ComponentName component = new ComponentName(activityContext, PickerActivity.class);
    myIntent.setComponent(component);
    activityContext.startActivity(myIntent, REQUEST_CODE);
minhazur
  • 4,928
  • 3
  • 25
  • 27
  • That's only valid for Gradle build, as the question was from Ant that does not had merge manifest task – Marcos Vasconcelos Sep 24 '18 at 14:49
  • I was able to make my app launch the library's activity by simply adding the activity declaration in the library's manifest. I didn't need to setComponent. – Adam Johns Jun 03 '20 at 17:49
0

you should import just the code of the activity ( not the manifest too ) , and then , declare your Activity ( of the library ) , in the manifest of your second project

Houcine
  • 24,001
  • 13
  • 56
  • 83