Android Common Resource as library for Android Application with Modular structure - android

I have Android res folder with some xml and images.
What need to be done
Need to put all common resources in separate module and other module will be referring this module for UI creation .
What i have tried
I have created Android Library Module and kept only res folder and in my Android Instant App project structure. I have given the reference of Android Library in base module build.gradle.
As other module have already reference of app module in their respective build.gradle then ideally it should work but its not working
Error : Android Linkage failed

In your settings.gradle add this:
include ':yourLibraryName'
project(':yourLibraryName').projectDir = new File(settingsDir, '/path/to/yourLibraryName')
Then go to your projects' build.gradle file and add this in the dependency:
dependencies{
implementation project(':yourLibraryName')
}
Then you should be able to access library's files.

Related

App Bundle - Dynamic feature modules : Base project not found in dynamic feature module error

I am working on a gradle android project which has custom project structure. We used sourceSets.main apis to make mappings for "AndroidManifest.xml ", "res" and others. There are no issues with this set up and all the functionality works fine.
In the project we are planning to implement dynamic feature module. As part of project configuration I have followed all the steps mentioned in android documentation https://developer.android.com/studio/projects/dynamic-delivery#feature_build_config.
As part of instructions, one has to put, base module as a dependency of the dynamic feature module, like below
dependencies {
// Declares a dependency on the base module, ':app'.
implementation project(':app')
}
When I compile the project, build is failing with below error, ("KSApp" is my main project name and "dynamic_feature" is dynamic feature module)
"Project with path ':KSApp' could not be found in project ':dynamic_feature'."
Can some one please explain, whats going wrong and how do I put base module as my dependency in dynamic feature module ?
What I tried :
Using implementation project(${project.rootDir}") in dependencies section of dynamic feature module.
Using implementation file(${project.rootDir}") in dependencies section of dynamic feature module.
Note :
I am able to successfully implement dynamic feature module in a regular projected created in Android studio. I see problem only in project with custom project structure .
Issue is in referring base module form sub module.
As my project has custom android structure, base module is in root project folder. In this case, to refer base module from sub module one should use below approach:
dependencies {
implementation project(':')
}
This is solving the issue.

Packing run time dependecies into aar library

I build an Android Application which contains a module as library. Most of the logic is inside the LibraryModule. The library module has several other dependencies which are included as gradle dependencies
Application
* LibraryModule
*implementation ('dependency1')
*implementation ('dependency2')
The library module is include in the application's build.gradle like
implementation project(":LibraryModule")
The application works fine then.
But when I first build the Library module as an aar file using
gradle :LibraryModule:assemble
and then using the applications build.gradle to include the aar
implementation(':LibraryModule#aar') The application compiles. But several of the classes of the dependencies (dependency1, dependency2) which are required at run time are missing from the aar.
Is there some way to include all the contents of the dependency1 and dependency2 in the aar file so that run time dependecies is also packed together in the aar file.
I have done some googling and found out that fat aar is an option. Is there some way to include all the class file from the dependeices also into the aar file which are also needed at run time ?
Building a fat-aar is not officially supported by Android. There are some similar discussions at Android gradle multiproject dependency resolution

Android library project debugging quickly

I am developing a library project. Right now my debugging process is something like this :
Make some changes in library code
build it into an aar file
use the aar file in main app project as new library module
then debug the code changes in lib code.
As you can see it's a long process and takes a lot of time to debug library. What is the proper way for library development using android studio?
You can do this way:
Add library module in same project directory like app folder : File-> new-> new module -> Android Library.
Add your library module name in setting.gradle (eg. include ':app',':xyz') : should be added automatically
Right click on your main app folder-> open module settings -> add your library as dependency to "app" (it's under module dependency).
Go to build.gradle file of your main project, add module like compile project (':xyz')
No need to create aar , just make changes in lib module and Run your main project and debug it .
Hope this will help you !
I would include a demo-app project in parallel to library project:
// /settings.gradle
include ':app', ':lib'
// /app/build.gradle
depencency {
compile project(':lib')
}

Gradle: Shared java project with an android project

I've got this shared common classes that I extracted into a separate "common" module with gradle (which has dependencies on some jars). I defined it as a java project because I want to also use it outside of the android context. But when I include it in my android project with:
compile project(':my_common')
It doesn't seem to work.

`rootProject` in a Gradle module that's imported to another project

In my current setup I have two top-level Gradle projects:
A library project VideoPresenter with modules
videopresenter-common
videopresenter-exoplayer
videopresenter-visualon
where both videopresenter-exoplayer and videopresenter-visualon depend on videopresenter-common.
All three of the modules depend on OkHttp so I defined a version variable in my top-level build.gradle:
ext {
okhttp = 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
}
which I use in the three other build.gradles:
dependencies {
compile rootProject.ext.okhttp
}
So far, this is completely analogous to the way, for example, RxBinding is set up. And it seems to work as long as I compile the modules from within this project.
However, I also have an application project that uses one or more of these modules. Let's say the settings.gradle of that project includes the following:
include ':videopresenter-common'
include ':videopresenter-exoplayer'
project(':videopresenter-common').projectDir = new File('../VideoPresenterAndroid/videopresenter-common')
project(':videopresenter-exoplayer').projectDir = new File('../VideoPresenterAndroid/videopresenter-exoplayer')
Now, when I try to compile the application project Gradle complains because it
Cannot get property 'okhttp' on extra properties extension as it does
not exist
presumably because rootProject now points to the top-level build.gradle of my application project.
If I add the property there the project compiles. However, I don't want to have to "inject" the correct version number from the main project into the library project. Is there a way to centrally declare the property in the library project so that it also applies when the module is imported into another project?
If the goal is to get the value from the library then you could just get the library project's rootProject then reference the ext like before. Being specific about what project we are looking for should provide the expected result.
dependencies {
compile project(':videopresenter-common').rootProject.ext.okhttp
}
As long as the project is in the settings.gradle you should be able to reference it's extension.

Categories

Resources