Mac Jenkins 構建時更改檔案內容

邢闖洋發表於2021-12-15

前言

本人使用的機器是 Mac M1,在開發 Android 專案時,有一個問題,是如果 Android 專案中有操作 SQLite 資料庫的地方,則無論打包還是編譯都會報一個錯

Mac Jenkins 構建時更改檔案內容

經過網上搜尋找到了一個解決方案,就是在專案根目錄的 build.gradle 中增加

allprojects {
    // 如果本地有操作sqlite,會報錯
    //Execution failed for task ':app:kaptDebugKotlin'.
    //> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
    //   > java.lang.reflect.InvocationTargetException (no error message)
    // https://issuetracker.google.com/issues/174695268?pli=1
    configurations.all {
        resolutionStrategy {
            force 'org.xerial:sqlite-jdbc:3.34.0'
        }
    }
}

但是這個內容我一直沒有提交到過 git

問題

問題來了,我在本地搭建了 Jenkins,並想實現 Android 自動打包,但是從 git 上拉下來的程式碼並沒有上面那段程式碼。所以我們需要通過構建指令碼來將上面這段程式碼每次構建的時候都追加到 build.gradle 檔案中

解決

專案根目錄新建 build-gradle-fix.txt

首先找到該專案在 jenkins 中的工作空間目錄

Mac Jenkins 構建時更改檔案內容

在該目錄下新建一個檔案, build-gradle-fix.txt,內容如下
需注意要有兩個空行



allprojects {
    // 如果本地有操作sqlite,會報錯
    //Execution failed for task ':app:kaptDebugKotlin'.
    //> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
    //   > java.lang.reflect.InvocationTargetException (no error message)
    // https://issuetracker.google.com/issues/174695268?pli=1
    configurations.all {
        resolutionStrategy {
            force 'org.xerial:sqlite-jdbc:3.34.0'
        }
    }
}

Jenkins 構建增加一個指令碼

Mac Jenkins 構建時更改檔案內容

build.gradle-fix.txt 在每次 jenkins 拉最新程式碼構建時,都將該檔案內容追加到 build.gradle
這樣就解決了這個問題

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章