FirebaseApp initialization unsuccessful in Android Instant apps - android

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.

Related

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

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.

Use buildSrc module versions in app module

I'm following this tutorial on how to manage Gradle dependencies with Kotlin in my Android app and now I would like to use the versions defined in my Versions object in my app module (the scenario is that I have an open source screen and I want to show the library versions). How can I do this?
I have tried adding the buildSrc directory to settings.gradle:
include ':app', ':buildSrc'
and then in my build.gradle file, adding it to the dependencies block:
dependencies {
implementation project(':buildSrc')
}
However, when I try to compile and run, the following error is thrown:
Don't know how to compute maven coordinate for artifact 'Gradle API' with component identifier of type 'class org.gradle.internal.component.local.model.OpaqueComponentIdentifier'.
and there is no much information in the web about this error.
Try to remove the buildSrc module from your settings & build.gradle. Its automatically build and accessible for all other modules in this project.

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.

Android dependency hierarchy

In the app module I added baseLayerCore module as a dependency and both app and baseLayerCore modules need core module as their dependency so I defined core in app but baseLayerCore dose not resolve it and needs to be defined in its own build.gradle too.
I believe baseLayerCore does not need core since it has defined in app.
Am I wrong?! Why is this happening?
app build.gradle:
According to this link, I finally recognized that this error occurs on compile time not run time so I should use compileOnly dependancy for baseLayerCore and implementation for app module.These way I can compile the project to see the result but there is no need to have the dependency on run time.

Dependency issues in reference project

I have an issue that dependencies of a project that is referenced as a dependency module in my android app seem not to be included into the .apk file of my android application.
Project Setup
Android App (Android studio & gradle)
Java desktop application (IntelliJ/maven/gradle project)
Java model (classes & features used by both android & desktop app)
The "Java model" is added as a dependency to both Android App and Java Desktop application.
When I run the desktop application, the dependencies of the JavaModel are resolved via maven, incuded into the application and everything runs smoothly.
From an Android Studio point of view, I have imported the JavaModel as a module into the android project and gradle is used to resolve the dependencies. I have set up the following gradle files:
Android App "settings.gradle"
include ':app'
include ':JavaModel'
project(':JavaModel').projectDir=new File('../../JavaModel')
Android App "build.gradle"
dependencies {
compile project(':JavaModel')
// and more...
}
Java Model "build.gradle"
dependencies {
compile(
'org.apache.httpcomponents:httpclient:4.4.1'
// and more ...
)
}
Everything compiles just fine without any errors and a .apk can be created and runs on my test device. However, as soon as I access features within the app that are provided by the "Java model" (in this example, I am using the HttpClient class from the org.apache.httpcomponents:httpclient:4.4.1 dependency), I get the following exception:
java.lang.ClassNotFoundException: Didn't find class
"org.apache.http.impl.client.HttpClients"
Plese note that this is just an example case and the issue also occurs with all other dependencies that are only referenced in the "JavaModel", but not in the Android app itself.
It seems to me like the dependencies of the "JavaModel" work just fine at compile time, since everything executes just fine, but are then not included into the .apk file and therefore cause this exeption.
The question is how can I (correctly) make sure that even dependencies of a dependency project are included into the .apk file?
Apache http client conflicts with android one, if you want to use recent one, you need to use android port https://hc.apache.org/httpcomponents-client-4.3.x/android-port.html
Regarding "JavaModel". If dependencies of JavaModel are compile dependencies it all must work fine (assuming dependency does not have fancy code like classloaders)

Categories

Resources