Android Studio 有內建的模組管理Setting,不過用過其他語言的專案依賴管理都有看過會類似一個file去記錄所有Libs的版本管理,譬如NodeJs 就有一個package.json專們負責依賴的版本控管。
AndroidStduio 主要是用Gradle 去做依賴管理,間單的說就是把專案所有模組的依賴可以寫成參考同一變數。
我也是參考網路的方法,是在專案根目錄底下建一個buildsystem資料夾,然後新增一個dependencies.gradle 的檔案。
檔案放置位置是在:
把你要使用的依賴都宣告在這,檔案內容大概像這樣
dependencies.gradle內容如下:
allprojects {
repositories {
jcenter()
}
}
ext {
//Android
androidBuildToolsVersion = "25.0.1"
androidSupportToolVersion = "25.2.0"
androidMinSdkVersion = 18
androidTargetSdkVersion = 25
androidCompileSdkVersion = 25
//Libraries
gsonVersion = '2.7'
butterKnifeVersion = '8.4.0'
appDependencies = [
gson: "com.google.code.gson:gson:${gsonVersion}",
butterKnife: "com.jakewharton:butterknife:${butterKnifeVersion}",
butterKnifeCompiler:"com.jakewharton:butterknife-compiler:${butterKnifeVersion}",
androidAppcompat: "com.android.support:appcompat-v7:${androidSupportToolVersion}",
androidDesign: "com.android.support:design:${androidSupportToolVersion}",
androidAnnotations: "com.android.support:support-annotations:${androidSupportToolVersion}"
]
appTestDependencies = [
androidAnnotations: "com.android.support:support-annotations:${androidSupportToolVersion}"
]
dataDependencies = [
androidAppcompat: "com.android.support:appcompat-v7:${androidSupportToolVersion}",
gson: "com.google.code.gson:gson:${gsonVersion}"
]
dataTestDependencies = [
androidAnnotations: "com.android.support:support-annotations:${androidSupportToolVersion}"
]
}
在Project gradle裡導入妳的dependencies.gradle
Project gradle內容:
apply from: 'buildsystem/dependencies.gradle'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
你的App gradle使用參考的方式做依賴跟build版本宣告
App gradle內容:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
android {
compileSdkVersion rootProject.ext.androidCompileSdkVersion
buildToolsVersion rootProject.ext.androidBuildToolsVersion
defaultConfig {
applicationId "go.deyu.anroidbloger"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
def appDependencies = rootProject.ext.appDependencies
compile appDependencies.androidAppcompat
compile appDependencies.androidDesign
compile appDependencies.gson
compile appDependencies.butterKnife
apt appDependencies.butterKnifeCompiler
testCompile 'junit:junit:4.12'
}
文章標籤
全站熱搜
留言列表