在另一个项目中使用Android库项目Activity

Mar*_*los 38 android android-library android-activity

我有一个Android库项目,我想在另一个Android项目中使用.

该库在其AndroidManifest中声明了一个Activity.当我在第二个项目中尝试以下内容时:

        Intent intent = new Intent(this, ReaderActivity.class);
        startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

我得到以下异常:

 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)
...
Run Code Online (Sandbox Code Playgroud)

如何从另一个项目中打开活动?

编辑:根据用户的答案,我将以下行添加到我的第二个项目中

<uses-library android:name="br.com.digitalpages.reader" android:required="true" />
Run Code Online (Sandbox Code Playgroud)

但它仍然不起作用

mah*_*mah 36

我相信你必须在自己的AndroidManifest.xml中包含<activity> - 我认为它不会从库中获取.我没有这方面的参考.

更新:这是官方解决方案.来自doc:

在清单文件中声明库组件

在应用程序项目的清单文件中,必须添加应用程序将从库项目导入的所有组件的声明.例如,您必须声明任何 <activity>,<service>,<receiver>,<provider>,等等,还有 <permission>,<uses-library>和相似的元素.

声明应在适当的时候通过其完全限定的包名引用库组件.

  • 添加jar会将代码添加到你的apk中,但如果不存在清单条目,Android将不知道任何活动(或服务或接收者)...只是代码不够,你需要告诉Android寻找它. (2认同)

she*_*tal 20

Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("packagename//ex-com.hello", 
                                     "classname//ex-com.hello.ExampleActivity"));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

并确保在库中您已声明了活动.您无需在当前项目的清单中声明库活动.