搞安卓開發以來,一直使用的Eclipse,Google出了AndroidStudio(簡稱AS)以後,一直忙于項目沒有時間試試,今天裝了一個适應一下,然後想在裡面用上AndroidAnnotions(簡稱AA)(一個挺不錯的開源架構,用起來特别友善,如果你現在在用Spring的注解,就會想在android中也來這麼一個)。配置的時候遇到點問題!!!!!
1.首先,你可以上AndroidAnnotations的官網:http://androidannotations.org/,學習一下如何使用标簽;
2.在Eclipse中的配置參考:https://github.com/excilys/androidannotations/wiki/Eclipse-Project-Configuration
下面講講如何在AS中配置AA:
1.安裝AS,有錢的翻牆,沒錢的百度下載下傳AS;
2.建立AS的新項目,在左側切換至Project的Porject模式,友善我們檢視項目的目錄
------>
注意右圖裡的有2個build.gradle檔案,一個局部有效,一個全局有效,在局部build.gradle中(加入紅色字型)
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion='3.3.2'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.bq.facecode"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt {
arguments {
//老版本的寫法2.2.1以前
//androidManifestFile variant.processResources.manifestFile
//2.2.1以後
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'com.bq.facecode'//項目包名
}
}
然後在全局build.gradle中加入
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
需要注意的地方是,gradle的版本會造成一個錯誤:
No such property 'processResources' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated
遇到這個問題請不要慌張,檢視一下全局檔案中的紅色字型部分,是不是使用的
com.neenbedankt.gradle.plugins:android-apt:1.3,如果是可以改成1.4試試
再檢視一下你的gradle的版本,如果版本低于2.2.1,可能你得在局部檔案中使用
androidManifestFile variant.processResources.manifestFile //(低版本的寫法)
androidManifestFile variant.outputs[0].processResources.manifestFile //(高版本的寫法)
關于檢視項目Gradle的版本:
在項目左側檔案夾中找到gradle->wrapper->gradle-wrapper.properties,打開後即可看到
#Wed Oct 21 11:34:03 PDT 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
紅字那裡就是你目前使用的版本