Strings from some library modules not included in application's R class - android

I have an Android app where application module depends on a few library modules.
Each of the library modules have: -
Different package names defined in their AndroidManifest.xml
strings defined in their res/values/strings.xml
Application module has a different package name defined in its AndroidManifest.xml
I am trying to access the strings from the library modules in my application module via
textView.setText(R.string.stringLibA);
textView1.setText(R.string.stringLibB);
However, I am getting compilation error when accessing R.string.stringLibB saying
cannot find symbol R.string.stringLibB
But it works fine if I access stringLibB using library's package name
textView.setText(R.string.stringLibA);
textView1.setText(com.package.libraryB.R.string.stringLibB);
Can someone please guide me what am I doing here that I cannot access libraryB's strings from application module's R class directly?
I apologize I could not share actual code from the project because of privacy concerns.
EDIT
My application module's build.gradle's dependency block
dependencies {
implementation project('libraryA')
implementation project('libraryB')
}
libraryA and libraryB are applying library plugin in their build.gradle
apply plugin: 'com.android.library'
I am trying to access strings declared in libraryA and libraryB inside of my application via application's R class

Check your dependency first, make sure all of your modules are declared properly. If you implement module properly then you should have access to the modules component including resources.
Sometimes, IDE errors happen. Simply, Invalidate & Restart will solve your problems.

Related

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.

Multiple APKs packaging the same library can cause runtime errors with only one dynamic module on AGP 4.0.1?

I have only one dynamic feature module in my project called search, But when I try to build project, I get that Error:
[:search, :search] all package the same library [androidx.recyclerview:recyclerview].
Multiple APKs packaging the same library can cause runtime errors.
Placing each of the above libraries in its own dynamic feature and adding that
feature as a dependency of modules requiring it will resolve this issue.
Libraries that are always used together can be combined into a single feature
module to be imported by their dependents. If a library is required by all
feature modules it can be added to the base module instead.
Of Course, the first thing I did is to research about people who had the same problem And I found:
1- This Question
2- This medium article
Both Introduce the same solution (Use Android Gradle Plugin 4.0) and my project uses AGP 4.0.1, But the problem is that I have only one dynamic module called search. I don't have any other dynamic modules, even further I don't have the dependency of RecyclerView: androidx.recyclerview:recyclerview in my search gradle file, So this is maybe a transitive dependency.
Also, you can find that duplicated dependency in one of two ways:
1-Navigate to: PROJECT_NAME/module_name(In my case: search)/build/intermediates/
and then search for "deps.txt" file in that directory, Open the file and you will see all your module dependencies direct and transitive ones
2- run ./gradlew :module_name:dependencies task
If you tried to remove that duplicated line: androidx.recyclerview:recyclerview from "deps.txt" file, it gets generated again after each build.
That being said, I need some rule in my packagingOptions {} like exclude to prevent that conflict between search.aar and and any other search.* format
Can anyone help, please?
In my case, I removed the below from one of the modules
Note: not from the base module
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

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.

FirebaseApp initialization unsuccessful in Android Instant apps

I am currently working on an exiting Android application in order to create an Instant apps version.
My Android Studio is now split into several modules :
the business object module (which is a library)
the base -feature- module
the moduleA -feature- module
the app module (which is a phone/tablet module)
the instant module (which is an instant apps module)
My instant app module can be compiled and launched on a phone/tablet but each time it crashes due to Firebase issues. I have the following message into the logcat :
I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
According to the documentation the Firebase library is compatible with Instant Apps, but I am pretty sure that I do not move the google-services.json file into the right place in my project...
Here what I have done :
I defined the following classpath dependencies into the build.gradle file of the Android Studio project : classpath 'com.google.gms:google-services:3.1.1'
I put the google-services.json file into my module base (because the documentation asks for it)
Now, if I try to apply the plugin (apply plugin: 'com.google.gms.google-services') into the build.gradle file of the base module, I cannot compile. I have the following message :
Error:Execution failed for task ':base:processGooglePlayProductionDebugFeatureGoogleServices'.
> No matching client found for package name 'com.mycompany.myapp.base'
In fact, the package name defined into the google-services.json file is the one use by the app (because according to the documentation the base library cannot have the same package name as the installed android app.
I also tried to apply the plugin into the build.gradle files of the installed app and into the instant apps module leaving the google-services.json file into the base module. The app compile but I have the log : "FirebaseApp initialization unsuccessful".
So I tried moving the google-services.json file into my instant app module but I still have the log : "FirebaseApp initialization unsuccessful"
I also tried to force the initialization calling the static method initializeApp from the FirebaseApp class but the log persists.
I cannot find an example of implementation on the web. In fact, the Google Sample repository does not use a google-services.json file.
Thank you in advance for your help !
Edit : Here the dependencies of my modules :
The dependencies of my base -feature- module :
implementation project(':businessobject')
feature project(':moduleA')
application project(':app')
The dependencies of my moduleA -feature- module :
api project(':base')
api project(':businessobject')
The dependencies of my app module (which is a phone/tablet module) :
implementation (project(':base'))
implementation (project(':businessobject'))
implementation (project(':moduleA'))
The dependencies of my instant module (which is an instant apps module) :
implementation project(':base')
implementation project(':businessobject')
implementation project(':moduleA')
because according to the documentation the base library cannot have
the same package name as the installed android app.
I think the documentation is a bit out of date. Each "feature" module needs to use a different package name, because that's what's used to generate the name of the R class. AFAIK there's no reason why you can't have the "base" feature use the same package name as the app itself.
It seems like the "google-services" plugin needs to be updated for the "feature" plugin to use the application ID rather than the name specified in AndroidManifest.xml (which will be overwritten later to be the same as the application ID).
tl;dr---changing the package name of "base" to "com.mycompany.myapp" should get things working.

Gradle Android project dependency not working

I'm trying to migrate from maven to Gradle for an Android project and I am facing the following issue:
I have three projects i.e
root
- projlib
build.gradle
- projA
build.gradle
- projB
build.gradle
build.gradle
settings.gradle
Basically what I want to achieve is that projB depends on projA and projlib. projlib is a lib folder that compiles and generates a lib(jar) file. projA is an Android Application and projB is another Android Application that needs to reference code in projA. Right now what I have added in the projB build.gradle file is
dependencies {
compile project(':projlib')
compile project(':projA')
}
So say if there's a class
FooProjLib in projlib and
FooProjA in projA
Then In projB I can do
FooProjLib foo = new FooProjLib
which works fine
but when I do
FooProjA foo = new FooProjA
Gradle gives me package projA does not exist, what I have observed is that both dependency is resolved but only the lib can be reference and not the apk.
Does anyone have an idea how to solve this?
You can't do exactly what you want. projA can't build an application (i.e. an APK) and also have other things depend on it. projB can only depend on projA if projA is an Android library, meaning in its build file you have this declaration:
apply plugin: 'android-library'
instead of
apply plugin: 'android'
Of course, this means that projA won't build an APK, but will build an AAR instead.
If projA needs to also be an APK, you'll have to restructure things such that the common code that's in projA that projB also needs is moved out to a shared library. If both projects are similar, perhaps you could have just one module for them and use project flavors to differentiate them; it's hard to say whether this is a good approach without a lot more information.

Categories

Resources