Using an Android Library Project Activity Within Another Project

Using an Android library project Activity within another project

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.

How to use a project in another project as a module?

settings.gradle in root project include each project.

include ':loginApp'
include ':signupApp'
include ':resetpasswordApp'

project(':loginApp').projectDir = file('path/to/LoginApp')
project(':signupApp').projectDir = file('path/to/SignupApp')
project(':resetpasswordApp').projectDir = file('path/to/ResetpasswordApp')

build.gradle Of main module

implementation project(':loginApp')
implementation project(':signupApp')
implementation project(':resetpasswordApp')

What is the right way to insert an activity in another project?

You need to extract these components in a separate module:

A module is a discrete unit of functionality which you can compile,
run, test and debug independently.

Modules contain everything that is required for their specific tasks:
source code, build scripts, unit tests, deployment descriptors, and
documentation. However, modules exist and are functional only in the
context of a project.

Then, include that module in all projects using it.

In fact, you can create the module in an independent "library" project of its own. And add it as a dependency for all projects using it.

Going a step further, you can publish the output of an open source library project as .aar or .jar on maven central, jcenter and other public repositories. Then other people will also be able to use your module.

Some important points to remember when creating android library projects:

  1. The resources (strings, layouts, xmls, images) of a library will be merged with those of final project on build. Still your module's R class will stay under your module's package name.

  2. Some attributes from manifest file of library might be merged to that of final project (like that of <application> ). So, a library module should have a minimal manifest file, with at most, the package name.

  3. You may include an example application project inside a library module, but do not redestribute the example app project with library. It will cause trouble for someone building an application using your library.

Unable To Access library activity in another project

I doubt that <activity> tag declaration in manifest file will work but
give a try and do include this in manifest file:

<uses-library android:name="com.themontcalm.droid.lib.activityr"
android:required="true" />

Using Activities from Library projects

The problem was being caused by the R files from each of the Library projects not correctly being built and referenced. The resource files are built and referenced directly from the calling project.

Each of the Libraries need to have unique package as defined in the AndroidManifest.xml. This leads to each of it's resources being compiled in that unique namespace within the calling project alongside the Library jar which contains the class files.

The <code>R</code> files located in situation

The problem becomes intermittent when switching between a library and a runnable project because a clean and build needs to take place to regenerate these files as it isn't done automagically when unclicking the Use as library checkbox, where as the jar (and java classes) don't require as much coaxing for them to be correctly referenced as the library projects references them when acting as a Library.

This can lead to intermittent and also varying errors including missing references, DexOp and NullPointerExceptions depending on to what degree the R.java files have been mangled or partially built and what conflicts are taking places between packages.



Related Topics



Leave a reply



Submit