Fat aar does not include modules resources in Android - android

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.

Related

how to create library ( jar ) in android studio ? [duplicate]

This question already has answers here:
How to make a .jar out from an Android Studio project
(11 answers)
Closed 5 years ago.
I want to create some library that i will use in the future beside my current project.
I can't find a way to create library in android studio.
How to do it on android ?
A library module is useful in the following situations:
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.
In either case, simply move the files you want to reuse into a library module then add the library as a dependency for each app module.
To create a new library module in your project, proceed as follows:
Click File > New > New Module.
In the Create New Module window that appears, click Android Library, then click Next.
There's also an option to create a Java Library, which builds a traditional JAR file.
Give your library a name and select a minimum SDK version for the code in the library, then click Finish.
Once the Gradle project sync completes, the library module appears in the Project panel on the left.
If you don't see the new module folder, make sure it's displaying the Android view.
Visit https://developer.android.com/studio/projects/android-library.html
As introduction I would suggest you to peek into this conceptually simple tutorial. Basically you can start your own library module when you chose your project, without adding any Activity. Then you create your Java Class, usually with a View. When your library is ready, with all its business logic, you can glue everything inserting in the top level build gradle the instruction you are using a library, basically before you remove the following line, that is not needed for a library:
applicationId
(This line in your gradle file is a unique application ID that looks like a Java package name, that identifies your app to the device you are running and in google play)
and then you change this line:
apply plugin: 'com.android.application'
to:
apply plugin: 'com.android.name_library'
When you have a more structured project you can follow the official documentation
As it is well explained the difference between a normal Application and a Library is:
An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.
I would not encourage you to use solutions like web services that do it on your behalf, namely just copying/pasting your existing code. In fact a library often needs specific architectural choices, so is important to consider and learn different factors, is not just writing some business logic is quite complex to explain, but you can imagine that also the choice of what the user can see and modify can be crucial. Also should be as much as possible bug free, because once it is adopted could cause problems to the users. I remand you to a famous post, superbly written where you can find some solution to this aspect.
you can use https://jitpack.io/ is very easy publish an android library. just upload your code to github/bitbucket and paste the repository link on jitpack website. that's all

Android Studio 3 difference between library module and feature module

In Android Studio 3 there are at least two new module types. First is Instant app module and the second one is feature module. With Instant App module it's quite obvious but feature module from my perspective is the same as the library module. So what is the real difference between library and feature modules and when I should use library module and when feature module?
I would complete Marcin Orlowski scheme like this.
You could picture library module in the same way as dependencies of a given feature or base module.
Hence the library modules will not be packaged in Instant APP APK.
A feature module is a module that applies com.android.feature plugin.
This module type has a dual nature:
When consumed by an application (com.android.application) during build, it produces an aar and works just like a library
When consumed by an Instant App APK (com.android.instantapp), it generates an Instant App APK
Developers should write feature modules just like library modules. The tools provided are responsible for applying the correct nature when used during a build.
In the simplest case an Instant app can have a single feature module.
If there is more than one feature module, these feature-to-feature
dependencies can be defined through the api configuration. In any
case, there must only be a single base feature which is marked with a
baseFeature attribute.
Main source: https://codelabs.developers.google.com/codelabs/android-instant-apps/#3
This all for Instant Apps so you only need it if you are making your app supporting instant app feature
See https://developer.android.com/topic/instant-apps/getting-started/structure.html#basic-app
Android SDK is the core features and software tools that allow you to create an app for the Android Platform. An SDK contains lots of libraries and tools which you will use to develop your application.
A Library is a collection of pre-built compiled code which you can use to extend your application's features. For example, you may need to show some graphics in your application. Instead of creating this from scratch, you may choose to use a pre-built library someone else has developed which will give you the features you need thus saving you some time.
A module is a small part of the application which can be tested and debugged on its own without needing the whole application. This is same for any programming language. Suppose you are building an app with Login feature. To test if the login feature works, you don't need the whole app. Thus the Login part is a module of your application.
The app module builds an app. A library module builds a library.
An app is what a user uses. The output of an app module is an APK, the package of an Android application.
A library is a collection of code that represents something that you want to use in multiple applications or otherwise want to keep in a separate "container" than the rest of the app code. The output of a library module is an AAR And Jar.
Use Feature for linked feature of your instant app (to launch it with deeplink).
Use Library for code dependency in your app or in your Feature modules.

Adding application specific code to an application module that depends on a library module in Android Studio

I have a library module in my Android Studio project. Because I would like to publish a free version and a paid version of my app, I've added two application modules that depends on this library project.
If I would like to toast e.g. "FREE" for the free application module and "PAID" for the paid application module, how do I tell the difference of which application module that is active at run-time?
I've had a look at this question but I believe that it contradicts the reason for implementing the library module in the first place.
Initially I thought that I would add two different MainActivity.java in each of the application modules, each with a different Toast message. This did not work, since the library modules MainActivity.java was still shown.
So my questions are; Am I on the right path here, is this how you utilize library modules in Android Studio? If so, how do I add code that is specific to the application module, thus not implemented in the library module, "overriding the default" library module code?
Rather than going with libraries you may want to investigate application flavours under the gradle documentation:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-BuildType-and-Product-Flavor-property-reference

Android Studio Java Library Module vs. Android Library Module

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.

Resources in Android Library

I have problems with accessing resources in my android library. I have created library project with some resources (com.library) and then I imported module into application project (com.app). So I have Android Studio project with library and application.
When I want to access some library resource (com.library.R.string.label) I get error during compilation
package com.library.R does not exist
When I want to run some library method from application which contains/uses R.string.label I get
java.lang.NoClassDefFoundError: com.library.R$string
I added library to application gradle file using
compile project(':Library')
and from IDE perspective looks everything fine and R.java is created with references to resources. I want same usage as I'm using for example android.R.string.cancel or similar libraries in my application project.Where I'm doing mistake? Thank you for help.
PS: In the future I want to have my library project as aar package.
As soon as you add a library to your project all resources will be "copied" to the R-file of your app. So if you want to access a string from your library you don't do something like getString(com.library.R.string.some_string) you simply call getString(R.string.some_string) instead.
In terms of the library method: Would you mind sharing some code with us? Currently I can't imagine what's going wrong.
Problem was in wrong package name in gradle configuration files (after renaming).

Categories

Resources