Search code examples
androidandroid-jetpack-composeandroid-theme

How to use Open Source Notices library with Jetpack Compose?


I'm using the official Google library for open source notices: https://developers.google.com/android/guides/opensource

But when I try to start the activity

Button(
    onClick = {
        context.startActivity(Intent(context, OssLicensesMenuActivity::class.java))
    }
) {
    Text(text = "Open Source Licenses")
}

I get error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.gms.oss.licenses.OssLicensesMenuActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Is there some way to use the app's default Compose theme to launch an activity?

I know that I can specify the AppCompat theme in the manifest and the crash will stop but I'd like to somehow use my Compose theme. Is it even possible?


Solution

  • Your app uses a nice new theme rather than the old AppCompat ones this library expects. You can set a different theme just for the Activities in this library though. eg:

        <!-- Google OSS Licenses library is a bit old, doesn't work with our theme. Set a compatible theme for its Activities -->
        <activity android:name="com.google.android.gms.oss.licenses.OssLicensesMenuActivity"
                android:theme="@style/Theme.AppCompat.Light.DarkActionBar" />
        <activity android:name="com.google.android.gms.oss.licenses.OssLicensesActivity"
                android:theme="@style/Theme.AppCompat.Light.DarkActionBar" />
    

    Also, heads up for the next issues you'll likely encounter:

    1. This library only generates a list for release builds, so you can only verify it worked on those (a pain for QA if they use debug builds).

    2. The list it generates seems to have lots of duplication and yet also things missing.

    Personally each time I try to use this library I find it to be a bit lacking for any real practical use and end up going to something else. It seems to be a bit too optimistic that all libraries will do the right thing, and not well maintained or supported, ie. no docs to make it work with Compose, or with modern gradle plugins DSL.

    I used to use the cookpad library for this but they have said it's now deprecated in favour of this, even though IMO theirs was much more thorough, flexible + practical, ie. generate list with all libraries, let you fill in what's missing, then generate final output from that. More work, but you were sure you'd covered all your libraries. There's also this library, haven't tried it yet but I think that's next on my list to look at.