Unresolved reference when i open experimental = true with flavorDimensions

855 Views Asked by At

I want to build to version app with flavorDimensions in my app gradle look like this

 androidExtensions {
    experimental = true
}

flavorDimensions "version", "a"

productFlavors {

    yooooo {
        dimension "version"
    }

    hei {
        dimension "version"
    }

    six {
        dimension "a"
    }

    fsdf {
        dimension "a"
    }

}

when I run my app I get this error

e: /Users/wanbo/Dev/Android/workspace/me/test/app/src/yooooo/java/com/werb/test/MainActivity.kt: (12, 9): Unresolved reference: tttt

cannot find my view in activity with android extensions

MyActivity code

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.yooooo.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    tttt.text = "2222"
}}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

 <TextView
    android:id="@+id/tttt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

1

There are 1 best solutions below

0
On

Flavor code is only acknowledged when you are using that "Build Variant" from the left menu, otherwise it will be red.

You have 2 options.

1) Use Flavor Check to see which flavor you are in (inside an If Check) to avoid touching bad code that shouldn't run in other flavors.

or

2) Create your respective flavor folders for the files that are only used in that flavor. If any other file references tttt then it also becomes a flavor specific implementation. You will need to make an instance of the MainActivity for all flavors, but only reference tttt in the myFlavor/MainActivity that lives under the correct flavor.

Hope that helps, remember the build variant is in lower left menu, you can switch your flavor variant for testing.