Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase support library dependency conflict for instant apps

I'm trying to implement instant apps into a project that uses Firebase database. I'm targeting SDK version 27, so the support libraries are on version 27.0.2.

Firebase database version is 11.8.0 and gms version is 3.1.0. When I try to sync, I get the following error:

Android dependency 'com.android.support:support-v4' has different 
version for the compile (25.2.0) and runtime (27.0.2) classpath. You 
should manually set the same version via DependencyResolution

I was able to get around the issue by adding the following dependencies explicitly before the instant apps

implementation 'com.android.support:support-v4:27.0.2'
implementation 'com.android.support:support-media-compat:27.0.2'

But with the instant apps, even if I have them in the feature module (app-base), when I try to build the actual app (com.android.application), I again get the same error.

I can again move around the issue by moving those conflicting dependencies into application module gradle file, in which case the sync succeeds, but then I'm facing another problem, this time with manifest merging, which prevents the app from finding the launcher activity:

Attribute provider#com.google.firebase.provider.FirebaseInitProvider@authorities value=(com.iamkaan.packagename.firebaseinitprovider) from AndroidManifest.xml:10:13-72 is also present at AndroidManifest.xml:33:350-423 value=(com.iamkaan.packagename.base.firebaseinitprovider). Suggestion: add 'tools:replace="android:authorities"' to element at AndroidManifest.xml:8:9-12:39 to override. app main manifest (this file), line 9

This last issue is somehow related to firebase-core dependency because when I changed my app gradle dependencies from

implementation project(':app-base')

to

implementation (project(':app-base')) {
    exclude group: 'com.google.firebase', module:'firebase-core'
}

I was able to run the app. But this time, I started getting the following error on runtime (the first time I call FirebaseDatabase.getInstance())

Default FirebaseApp is not initialized in this process com.iamkaan.packagename. Make sure to call FirebaseApp.initializeApp(Context) first

It was indeed not called but was working without anyway until instant app implementation. Anyway, I added the call to various places before the first FirebaseDatabase call, nothing helped.

Package names

  • app manifest: com.iamkaan.packagename
  • app gradle applicationId: com.iamkaan.packagename
  • app-base manifest: com.iamkaan.packagename.base
  • app-base gradle file doesn't have an applicationId
like image 987
iamkaan Avatar asked Jan 17 '18 02:01

iamkaan


1 Answers

I ran into something similar and it is caused by support libraries being included by dependencies. It's important to note that almost all of Google/Android support libraries (CardView, RecyclerView etc) includes the latest v4 and v7 support libraries. So that usually causes conflicts.

What you need to do is:

  1. Don't exclude anything while adding Base Module in main application, i.e. keep using implementation project(':app-base') only
  2. Use api instead of implementation for support libs included inside Base Module's build.gradle i.e. api 'com.android.support:support-v4:27.0.2'
  3. Make sure whichever library you've added in Base Module must NOT be added again in main app's build.gradle file
  4. MOST IMPORTANT: For both main app and base module's build.gradle file, exclude support libs FOR EACH item (see example below)

 

api('com.android.support:support-media-compat:27.0.2') {
    exclude group: 'com.android.support'
}
api('com.android.support:support-v7:27.0.2') {
    exclude group: 'com.android.support'
}

I will also recommend not using com.android.support:support-v7:27.0.2 instead use only the specific items from support libs that you need. See Support Library Packages on how can you add only specific items from support libs.

like image 102
adnanyousafch Avatar answered Oct 05 '22 16:10

adnanyousafch