当我打开flavorDimensions打开实验= true时,无法解析的参考

时间:2018-09-25 13:19:05

标签: android kotlin android-productflavors kotlin-android-extensions android-build-flavors

我要在应用程序gradle中使用flavorDimensions来构建版本应用程序

 androidExtensions {
    experimental = true
}

flavorDimensions "version", "a"

productFlavors {

    yooooo {
        dimension "version"
    }

    hei {
        dimension "version"
    }

    six {
        dimension "a"
    }

    fsdf {
        dimension "a"
    }

}

运行应用程序时出现此错误

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

在具有Android扩展的活动中找不到我的视图

MyActivity代码

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 个答案:

答案 0 :(得分:0)

仅当您使用左侧菜单中的“ Build Variant”时,才会确认风味代码,否则它将为红色。

您有2个选择。

1)使用“风味检查”来查看您所处的风味(在“ If Check”中),以避免碰到不应以其他风味运行的错误代码。

2)为仅用于该风味的文件创建各自的风味文件夹。如果还有其他文件引用tttt,则它也将成为特定于风味的实现。您将需要为所有口味创建MainActivity的实例,但仅在myFlavor / MainActivity中引用正确的口味下的tttt。

希望如此,请记住构建变体在左下方菜单中,您可以切换风味变体进行测试。