Reputation: 9
I have a project which supports API 19 and higher. I want to implement jetpack compose to the project but i want to keep supporting below API 21. For this purpose, I created two flavors: "minApi21", "minApi19". I want minApi21 to support compose while minApi19 works with the old code.
Here is the build.gradle file:
android {
.
.
.
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
.
.
.
flavorDimensions "api"
productFlavors{
minApi21 {
dimension "api"
minSdkVersion 21
versionCode 2000 + android.defaultConfig.versionCode
}
minApi19 {
dimension "api"
minSdkVersion 19
versionCode 1000 + android.defaultConfig.versionCode
versionNameSuffix ".19"
}
}
.
.
.
if (getGradle().getStartParameter().getTaskRequests()
.toString().contains("MinApi21")){
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerVersion = "1.5.31"
kotlinCompilerExtensionVersion = "1.1.0-alpha05"
}
}
}
Here is the dependencies:
dependencies{
.
.
.
minApi21Implementation 'androidx.activity:activity-compose:1.3.1'
// Integration with activities
minApi21Implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-beta02"
minApi21Implementation "androidx.compose.runtime:runtime:1.1.0-alpha05"
// Compose Material Design
minApi21Implementation 'androidx.compose.material:material:1.0.2'
// Animations
minApi21Implementation "androidx.navigation:navigation-compose:2.4.0-alpha09"
minApi21Implementation 'androidx.compose.animation:animation:1.0.2'
// Tooling support (Previews, etc.)
minApi21Implementation "com.github.skydoves:landscapist-fresco:1.3.6"
minApi21Implementation "androidx.compose.ui:ui-tooling:1.0.2"
// Integration with ViewModels
minApi21Implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.4.0-beta01'
minApi21Implementation "com.google.accompanist:accompanist-permissions:0.18.0"
minApi21Implementation "androidx.compose.ui:ui:1.0.2"
minApi21Implementation "androidx.compose.ui:ui-tooling-preview:1.0.2"
minApi21Implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
androidTestMinApi21Implementation 'androidx.compose.ui:ui-test-junit4:1.0.2'
androidTestMinApi21Implementation "androidx.test.espresso:espresso-core:$espressoVersion"
androidTestMinApi21Implementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
androidTestMinApi21Implementation "androidx.compose.ui:ui-test-junit4:1.0.2"
}
With this build.gradle
file, both minApi19 and minApi21 debug versions build perfectly fine, but when i create signed bundles i got the error message:
Task :app:compileMinApi19ReleaseKotlin FAILED
androidx.compose.compiler.plugins.kotlin.IncompatibleComposeRuntimeVersionException: The Compose Compiler requires the Compose Runtime to be on the class path, but none could be found. The compose compiler plugin you are using (version 1.1.0-alpha05) expects a minimum runtime version of 1.0.0.
at androidx.compose.compiler.plugins.kotlin.VersionChecker.noRuntimeOnClasspathError(VersionChecker.kt:112)
at androidx.compose.compiler.plugins.kotlin.VersionChecker.check(VersionChecker.kt:89)
at androidx.compose.compiler.plugins.kotlin.ComposeIrGenerationExtension.generate(ComposeIrGenerationExtension.kt:61)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr$lambda-1(JvmIrCodegenFactory.kt:126)
at org.jetbrains.kotlin.psi2ir.Psi2IrTranslator.generateModuleFragment(Psi2IrTranslator.kt:89)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr(JvmIrCodegenFactory.kt:146)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.convertToIr$default(JvmIrCodegenFactory.kt:64)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.generateModule(JvmIrCodegenFactory.kt:59)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:35)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:321)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:113)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:56)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:169)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:92)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:412)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:112)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:358)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally$default(IncrementalCompilerRunner.kt:300)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl$rebuild(IncrementalCompilerRunner.kt:119)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:170)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:81)
at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:607)
at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1658)
I tried to declare compileOptions and buildFeatures in flavor, got the same error.
How can i solve this issue and why it fails only in release version?
Upvotes: 0
Views: 1305
Reputation: 11228
We're also playing around Jetpack Compose on an opensource project and had the same issue which solved by actually doing something like this only for the runtime part,
if (gradle.startParameter.taskNames.any { "minApi21" in it || "MinApi21" in it }) {
implementation("androidx.compose.runtime:runtime:$composeVersion")
}
(it is in Kotlin DSL not Groovy so you have to do the conversion yourself)
And here is the part I've done it, https://github.com/persian-calendar/DroidPersianCalendar/blob/2af5eb3/PersianCalendar/build.gradle.kts#L200
I know it is not ideal but was is good enough for us until compose itself gets useful enough so abandoning Android 4 worth the benefits of compose brings on the table and this makes it the switch actually worthy but this hacky solution makes the signed builds of separated flavor for compose actually work.
Upvotes: 1