1. 程式人生 > >Android Studio中配置AndroidAnnotations,遇到的問題及解決方法

Android Studio中配置AndroidAnnotations,遇到的問題及解決方法

        搞安卓開發以來,一直使用的Eclipse,Google出了AndroidStudio(簡稱AS)以後,一直忙於專案沒有時間試試,今天裝了一個適應一下,然後想在裡面用上AndroidAnnotions(簡稱AA)(一個挺不錯的開源框架,用起來特別方便,如果你現在在用Spring的註解,就會想在android中也來這麼一個)。配置的時候遇到點問題!!!!!

下面講講如何在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
紅字那裡就是你當前使用的版本