Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Studio 引用 Module 完整流程與常見報錯排查

編輯:Android 開發教學

把 GitHub 上的開源專案當成 module 引入,是 Android Studio 裡很常見的操作。本文以 xrecyclerview 為例,走過一次完整流程,並整理過程中會遇到的幾個典型錯誤。

1. 先在 gradle.properties 抽出共用變數

把 compileSdk、buildTools、support 版本等集中管理,各 module 就不必各自寫死版本號:

# Project-wide Gradle settings.
# Specifies the JVM arguments used for the daemon process.
org.gradle.jvmargs=-Xmx1536m

# 需要時才開啟並行建置(僅適用於彼此解耦的專案)
# org.gradle.parallel=true

COMPILE_SDK_VERSION=24
BUILD_TOOLS_VERSION=24.0.3
JUNIT_VERSION=4.12
SUPPORT_VERSION=24.2.1

# verticalrefreshlayout
LIB_MINI_SDK_VERSION=16
LIB_TARGET_SDK_VERSION=24

# sample
SAMPLE_MIN_SDK_VERSION=16
SAMPLE_TARGET_SDK_VERSION=23

2. 在 app 模組中引用這些變數

apply plugin: 'com.android.application'

android {
    compileSdkVersion Integer.parseInt(COMPILE_SDK_VERSION)
    buildToolsVersion BUILD_TOOLS_VERSION

    defaultConfig {
        applicationId "cn.baoyou.curiousby.com.newrproject"
        minSdkVersion SAMPLE_MIN_SDK_VERSION
        targetSdkVersion SAMPLE_TARGET_SDK_VERSION
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:appcompat-v7:$SUPPORT_VERSION"
    compile "com.android.support:recyclerview-v7:$SUPPORT_VERSION"
    compile "com.android.support:design:$SUPPORT_VERSION"
    compile 'com.tomergoldst.android:tooltips:1.0.6'
    compile 'q.rorbin:badgeview:1.0.5'
    compile 'com.muddzdev:styleabletoast:1.0.5'
    compile 'com.wang.avi:library:2.1.3'
    testCompile "junit:junit:$JUNIT_VERSION"
    compile project(":verticalrefreshlayout")
    compile project(":xrecyclerview")
}

3. 匯入 module

接著從選單走 File → New → Import Module,指定開源專案的路徑。

4. 下載並引入開源 module

本文示範的是 GitHub 上的 xrecyclerview:
https://github.com/curoiusby/XRecyclerView

5. 調整 module 自身的 gradle 設定

開源專案的 build.gradle 常寫死自己的 SDK 版本,記得對齊主專案,避免版本衝突。

6. Sync Now 並驗證

補充:四個常見報錯

(1)jsr305 版本衝突

在 gradle 中強制指定版本即可:

configurations.all {
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:2.0.1'
}

(2)com.android.ide.common.process.ProcessException

這個錯誤多半出現在把 Eclipse 專案移轉到 Android Studio 之後,肇因是 .9.png 圖片處理失敗。最省事的解法是把該圖片重新輸出成標準 png 後替換。

(3)Error:(1, 1) 錯誤: 非法字符: '\ufeff'

畫面上看不到任何亂碼,但檔案開頭帶了 BOM。解法是重新建立該類別,把內容複製進去,讓檔案以正確編碼重新寫入。

(4)DuplicateFileException:META-INF/LICENSE 重複

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException:
  com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE
  File1: ...\libs\httpclient-4.3.5.jar
  File2: ...\libs\httpcore-4.3.2.jar
  File3: ...\libs\httpmime-4.3.5.jar
  File4: ...\libs\jackson-all-1.6.0.jar

多個 jar 都帶了同一份授權檔,在 app 的 build.gradle 中排除即可:

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
}

附錄:常用快捷鍵

  • 行註解 //:Cmd + /(Mac)、Ctrl + /(Win/Linux)
  • 區塊註解 /**/:Cmd + Option + /、Ctrl + Shift + /
  • 格式化程式碼:Cmd + Option + L、Ctrl + Alt + L
  • 清除無效 import:Cmd + Option + O、Ctrl + Alt + O
  • 搜尋/取代:Cmd + F / Cmd + R、Ctrl + F / Ctrl + R
  • 上下移動程式碼行:Cmd + Shift + ↑/↓、Alt + Shift + ↑/↓
  • 刪除整行:Cmd + Delete、Ctrl + Y
  • 擴大/縮小選取範圍:Cmd + Option + ↑/↓、Ctrl + W / Ctrl + Shift + W
  • 快速生成結構(包裹):Cmd + Option + T、Ctrl + Alt + T
  • 覆寫方法/實作方法:Cmd + O、Ctrl + O
  • 定位到行首/行尾:Cmd + ←/→、Ctrl + ←/→
  • 折疊/展開程式碼區塊:Cmd + +/-、Ctrl + +/-
  • 折疊/展開全部:Cmd + Shift + +/-、Ctrl + Shift + +/-
  • 檢視檔案方法結構:Cmd + F12、Ctrl + F12
  • 尋找呼叫位置:Cmd + Option + H、Ctrl + Alt + H
  • 大小寫轉換:Cmd + Shift + U、Ctrl + Shift + U

捐助開發者

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved