0

I want to change theme of app (actually I need to change windowBackground only) in build time, depends on my environment.

styles.xml

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>

    <style name="AppTheme.WithSplashScreen" parent="AppTheme">
        <item name="android:windowBackground">@drawable/window_background</item>
    </style>
</resources>

I tried manifest placeholders and inject theme name into manifest app/build.gradle

defaultConfig {
      manifestPlaceholders = [app_theme:"@style/AppTheme.WithSplashScreen"]
      //...
  }

AndroidManifest.xml

<application
    android:theme="@{app_theme}">

but I got an error

app/android/app/build/intermediates/manifests/full/debug/AndroidManifest.xml:56:24-44: AAPT: No resource type specified (at 'theme' with value '@{app_theme}').

Maybe appt expects id of theme resource form R.class, but I am not sure how to reference to that from gradle file. Or there should be other way.

I can't apply themes in runtime because I want window-background appear immediately .

Matthew Mitchell
  • 514
  • 4
  • 14
punksta
  • 2,738
  • 3
  • 23
  • 41
  • looks like one of alternative solution is creating build flavor for each theme. https://stackoverflow.com/questions/34068870/android-flavors-with-different-base-themes – punksta Jan 18 '19 at 12:10

1 Answers1

0

Probably you already solved it, but I think is valuable to answer this question as I came to the same need in my APP.

According to the documentation here you can inject build variables in your manifest but the syntax is with $ and not @.

In my APP I used:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.app">
<application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="${appTheme}">

And on build.gradle

flavorDimensions "version"
productFlavors {
    flavor1{
        dimension "version"
        ...
        manifestPlaceholders = [appTheme: "@style/AppTheme"]
    }
    flavor2 {
        dimension "version"
        ...
        manifestPlaceholders = [appTheme: "@style/OtherAppTheme"]
    }
}
Tito Rezende
  • 346
  • 3
  • 6