maxkomarychev / react-native-ultimate-config

Config that works

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Geting variables in android/build.gradle

tr3v3r opened this issue · comments

Hello!
First of all, thanks for your work!

The question:
Is it possible to get value in android/build.gradle (not from android/app dir)? For now, I'm getting an error during build

> Could not get unknown property 'config' for root project '***' of type org.gradle.api.Project.

Thanks!

hi @tr3v3r the last time I checked this was possible using the following API: https://github.com/maxkomarychev/react-native-ultimate-config/blob/master/docs/api.md#buildgradle

it's quite hard to debug your issue not seeing any code - can you please create project with minimal reproducible example of the problem?

@tr3v3r I am assuming things but you might be seeing this problem because you're not applying gradle plugin as described in quickstart guide:

apply from: "../../node_modules/react-native-ultimate-config/android/rnuc.gradle"

@maxkomarychev I’ve got error not in android/app/build.gradle ( when according to docs I need to insert ‘apply from’ statement ) but in android/build.gradle. I suppose the config could not be reached from this place with current implementation. What do you think?

oh I see, sorry for missing that part.

unfortunately I haven't even tried to apply the plugin there and frankly I'm not that well versed in gradle nuances.

do you mind sharing why is this needed? can this be moved into internal app/build.gradle? buildscripts are typically free from application-specific logic.. (at least on projects I've worked on)...

I needed this because of this tutorial from the react-native-mapbox library.
In particular I need paste this code in android/build.gradle:

 maven {
            url 'https://api.mapbox.com/downloads/v2/releases/maven'
            authentication {
                basic(BasicAuthentication)
            }
            credentials {
                // Do not change the username below.
                // This should always be `mapbox` (not your username). 
                username = 'mapbox'
                // Use the secret token you stored in gradle.properties as the password
                password = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: ""
            }
        }

But also it's mandatory for us NOT to store this token directly in the project directory - so we decided to link it during build on CI. Obviously, the first thing that came to my mind is storing MAPBOX_DOWNLOADS_TOKEN in .env file (which is already linking on CI during the build ).

But it doesn't work:

 password = project.config.get['MAPBOX_DOWNLOADS_TOKEN'] ?: "" 

Actually, we came up with another solution:

password = System.getenv("MAPBOX_DOWNLOADS_TOKEN") as String

and just export this env variable on CI during the build. So we can close the issue if you want )

Then I can suggest you to use hooks API and generate '.properties' file which gradle can naturally pick up

Yeap good idea thanks. I even didn't know that your lib provides such a possibility!

Actually, if we talk about CI, getting value from the environment is preferable, since it provides this variable automatically from the Secrets store ( Bitrise ).

But locally (during development) I didn't want to do the same way )

Thanks for your time! I suppose the question is resolved