Android Jetpack Compose: Можно ли включить compose только для отладочного типа сборки?

#android #gradle #build.gradle #android-jetpack-compose

Вопрос:

Интересно, можем ли мы включить compose только для типов отладочных сборок ?

Я думаю, что это будет выглядеть так в build.gradle файле ->

 android { 

    buildTypes {

        debug {
            ...
            buildFeatures {
                compose true
            }
            composeOptions {
                kotlinCompilerExtensionVersion = composeCompilerVersion
            }
        }
        
        release {
            buildFeatures {
                compose false
            }
        }
    }
}
dependencies {
    debugImplementation "androidx.compose.ui:ui:$composeVersion"
    debugImplementation "androidx.compose.material:material:$composeVersion"
    debugImplementation "androidx.compose.ui:ui-tooling:$composeVersion"
    debugImplementation "com.google.android.material:compose-theme-adapter:$composeVersion"
    debugImplementation "androidx.compose.runtime:runtime-livedata:$composeVersion"
    debugImplementation "androidx.compose.compiler:compiler:$composeCompilerVersion"
    debugImplementation 'androidx.activity:activity-compose:1.3.1'
}

 

Я пробовал это, но это не компилируется. При выполнении ./gradlew assembleDebug у меня есть java.lang.Исключение IllegalStateException: не удалось найти встроенный метод Landroidx/compose/runtime/ComposablesKt;.помните

 org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: /Users/BruceWayne/AndroidStudioProjects/Libon/android-libon-lite/libon-lite-client/src/debug/kotlin/com/libon/lite/debug/CallLogDebugActivity.kt
The root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
    at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:239)
    at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:235)
Caused by: java.lang.RuntimeException: Exception while generating code for:
FUN name:FillCallLog visibility:private modality:FINAL <> ($this:com.libon.lite.debug.CallLogDebugActivity) returnType:kotlin.Unit
  annotations:
    Composable
  $this: VALUE_PARAMETER name:<this> type:com.libon.lite.debug.CallLogDebugActivity
  BLOCK_BODY
    VAR name:snackbarHostState type:androidx.compose.material.SnackbarHostState [val]
      CALL 'public final fun remember <T> (calculation: @[DisallowComposableCalls] kotlin.Function0<T of androidx.compose.runtime.ComposablesKt.remember>): T of androidx.compose.runtime.ComposablesKt.remember [inline] declared in androidx.compose.runtime.ComposablesKt' type=androidx.compose.material.SnackbarHostState origin=null
        <T>: androidx.compose.material.SnackbarHostState
        calculation: BLOCK type=kotlin.Function0<androidx.compose.material.SnackbarHostState> origin=LAMBDA
          COMPOSITE type=kotlin.Unit origin=null
          FUNCTION_REFERENCE 'private final fun FillCallLog$lambda-1 (): androidx.compose.material.SnackbarHostState declared in com.libon.lite.debug.CallLogDebugActivity' type=kotlin.Function0<androidx.compose.material.SnackbarHostState> origin=LAMBDA reflectionTarget=null
    CALL 'public final fun Column$default (modifier: androidx.compose.ui.Modifier?, verticalArrangement: androidx.compose.foundation.layout.Arrangement.Vertical?, horizontalAlignment: androidx.compose.ui.Alignment.Horizontal?, content: @[Composable] @[ExtensionFunctionType] kotlin.Function1<androidx.compose.foundation.layout.ColumnScope, kotlin.Unit>, $mask0: kotlin.Int, $handler: kotlin.Any?): kotlin.Unit [inline] declared in androidx.compose.foundation.layout.ColumnKt' type=kotlin.Unit origin=DEFAULT_DISPATCH_CALL
      modifier: COMPOSITE type=androidx.compose.ui.Modifier? origin=DEFAULT_VALUE
        CONST Null type=androidx.compose.ui.Modifier? value=null
      verticalArrangement: COMPOSITE type=androidx.compose.foundation.layout.Arrangement.Vertical? origin=DEFAULT_VALUE
        CONST Null type=androidx.compose.foundation.layout.Arrangement.Vertical? value=null
      horizontalAlignment: COMPOSITE type=androidx.compose.ui.Alignment.Horizontal? origin=DEFAULT_VALUE
        CONST Null type=androidx.compose.ui.Alignment.Horizontal? value=null
      content: BLOCK type=@[ExtensionFunctionType] kotlin.Function1<androidx.compose.foundation.layout.ColumnScope, kotlin.Unit> origin=LAMBDA
        COMPOSITE type=kotlin.Unit origin=null
        FUNCTION_REFERENCE 'private final fun FillCallLog$lambda-2 (this$0: com.libon.lite.debug.CallLogDebugActivity, $snackbarHostState: androidx.compose.material.SnackbarHostState): kotlin.Unit declared in com.libon.lite.debug.CallLogDebugActivity' type=@[ExtensionFunctionType] kotlin.Function1<androidx.compose.foundation.layout.ColumnScope, kotlin.Unit> origin=LAMBDA reflectionTarget=null
          this$0: GET_VAR '<this>: com.libon.lite.debug.CallLogDebugActivity declared in com.libon.lite.debug.CallLogDebugActivity.FillCallLog' type=com.libon.lite.debug.CallLogDebugActivity origin=null
          $snackbarHostState: GET_VAR 'val snackbarHostState: androidx.compose.material.SnackbarHostState [val] declared in com.libon.lite.debug.CallLogDebugActivity.FillCallLog' type=androidx.compose.material.SnackbarHostState origin=null
      $mask0: CONST Int type=kotlin.Int value=7
      $handler: CONST Null type=kotlin.Any? value=null
    CALL 'public final fun SnackbarHost$default (hostState: androidx.compose.material.SnackbarHostState, modifier: androidx.compose.ui.Modifier?, snackbar: @[Composable] kotlin.Function1<androidx.compose.material.SnackbarData, kotlin.Unit>?, $mask0: kotlin.Int, $handler: kotlin.Any?): kotlin.Unit declared in androidx.compose.material.SnackbarHostKt' type=kotlin.Unit origin=DEFAULT_DISPATCH_CALL
      hostState: GET_VAR 'val snackbarHostState: androidx.compose.material.SnackbarHostState [val] declared in com.libon.lite.debug.CallLogDebugActivity.FillCallLog' type=androidx.compose.material.SnackbarHostState origin=null
      modifier: COMPOSITE type=androidx.compose.ui.Modifier? origin=DEFAULT_VALUE
        CONST Null type=androidx.compose.ui.Modifier? value=null
      snackbar: COMPOSITE type=@[Composable] kotlin.Function1<androidx.compose.material.SnackbarData, kotlin.Unit>? origin=DEFAULT_VALUE
        CONST Null type=@[Composable] kotlin.Function1<androidx.compose.material.SnackbarData, kotlin.Unit>? value=null
      $mask0: CONST Int type=kotlin.Int value=6
      $handler: CONST Null type=kotlin.Any? value=null
    at org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
    
Caused by: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Couldn't inline method call: CALL 'public final fun remember <T> (calculation: @[DisallowComposableCalls] kotlin.Function0<T of androidx.compose.runtime.ComposablesKt.remember>): T of androidx.compose.runtime.ComposablesKt.remember [inline] declared in androidx.compose.runtime.ComposablesKt' type=androidx.compose.material.SnackbarHostState origin=null
Method: null
File is unknown
The root cause java.lang.IllegalStateException was thrown at: org.jetbrains.kotlin.codegen.inline.SourceCompilerForInlineKt.getMethodNode(SourceCompilerForInline.kt:109)
    
Caused by: java.lang.IllegalStateException: couldn't find inline method Landroidx/compose/runtime/ComposablesKt;.remember(Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
    at org.jetbrains.kotlin.codegen.inline.SourceCompilerForInlineKt.getMethodNode(SourceCompilerForInline.kt:109)
    at org.jetbrains.kotlin.codegen.inline.SourceCompilerForInlineKt.loadCompiledInlineFunction(SourceCompilerForInline.kt:86)
    at org.jetbrains.kotlin.backend.jvm.codegen.IrSourceCompilerForInline.compileInlineFunction(IrSourceCompilerForInline.kt:89)
    at org.jetbrains.kotlin.codegen.inline.InlineCodegen.performInline(InlineCodegen.kt:48)
    ... 70 more
 

I’m not even sure we can do that. The android documentation about BuildFeatures doesn’t mention a way to use it inside build types.