I have one project with two modules: the app and the lib.
Obviously the app module depends of the lib module.
However I need that the lib module uses resources (image drawables) from app module. Is possible?
No, because all library modules are compiled first, and they do not have any access to the application resources at this point. A library module must include all of the resources it is dependent on by definition. Only when the application is compiled do its resources get built into the relevant R classes, at which point the library is already fully compiled.
No. The problem is that resource id-s are assigned dynamically, so you can't refer to a resource from a library, since its id is unpredictable.
Related
I want to generate a fat-aar of an app module and use it as a dependency in another app.
I have used this library to generate the fat-aar.
I have an app module which is converted as a library module and a feature module called splash and Splash module is included in the app module.
With the help of above library I have successfully generated the fat-aar, but this fat-aar doesnot include the drawable resources of Splash module which crashes the app at run time with below exception.
java.lang.NoSuchFieldError: No static field ic_app_splash of type I in class
Lcom/app/passenger/R$drawable; or its superclasses
This ic_app_splash is present inside the splash module.
Please provide the solution as why the fat-aar is not including the resources of a module.
I suppose that is Issue 406. Reading the associated issues your problem might be solved with the answer to Issue 19 and the corresponding explanation by the lib's dev which, in short, is:
The support lib version that your application uses is best to be the same as the library uses.
Note, that the developer also states:
I am no longer engaged in research and development, so the project will not be updated and maintained.
However you can also use the probably more common way, and either create AARs with Android Studio or via CLI with AAPT.
I have read some sample codes, I find that many project use library module structure, you can see Image A.
Could you tell me the benefit to use library module in Android Studio ?
What code do I need to place it in library ?
And More, both app and lib module use the same namesapce in the sample code, I don't know if it's suitable, could you tell me ?
Image A
Library module gives you two options to create library Android and Java.
Android library module-> Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module . It allows you to add android specific components like resources and manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
Java Library -> It builds a JAR file. JAR file is useful for many projects especially when you want to share code with other platforms. But it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So when you do not need any android specific resources in library you should create a java library.
Library modules are beneficial for projects :-
When you're building multiple apps that use some of the same components, such as activities, services, or UI layouts.
When you're building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both.
Quoted from developer.android.com
Other than that same namespace is not problematic unless you have same package name inside App and libraries . You should use a different namespace for libraries.
PS-> If you are familiar with Clean Architecture, The idea behind most of the software design pattern is Separation of concern . In Clean architecture a project is divided into multiple modules. When you implement clean architecture in android you'll see that some of the module you can create as Java library like domain module. Creating module is really useful to follow re-usability and SOLID principles and Inversion of control.
Firstly, don't look into the package name declared in the java directory. Look into the manifest file. You can see that these modules have different package name. It means that all modules in a project must have different package name.
Regarding to your question, what are the benefit of naming library module as lib?
There's no benefit at all. Some people are comfort with lib name, so they can differentiate the demo and library module easily. However, using lib as library's module name requires you to add additional configuration in the lib/build.gradle, i.e. archiveBaseName. This Gradle attribute will rename the JAR/AAR from lib.aar to work-runtime.aar, so people can use it like this:
implementation "androidx.work:work-runtime:$work_version"
If archiveBaseName is not set, people will use it like this:
implementation "androidx.work:lib:$work_version"
In real case, let's take my open source library as the example, MaterialPreference. I used to use lib name on this project, but now I think lib is not a good module name. Using materialpreference as module name will remove additional configuration archiveBaseName. So I feel it is more simple.
I have the following structure of android project
application module
base feature module
feature module
a local library project
remote libraries
When I try to include the local library project in base feature module with
api or implementation, the feature either not compiling because of not found resources or throwing the same errors at runtime.
And now I am wondering is there a point which I misunderstood. The documentation says the base feature module is intended to share the common code and the resources, but it seems it doesn't cover the library cases.
The runtime resource not found errors are thrown when I try to include the same library also in the feature module.
When referecing resources from base module in your feature modules you need to use the full packageName qualifier.
This is because in instant apps they will belong to another name space.
Hence:
R.id.resource will point to resources of your feature module
com.yourdomain.appbase.R.id.resource will reference resources of your base module
I have a multi-module project with base-feature and feature modules.
The problem is that after the build the resources from feature module are duplicated with a second id and the resources are not merged properly in the end. Is there a something about the gradle plugin which should be done to make this run? ==> com.android.feature
When I run the project it tries to find the resource from feature module with the feature module resource id, but in the end the main module has the same resource already with another id, which I guess should be merged.
This is an intended behavior. Please make sure that your resource IDs are unique. Refer to this Android Instant Apps FAQ: “Can I share resources between features?”
https://developer.android.com/topic/instant-apps/faqs.html#project-structure
However, you must keep the IDs for your resources unique between your
dependent features and base feature. For example, if your base feature
provides a resource with the ID R.id.feature_layout and a dependent
feature defines another resource with the same ID, the instant app
uses the resource from the base feature instead of the dependent
feature.
My desired end result is to have a Project that can output different productflavors of Mobile, Tv, and Wear apps, that share the same code-base. So I would like the codebase to be a dependency for all three app-types, where each app module would only contain activities and interaction with the codebase.
My first idea was to add a "Android Library Module" to my newly created project, but I noticed that this is really just a new app module with its own resources and everything. I would like the codebase to function more like a "Java Library", but it needs access to packages like "android.graphics.Color".
So in short, is the correct way of achieving this result to use a java library that has a reference to an android sdk or am i just going about this the wrong way?
Continuation of this question at:Does an Android Library need a manifest ,app_name,Icon?
There's no in-between. If you want access to Android APIs, then the library needs to be an Android library so that the build system can properly link it in to dependent projects. It's true that Android Libraries have resources and other things you may not need, but you can ignore those bits and treat it essentially as a plain Java library if you wish. Even if you're not using resources, you may find useful the ability to specify AndroidManifest.xml attributes to be merged into the dependent app.
The Android Library project doesn't build a fully-fledged APK as its output; it generates an AAR, which is conceptually similar to a JAR archive, but has resources and meta-information useful to Android projects.
Supplemental answer defining terms
The Android Studio documentation defines a module as follows:
A module is a collection of source files and build settings that allow
you to divide your project into discrete units of functionality. Your
project can have one or many modules and one module may use another
module as a dependency. Each module can be independently built,
tested, and debugged.
So basically a module is a subproject in your bigger project.
Types of modules
Android app module - The app module is usually what you are working with in a normal project. When you compile it you get an APK file that will run on a device. Here are the different types of app modules that exist:
Phone & Tablet Module
Android Wear Module
Android TV Module
Glass Module
Library module - The purpose of a library is to share code. For example, you could have a project with a couple different app modules. The common code that they both use could be located in the library.
Android Library - In addition to Java code, this allows you to also include Android resource files and a manifest. If you are making an Android project and are wondering what kind of library to use, then choose the Android Library. When compiled it creates an AAR (Android Archive) file.
Java Library - This only allows you to include Java code files, no Android resource files. This is useful for cross-platform code sharing. When compiled it creates a JAR (Java Archive) file.
Google Cloud module - This type of module is the Google Cloud backend for communication with your client side app.
One additional point that I've not seen well documented: An android library module can have a dependency on another android library module or java library module, but a java library module cannot have a dependency on an android library module.