Replies: 26 comments 7 replies
-
@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? |
Beta Was this translation helpful? Give feedback.
-
@yukai18 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 |
Beta Was this translation helpful? Give feedback.
-
@yshrsmz thank you for your fast response, I tried converting it to Kotlin, the sync was successful, i run
in my
I had to set below code in my
my |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
hi @yshrsmz, it seems that I cannot get your script working. I have a root project ( However, no matter what I set in |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if 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) |
Beta Was this translation helpful? Give feedback.
-
Thanks! I finally pulled the code and figured out the reason.
By printing
And when I try to print
And I learnt that projectDir.rootProjectDir.setProperty(key, flavor) It doesn't work. Solution
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 ""
}
} |
Beta Was this translation helpful? Give feedback.
-
Hello! First of all I want to say you thank you for such a great library! |
Beta Was this translation helpful? Give feedback.
-
Hi, Flavor(or build variant) in Android is a fairly complex feature. https://developer.android.com/build/build-variants You can define multiple flavor dimensions, and I'm not sure how we can distinguish which flavor relates to BuildKonfig's flavor. Here's a groovy script from my previous project, which extracts Android flavor from the task name and maps it to BuildKonfig's flavor. My project had a single flavor dimension def configureBuildKonfigFlavorFromAndroidTasks() {
if (project.gradle.startParameter.projectProperties.containsKey("buildkonfig.flavor")) {
// prefer cli parameter
println("buildkonfig.flavor=${project.gradle.startParameter.projectProperties["buildkonfig.flavor"]}")
return
}
def pattern = "^:androidApp:(assemble|test|bundle|extractApksFor)(\\w+)(Release|Debug)(|UnitTest)\$"
def runningTasks = project.gradle.startParameter.taskNames
def matchingTask = runningTasks.find { it.matches(pattern) }
if (matchingTask == null) {
return
}
Pattern p = Pattern.compile(pattern)
Matcher m = p.matcher(matchingTask)
if (!m.find()) {
return
}
def flavor = m.group(2)
def buildkonfigFlavor = ""
// map matching android flavor to BuildKonfig's flavor
switch (flavor) {
case "Dev":
buildkonfigFlavor = "dev"
break
case "Stg":
buildkonfigFlavor = "stg"
break
case "Prd":
def buildType = m.group(3)
if (buildType == "Debug") {
buildkonfigFlavor = "prdDebug"
} else {
buildkonfigFlavor = "prd"
}
break
}
println("presentation-base:buildkonfig.flavor=${buildkonfigFlavor}")
project.setProperty("buildkonfig.flavor", buildkonfigFlavor)
} |
Beta Was this translation helpful? Give feedback.
-
@yshrsmz Above solution only works best for android targets but for iOS target it is not able to find flavour. Here is code.
Which does not contain any Release or Debug buildTypes, eventually it is not able to find any flavour. Do i need to define any flavours in xcode? |
Beta Was this translation helpful? Give feedback.
-
@farhazulmullick-pw you can configure BuildKonfig flavor by passing ./gradlew build -Pbuildkonfig.flavor=release |
Beta Was this translation helpful? Give feedback.
-
So @yshrsmz we cannot dynamically get productFlavours for iOS targets from AndoridTarget? |
Beta Was this translation helpful? Give feedback.
-
@farhazulmullick-pw Yes because Android targets have nothing to do with iOS targets |
Beta Was this translation helpful? Give feedback.
-
@yshrsmz Ok. Because earlier i was doing someting like this and it was working flawless on andorid.
|
Beta Was this translation helpful? Give feedback.
-
From the point of view of the BuildKonfig author, if you need to distinguish between prodDebug and prodRelease, then yes, you need to define these flavors. But if you really want to go that way(which is not the BuildKonfig-standard), you should be able to simply pass some arbitrary property and do whatever you want. // this code is executed from Xcode
./gradlew :shared:embedAndSignAppleFrameworkForXcode -Pyourcustomflavor=prdDebug fun getCurrentBuildConfigs(): BuildConfigs {
val taskRequestsStr = gradle.startParameter.taskRequests.toString()
// check if yourcustomflavor exists
val iOSflavor = project.findProperty("yourcustomflavor")
if (iOSflavor != null) {
// calculate config for IOS
}
.....
// calulate and fill BuildConfig
return buildConfigs
} |
Beta Was this translation helpful? Give feedback.
-
Thanks @yshrsmz Its is a great help. |
Beta Was this translation helpful? Give feedback.
-
Just update your existing Xcode configuration(should be in BuildPhase though)
That's depending on your preference. If you want default value, then define it. |
Beta Was this translation helpful? Give feedback.
-
Thank You @yshrsmz for your help :) |
Beta Was this translation helpful? Give feedback.
-
@yshrsmz We have added these scripts to generate a build for iOS unfortunately its not working, can you help us to know what's wrong in these scripts
the last one we use for Crashlytics
|
Beta Was this translation helpful? Give feedback.
-
@sureshmaidaragi1919 Can you provide a relevant part of your build.gradle script? |
Beta Was this translation helpful? Give feedback.
-
here it is inside
|
Beta Was this translation helpful? Give feedback.
-
Again, that is a pure Gradle problem and I can't exactly help with it(and I don't recommend that configuration). But can you check what happens if you use or |
Beta Was this translation helpful? Give feedback.
-
Tried both still didnt help
|
Beta Was this translation helpful? Give feedback.
-
That's weird 🤔 I updated https://github.com/yshrsmz/BuildKonfig/blob/issue-23/sample-kts/build.gradle.kts#L19
and it successfully found So it should be something to do with your gradle configuration or xcode-specific tasks such as |
Beta Was this translation helpful? Give feedback.
-
I'm confused as to why this logic on the build script wouldn't work: val FLAVOR_PROPERTY = "buildkonfig.flavor"
tasks.whenTaskAdded {
if(name == "generateBuildKonfig"){
dependsOn(project.tasks.register("setFlavorToRelease") {
group = "buildkonfig"
doLast{
project.ext.set(FLAVOR_PROPERTY, "release")
println("-----> ${project.findProperty(FLAVOR_PROPERTY)}")
}
})
}
} When
However the generated BuildKonfig file is the default variant ( Would it be possible that this is an issue with the way the BuildKonfig library reads properties? |
Beta Was this translation helpful? Give feedback.
-
edit: I'm converting this issue to a generic discussion, as it becomes out of control as a feature-tracking issue.
personally I wrote this script and use it in my project. But it would be nice if this is baked into BuildKonfig
Beta Was this translation helpful? Give feedback.
All reactions