Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project isolated replacement for findProperty #28857

Open
mathjeff opened this issue Apr 17, 2024 · 5 comments
Open

Project isolated replacement for findProperty #28857

mathjeff opened this issue Apr 17, 2024 · 5 comments
Labels

Comments

@mathjeff
Copy link
Contributor

mathjeff commented Apr 17, 2024

Expected Behavior

It would be nice if there could be a convenient replacement for Project.findProperty(String) that is compatible with project isolation by not checking parent projects and still returns null if the property is not found.

Maybe this could be one method that checks all of the same places as findProperty() (project properties, extensions, conventions, extra properties, tasks) or maybe it could be a separate method for each individual place like ExtraPropertiesExtension.getOrNull()

If that already exists and I just wasn't able to find it, it could be nice to link to it from the documentation of findProperty()

Current Behavior (optional)

No response

Context

AndroidX is addressing more project isolation violations.
It feels a little bit verbose to replace things that look like project.findProperty(STUDIO_TYPE) with things that look like if (project.hasProperty(STUDIO_TYPE)) project.property(STUDIO_TYPE) else null

I tried project.providers.gradleProperty(STUDIO_TYPE).getOrNull() but that returns null because the property was set via project.extensions.extraProperties and I don't seem to be able to do project.extensions.extraProperties.getOrNull(String) either.

Thanks!

@ov7a
Copy link
Member

ov7a commented Apr 18, 2024

This feature request is in the backlog of the relevant team and is prioritized by them.

@mathjeff
Copy link
Contributor Author

mathjeff commented Apr 18, 2024

Also related is #23572 where it can be a little bit tricky to determine an appropriate replacement for findProperty since providers.gradleProperty() (which was what I expected to be the best replacement) doesn't check all of the places I'd expected

@mlopatkin
Copy link
Member

This is going to be handled as part of #25179.

@mathjeff
Copy link
Contributor Author

Cool thanks!

@mathjeff
Copy link
Contributor Author

Also potentially relevant, what we ended up going with for now was making our own function extraPropertyOrNull:

fun Project.extraPropertyOrNull(key: String): Any? {
    val container = project.extensions.extraProperties
    var result: Any? = null
    if (container.has(key))
        result = container.get(key)
    return result
}

https://r.android.com/c/platform/frameworks/support/+/3047373/11/buildSrc/public/src/main/kotlin/androidx/build/gradle/Extensions.kt#31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants