Coder Social home page Coder Social logo

Comments (8)

yukai18 avatar yukai18 commented on May 29, 2024 2

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. 😅
Screen Shot 2021-12-17 at 12 13 25

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.

yukai18 avatar yukai18 commented on May 29, 2024

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

yshrsmz avatar yshrsmz commented on May 29, 2024

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

yukai18 avatar yukai18 commented on May 29, 2024

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

yshrsmz avatar yshrsmz commented on May 29, 2024

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

raymondctc avatar raymondctc commented on May 29, 2024

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.

yshrsmz avatar yshrsmz commented on May 29, 2024

@raymondctc

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.

raymondctc avatar raymondctc commented on May 29, 2024

@raymondctc

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)

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

  1. Create a gradle.properties in ProjectB, add key buildkonfig.flavor=
  2. In ProjectA's build.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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.