編輯:Android 開發教學
把 GitHub 上的開源專案當成 module 引入,是 Android Studio 裡很常見的操作。本文以 xrecyclerview 為例,走過一次完整流程,並整理過程中會遇到的幾個典型錯誤。
把 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
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")
}
接著從選單走 File → New → Import Module,指定開源專案的路徑。

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

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



(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 + /


Android RoboGuice 使用指南(18):Inject Resources 注入資源
RoboGuice 對存取 res 目錄下的各類資源——drawable、array、string 等——也提供了注入支援。透過 @InjectResource,可以相当
Android 4.2 Jelly Bean 值得關注的新特性
Android 4.2 沿用了 Jelly Bean 這個名字,整體使用者體驗並沒有大幅改動,但新增的幾項功能確實值得關注。 Google Now 更加深入生活 新版 G
Android 4.3 Jelly Bean 更新重點整理
Android 4.3 Jelly Bean 雖未大幅改變外觀,但在細節與系統功能上有多項調整。相機與網路設定相機設定欄從圓形改為半圓形,並支援以音量鍵作為快門。Wi-F
android中如何判斷edittext中資料為空
在 Android 中判斷 EditText 是否為空,常見錯誤是直接使用物件的 equals 方法。若字串本身為 null,呼叫 equals 可能觸發空指標異常。推薦