maxkomarychev / react-native-ultimate-config

Config that works
MIT License
261 stars 31 forks source link

Geting variables in android/build.gradle #69

Closed tr3v3r closed 3 years ago

tr3v3r commented 3 years ago

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!

maxkomarychev commented 3 years ago

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?

maxkomarychev commented 3 years ago

@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"
tr3v3r commented 3 years ago

@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?

maxkomarychev commented 3 years ago

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)...

tr3v3r commented 3 years ago

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 )

maxkomarychev commented 3 years ago

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

tr3v3r commented 3 years ago

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