Flutter/오류수정

[Flutter 오류수정] Gradle build failed to produce an .apk file

달님개발자 2025. 1. 18. 10:23
반응형

플러터앱을 안드로이드용 디버그 모드에서 잘실행되던게 릴리즈 모드에서 하니 아래처럼 에러가 난다.

Running Gradle task 'assembleRelease'...                           12.5s
Error: Gradle build failed to produce an .apk file. It's likely that this file was generated under /Users/dalnimbest/Documents/workspace/Flutter/helpee/build, but the tool couldn't find it.

 

apk는 잘 만들었는데, helpee/build 밑에서 못찼고 있단다. helpee는 프로젝트 명이고, 난 helpee프로젝트에 flavor가 여러개 있는데, 그중에 하나가 프로젝트명과 같은 helpee라는 flavor다.

 

 

 

flutter run –-flavor --release helpee -t lib/main.dart

 

보면 나는 아래의 경로에 생성된다. apk밑에 flavor들이 여러개 있기 때문에 helpee라는 flavor에 생성되고 있다.

 

Flutter에서는 outputs/flutter-apk에서 아래처럼 release apk가 있어야 한다. (왜 저경로인지는 어디서 설정하는지 모르겠다.)

 

기존 코드를 보니 assembleDebug일때는 생성된 apk파일을  outputs/flutter-apk밑으로 옮겨주는 코드가 있다. 기억은 안나지만 예전에 만들었던거 같다.

tasks.configureEach { task ->
    if (task.name == "assembleDebug") {
        task.finalizedBy("copyDebugApkToFlutterExpectedLocation")
    }
}

tasks.register('copyDebugApkToFlutterExpectedLocation') {
    doLast {
        def flavorNames = android.productFlavors.collect { it.name }
        def flavorName = flavorNames.isEmpty() ? "" : flavorNames[0].toLowerCase() // Use the first flavor dynamically

        def debugApkPath = "${buildDir}/outputs/apk/${flavorName}/debug/app-${flavorName}-debug.apk"
        def expectedPath = "${buildDir}/outputs/flutter-apk/app-debug.apk"
        def expectedDir = file(expectedPath).parentFile

        if (file(debugApkPath).exists()) {
            expectedDir.mkdirs()
            copy {
                from debugApkPath
                into expectedDir
                rename { "app-debug.apk" }
            }
            println "Debug APK copied to Flutter expected location: $expectedPath"
        } else {
            println "Debug APK not found at: $debugApkPath"
        }
    }
}

 

 

그래서 assembleRelease일때나오는 apk파일도 같이 옮기는 코드를 만들었다. (이상하게 rename이 되지 않는데, 그래도 컴파일이 정상적으로 된다)

tasks.configureEach { task ->
    if (task.name == "assembleDebug") {
        task.finalizedBy("copyDebugApkToFlutterExpectedLocation")
    } else if (task.name == "assembleRelease") {
        task.finalizedBy("copyReleaseApkToFlutterExpectedLocation")
    }
}

tasks.register('copyReleaseApkToFlutterExpectedLocation') {
    doLast {
        def flavorNames = android.productFlavors.collect { it.name }
        def flavorName = flavorNames.isEmpty() ? "" : flavorNames[0].toLowerCase() // Use the first flavor dynamically

        def releaseApkPath = "${buildDir}/outputs/apk/${flavorName}/release/app-${flavorName}-release.apk"
        def expectedPath = "${buildDir}/outputs/flutter-apk/app-release.apk"
        def expectedDir = file(expectedPath).parentFile

        if (file(releaseApkPath).exists()) {
            expectedDir.mkdirs()
            copy {
                from releaseApkPath
                into expectedDir
                rename { "app-release.apk" }
            }
            println "Release APK copied to Flutter expected location: $expectedPath"
        } else {
            println "Release APK not found at: $releaseApkPath"
        }
    }
}

 

 

다시 실행해보니 이제 정상적으로 돌아가고 있다. "Release APK copied to Flutter expected location"라고 하면서...

git:(dalnim*)> flutter run –-flavor --release helpee -t lib/main.dart 
Launching lib/main.dart on SM N971N in release mode...
Release APK copied to Flutter expected location: /Users/dalnimbest/Documents/workspace/Flutter/helpee/build/app/outputs/flutter-apk/app-release.apk
Running Gradle task 'assembleRelease'...                           12.5s
✓ Built build/app/outputs/flutter-apk/app-release.apk (120.3MB)
Installing build/app/outputs/flutter-apk/app-release.apk...            ⣻

 

 

 

로그에는 flutter-apk/app-release.apk라고 하는데, 내 폴더에는 app-helpee-release.apk가 생성되어 있고, app-release.apk는 없는데 왜 그런지는 잘 모르겠다. 일단 되니까 또 어물쩍 넘어간다..

 

 

app-release.apk 로 rename이 안되어서, 몇번을 테스트해보니 이제 app-release.apk가 생성된다.(같은 코드인데) 일단 또 넘어간다... 여기서 시간 너무 많이 소비되고 있다...

반응형