How to use an android module as app and library - android

I have an Android Studio project with an app module.
Currently, I am writing another module that should use the first one.
Can I have the first module (which is an app module) be used by the second one as a library?

you have to do like below
In your 1st app module gradle you have to add your second module implementation.
implementation project(':your_second_module_name')
Also you have to update one line in your setting.gradle
include ':app', ':your_second_module_name'

Related

How to add a module dependency while building an AAR

I created one library project with two modules. I added the 2nd module in the dependencies of the first module like this:
implementation project(":module2")
I am able to generate the AAR file after this. But I'm getting
java.lang.NoClassDefFoundError
while running the client app.
Kindly help me find out what I am missing here.
In your first module build.gradle file, you can include like ,
api project(":module2")

Android Common Resource as library for Android Application with Modular structure

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.

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.

aar dependency error in my android project using android-maven-gradle-plugin

I have a problem when using the aar module dependency aar module as the below issue explains.
https://github.com/dcendents/android-maven-gradle-plugin/issues/65
I have a project such like a client-server structure.
It has 3 modules:
app (my server to provide some service) -- apk module
sdk (my sdk to provide some API to communicate with my app) -- aar module
common (something which should include both in my app module and sdk module ,such as utils,beans,etc. --aar module
I do not want to write it in my both app and sdk modules, debugging and modifying should work twice.
Any idea how I can resolve this?
I'm not sure exactly what you did wrong there, but I've created/modify those gradle scripts and they're working great: https://github.com/sensorberg-dev/gradle-scripts
I can see you're trying to "install" on your local maven. To do that, you have to add this line at the end of the build.gradle from the common module this line:
apply from: 'https://raw.githubusercontent.com/sensorberg-dev/gradle-scripts/master/publish-to-local-maven-repo.gradle'
and then add the metadata to the gradle.properties files, like this:
project gradle.properties:
POM_GROUP_ID=com.company.omg
POM_VERSION=0.0.1
POM_PACKAGING=aar
and common module gradle.properties:
POM_ARTIFACT_ID=common
then it's just call: ./gradlew clean installArchives

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')
}

Categories

Resources