16

In an android project which consists of a library project, I need to have a different theme set in manifest for main app and library. Like I need to set below theme in the main project

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorPrimary">@color/grey</item>
  <item name="colorPrimaryDark">@color/black</item>
  <item name="colorAccent">@color/green</item>
</style>

and the one below for library project

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorPrimary">@color/blue</item>
  <item name="colorPrimaryDark">@color/red</item>
  <item name="colorAccent">@color/white</item>
</style>

Library project styles are overridden by main app styles. when I give different names for styles it throws manifest merger conflict error.

Below is the manifest of main app

<application
  android:name=".MyApp"
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">

and this one is for library

<application
  android:allowBackup="true"
  android:label="@string/app_name"
  android:supportsRtl="true"
  android:theme="@style/AppTheme"
  >
arjun
  • 3,514
  • 4
  • 27
  • 48

2 Answers2

21

After building your app, gradle will merge your manifest files into one, so setting theme to application tag in library will be overridden by app's attributes.

Change your style name to a different name,

<style name="MyLibTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorPrimary">@color/blue</item>
  <item name="colorPrimaryDark">@color/red</item>
  <item name="colorAccent">@color/white</item>
</style>

If you are starting some activity in your library functions, then set the theme to activities individually in library manifest file.Theme set on application tag on your library manifest will be replaced on merge after project build, so set theme for individual activity

   <activity android:name=".MyLibActivity" android:theme="@style/MyLibTheme">
    </activity>

or you can use setTheme in your activity code.

setTheme(R.style.MyLibTheme);

On build process if the user (library user) is using your same theme name it will be replaced with main app module theme, to protect that situation you can use gradle option in your library's gradle file.

android{
  // replace mylib_ with name related to your library
  resourcePrefix 'mylib_'
}
Renjith Thankachan
  • 4,178
  • 1
  • 30
  • 47
  • I tried this, but it is not working in when I set this to activity in library project – arjun Mar 16 '17 at 06:28
  • you will need to set theme for the specific activity, not in your `application` theme you set in your library's android manifest, it will not work as i said earlier – Renjith Thankachan Mar 16 '17 at 07:36
  • 1
    setting the theme for individual activity worked but is there a way to set for the whole project in a library. – arjun Mar 17 '17 at 09:39
  • You can make use of fragments, Use a single activity with your theme, then use fragments or nested fragments for specific views – Renjith Thankachan Mar 17 '17 at 14:22
  • @IAmBatman, I have the same problem. In my case, the application is crashing. I got "Failed to resolve attribute at index". One more thing is, If I do not use any custom attributes, theme gets applied to the activity and app is not crashing. Can anyone help me resolve this? – Uma Sankar Aug 27 '18 at 12:51
1

I think you might want to use tools:replace="android:theme" under application node in the manifest of the main project. You can find the details and more here: https://developer.android.com/studio/build/manifest-merge

salbury
  • 91
  • 1
  • 7