找不到'kotlinPlugin'和'darkThemeColors'

时间:2019-10-25 03:48:08

标签: android android-jetpack android-jetpack-compose

当我尝试使用jetpack的compose库时,我被困在执行jetpack文档中提到的步骤(源:https://developer.android.com/jetpack/compose/setup#compose-compiler)。我遇到以下异常,无法使用kotlinPlugin。

Caused by: org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method kotlinPlugin() for arguments [androidx.compose:compose-compiler:0.1.0-dev02] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

此外,我无法使用 darkThemeColors lightThemeColors 。以下是我添加的Gradle依赖项。

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    kotlinPlugin "androidx.compose:compose-compiler:0.1.0-dev02"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.ui:ui-framework:0.1.0-dev02'
    implementation 'androidx.ui:ui-layout:0.1.0-dev02'
    implementation 'androidx.ui:ui-material:0.1.0-dev02'
    implementation 'androidx.ui:ui-tooling:0.1.0-dev02'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'org.jetbrains.kotlin:kotlin-reflect:1.3.50'
}

我缺少任何依赖吗?我检查了 MaterialTheme MaterialColors 类,但是没有找到darkThemeColors和lightThemeColors。

2 个答案:

答案 0 :(得分:2)

jetpackCompose示例应用jetnews中,darkThemeColorslightThemeColors在下面的Themes.kt文件示例中定义,因此您需要定义颜色。

val lightThemeColors = MaterialColors(
    primary = Color(0xFFDD0D3C),
    primaryVariant = Color(0xFFC20029),
    onPrimary = Color.White,
    secondary = Color.White,
    onSecondary = Color.Black,
    background = Color.White,
    onBackground = Color.Black,
    surface = Color.White,
    onSurface = Color.Black,
    error = Color(0xFFD00036),
    onError = Color.White
)

/**
 * Note: Dark Theme support is not yet available, it will come in 2020. This is just an example of
 * using dark colors.
 */
val darkThemeColors = MaterialColors(
    primary = Color(0xFFEA6D7E),
    primaryVariant = Color(0xFFDD0D3E),
    onPrimary = Color.Black,
    secondary = Color(0xFF121212),
    onSecondary = Color.White,
    surface = Color(0xFF121212),
    background = Color(0xFF121212),
    onBackground = Color.White,
    onSurface = Color.White
) 

在以下jetnews项目的屏幕截图中 enter image description here 对于jetnews示例,请检查此链接https://developer.android.com/jetpack/compose/setup#sample

答案 1 :(得分:1)

关于darkThemeColorslightThemeColors
您可以根据需要定义它们。

对于0.1.0-dev03,请使用类似以下内容的

val lightThemeColors = ColorPalette(
    primary = Color(0xFFDD0D1F),
    surface = customSurface,
    onSurface = Color.Black
)

对于0.1.0-dev02,请使用类似以下内容的

val customSurface = Color(0xFF21212F.toInt())
private val lightThemeColors = MaterialColors(
    primary = Color(0xFFDD0D1F),
    surface = customSurface,
    onSurface = Color.Black
)

然后在您的代码中

setContent {
   MaterialTheme(colors = lightThemeColors) {
   // Greeting("Android")
   }
}