Comments (8)
Hi @yshrsmz , as I was creating the mini repro project, I realized my mistakes. I'm able to fully integrate the plugin now. Thank you so much! Sorry for the trouble.
Here's what I did, in case others are experiencing the same issue as mine
For the generated file not showing, make sure that you did not exclude the build
folder in the project navigator, I forgot if this is excluded by default or I somehow turned it off before. 😅
For the local.properties
file make sure there are no double quotes on the string, I wasn't able to make the flavor work because of this
buildkonfig.flavor=debug
from buildkonfig.
@yshrsmz hi, thank you for the great tool. I'm quite new to Kotlin in general, where do you put this script so it will get the flavor from local.properties instead of gradle.properties?
from buildkonfig.
@yukai18
Write it in a build.gradle
which you apply BuildKonfig plugin, and call it.
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'com.android.library'
apply plugin: 'com.codingfeline.buildkonfig' // <- BuildKonfig plugin
// call it
configureBuildKonfigFlavorFromLocalProperties()
kotlin {
// kotlin configuration
}
buildkonfig {
// BuildKonfig configuration
}
def configureBuildKonfigFlavorFromLocalProperties() {
// THE method
}
Please note this is written in groovy, so if you are using KTS then you need to re-write this in Kotlin
from buildkonfig.
@yshrsmz thank you for your fast response, I tried converting it to Kotlin, the sync was successful, i run ./gradlew :shared:generateBuildKonfig
in the terminal, it was successful as well but no file was generated.
Can you kindly help check if I am doing something wrong?
Here is my code in build.gradle.kts
located inside shared module:
import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING
plugins {
id("com.codingfeline.buildkonfig") version "0.11.0" // BuilKonfig plugin
}
configureBuildKonfigFlavorFromLocalProperties()
kotlin {
// kotlin config
}
buildkonfig {
packageName = "com.example.packageName"
defaultConfigs {
buildConfigField(STRING, "flavor", "prod")
}
defaultConfigs("debug") {
buildConfigField(STRING, "flavor", "debug")
}
}
fun configureBuildKonfigFlavorFromLocalProperties() {
val key = "buildkonfig.flavor"
if (project.gradle.startParameter.projectProperties.containsKey(key)) {
// prefer cli parameter
return
}
// load buildkonfig.flavor if exists
val localProperties = gradleLocalProperties(rootDir)
if (localProperties.containsKey(key)) {
project.setProperty(key, localProperties.getProperty(key))
}
}
in my local.properties
file
buildkonfig.flavor="debug"
I had to set below code in my gradle.properties
that is located at the project's root directory or else it will generate could not set unkown type 'buildkonfig.flavor'
error when running project.setProperty(key, localProperties.getProperty(key))
#BuildKonfig
buildkonfig.flavor=
my local.properties
and gradle.properties
files are at the project root directory, while the build.gradle.kts
is inside shared module, not sure if this will matter but I did confirm that localProperties.containsKey(key)
was able to get the key from local.properties
from buildkonfig.
@yukai18 Can you provide a minimal repro?
buildkonfig setup looks fine, but I'd like to know how you configure the entire project(or at least shared
module)
from buildkonfig.
hi @yshrsmz, it seems that I cannot get your script working. I have a root project (ProjectA
) which include another project called ProjectB
and is a Kotlin multiplatform library project and BuildKonfig
was included in ProjectB
.
However, no matter what I set in ProjectA
's local.properties
, it just simply won't recognize and BuildKonfig
will just use the default flavor. I can only change the flavor in ProjectA
's gradle.properties
. Do you have any insight on what should I look for? Thanks! :)
from buildkonfig.
I'm not sure if ProjectB
is a module or a project, but I think you can use rootProject
instead
if (Files.exists(Paths.get("$rootProject.rootDir/local.properties"))) {
or simply add ..
if (Files.exists(Paths.get("$project.rootDir/../local.properties"))) {
(I didn't test these snippets, but you can get the idea)
from buildkonfig.
I'm not sure if
ProjectB
is a module or a project, but I think you can userootProject
insteadif (Files.exists(Paths.get("$rootProject.rootDir/local.properties"))) {
or simply add
..
if (Files.exists(Paths.get("$project.rootDir/../local.properties"))) {
(I didn't test these snippets, but you can get the idea)
Thanks! I finally pulled the code and figured out the reason.
My project structure is a bit complicated and looks like this
MyProject/
├── BuildScriptProject
│ ├── gradle.properties
│ └── gradlew
├── KMMProjects
│ └── ProjectB
└── ProjectA
└── build.gradle
By printing project.projectDir
and project.rootProject.projectDir
on ProjectA
's build.gradle
, the following result is given:
projectDir=/Users/me/MyProject/ProjectA
rootProjectDir=/Users/me/MyProject/BuildScriptProject
And when I try to print project.projectDir
and project.rootProject.projectDir
in BuildKonfigPlugin
's configure
call, the following result is given
projectDir=/Users/me/MyProject/ProjectB
rootProjectDir=/Users/me/MyProject/BuildScriptProject
And I learnt that findProperty
will by default check for ProjectB
's gradle.properties
, if it doesn't exist, it will check for the root project (BuildScriptProject
). I tried to do the following in ProjectA
:
projectDir.rootProjectDir.setProperty(key, flavor)
It doesn't work. findProperty
won't search anything to the root project that's set progrmmatically, it only reads value from gradle.properties
of the root project.
Solution
- Create a
gradle.properties
inProjectB
, add keybuildkonfig.flavor=
- In
ProjectA
'sbuild.gradle
project(':ProjectB').setProperty(key, flavor)
Now it works!
I made a modification to your script too with automatic flavor detection. In case some one is interested:
def configureBuildKonfigFlavorFromLocalProperties() {
def key = "buildkonfig.flavor"
if (project.gradle.startParameter.projectProperties.containsKey(key)) {
// prefer cli parameter
return
}
def targetProject = project(":ProjectB")
def currFlavor = getCurrentFlavor()
targetProject.setProperty(key, currFlavor)
}
def getCurrentFlavor() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
Pattern pattern
if (tskReqStr.contains("assemble") || tskReqStr.contains("install")) {
pattern = Pattern.compile("(assemble|install)(\\w+)(Release|Debug|Jokes)")
} else {
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")
}
Matcher matcher = pattern.matcher( tskReqStr )
if( matcher.find() ) {
return matcher.group(3).toLowerCase()
} else {
println "NO MATCH FOUND"
return ""
}
}
from buildkonfig.
Related Issues (20)
- Kotlin/JS Exception: non-flavored defaultConfigs must be provided. Despite is provided. HOT 5
- Support config inheritance HOT 9
- Update Gradle to 7.3+ and use Kotlin 1.5.31
- Support for Gradle configuration cache HOT 1
- Support for adding platform specific config HOT 2
- Generating Non-Internal BuildKonfig object HOT 2
- Generating Secondary and more BuildKonfig object HOT 9
- can support kotlin Native ? HOT 5
- BuildType & ProductFlavor & sourceSets support ? HOT 1
- Plugin generates invalid class so build fails HOT 5
- Readme does not document hierarchical project support HOT 2
- Type ...BuildConfig is defined multiple times HOT 2
- BuildKonfig doesn't work with npm-publish HOT 4
- Set generated value visibility modifier public/internal HOT 2
- non-flavored defaultConfigs must be provided. HOT 3
- Unresolved reference HOT 2
- Rename `buildkonfig` to `buildKonfig` HOT 1
- Support for Kotlin 1.9.0? HOT 4
- Dependency Dashboard
- SourceSet change support HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from buildkonfig.