androidandroid-flavors

Android Flavors :Applying only flavor specific code to a source set


I am running into some trouble with source sets, I have a main source set that contains most of the common code but then a build flavor switch was included by the previous developer for example :

    if (Application.inKiosk) {
        navigation.visibility = View.INVISIBLE
        val employeeSelectionFragment = EmployeeSelectionFragment()
        employeeSelectionFragment.setAllList(employees)
        employeeSelectionFragment.setCallback(employeeSelected)
        supportFragmentManager.beginTransaction().add(R.id.small_container, employeeSelectionFragment, "EmployeeSelectionFragment").commitNow()
        currentTimeRecord = TimeRecord()
    } else {
        setupTimeRecords(employees!!.first()!!.id, savedInstanceState == null)
    }

I want to extract this flavor specific block and include it in its own source set folder. I can't get access to the common members though such as navigation. How would I extract this section ?

        navigation.visibility = View.INVISIBLE
        val employeeSelectionFragment = EmployeeSelectionFragment()
        employeeSelectionFragment.setAllList(employees)
        employeeSelectionFragment.setCallback(employeeSelected)
        supportFragmentManager.beginTransaction().add(R.id.small_container, employeeSelectionFragment, "EmployeeSelectionFragment").commitNow()
        currentTimeRecord = TimeRecord()

Solution

  • The basic recipe for splitting code between product flavors is to:

    Gradle will pull in the implementation of the class from the flavor's source set for whatever build variant you are building, and that is the implementation that the main code will use in that build.