Android Studio配置Android Annotations框架详解--说说那些坑

我们开发过程中都需要写些findViewByid、serOnclickListener等类似的代码,虽然不费事,但是一个项目下来,工作量还是很大的。为了节省工作量,运生了很多对应的注解框架。网上的博客、身边的同事大多使用的是 xUtils、ButterKnife实现注解,我初次使用的也是ButterKnife。然而,今天小试了下Android Annotations注解框架,用起来确实比ButterKnife爽。其他的介绍和废话就不多说了,下面就介绍如何在

Android Studio中配置Android Annotations框架,以及我所发现的坑。 AA官网:http://androidannotations.org/ AA Github:https://github.com/excilys/androidannotations AA Github wiki页:https://github.com/excilys/androidannotations/wiki AA Github Gradle配置页:https://github.com/excilys/androidannotations/wiki/Building-Project-Gradle 以上网址是很有用的,尤其是Gradle配置页。AA框架配置比较蛋痛,看似简单,但是有很多小细节。我今天在网上搜了很多博客,都没弄成功,总是报错。英文不好而官网的配置有一直没找到。其实不用再去看其他人写的博客文档了,或过时或错误很误导人。接下来我带领大家根据官方Gradle配置搭建和使用AA框架。
一、新建一个项目。 二、在project根目录下的build.gradle文件中,添加:

  1. classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'  
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

这个地方的版本号是1.8,就用最新的吧,怎么知道这个最新的版本号呢?去这儿查:右键整个项目-》Open Module Settings-》点击左侧的app-》点击右边顶部的Dependencies-》右上角的+号-》选择1.Dependency-》输入com.neenbedankt.gradle.plugins:android-apt 回车查询版本号。 这个文件的配置就完成了。官网文档和许多网上的博客都说还要修改依赖库,即mavenCentral()发现也可以不改,无所谓。

  1. apply plugin: 'com.android.application'    
  2. apply plugin: 'android-apt'  //1处添加    
  3. def AAVersion = '4.0.0'    
  4.     
  5. android {    
  6.     compileSdkVersion 23    
  7.     buildToolsVersion "24.0.0"    
  8.     
  9.     defaultConfig {    
  10.         applicationId "com.znke.androidannotations_test"    
  11.         minSdkVersion 15    
  12.         targetSdkVersion 23    
  13.         versionCode 1    
  14.         versionName "1.0"    
  15.     }    
  16.     buildTypes {    
  17.         release {    
  18.             minifyEnabled false    
  19.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'    
  20.         }    
  21.     }    
  22. }    
  23.     
  24. dependencies {    
  25.     compile fileTree(include: ['*.jar'], dir: 'libs')    
  26.     testCompile 'junit:junit:4.12'    
  27.     compile 'com.android.support:appcompat-v7:23.4.0'    
  28.     
  29.     apt "org.androidannotations:androidannotations:$AAVersion"  //2处添加    
  30.     compile "org.androidannotations:androidannotations-api:$AAVersion"    
  31. }    
  32.     
  33. apt {  //3处添加    
  34.     arguments {    
  35.         androidManifestFile variant.outputs[0]?.processResources?.manifestFile//new style    
  36.         //androidManifestFile variant.processResources.manifestFile           //old style    
  37.         resourcePackageName "com.znke.androidannotations_test"                //your pakcage name    
  38.     }    
  39. }   
apply plugin: 'com.android.application'  
apply plugin: 'android-apt'  //1处添加  
def AAVersion = '4.0.0'  
  
android {  
    compileSdkVersion 23  
    buildToolsVersion "24.0.0"  
  
    defaultConfig {  
        applicationId "com.znke.androidannotations_test"  
        minSdkVersion 15  
        targetSdkVersion 23  
        versionCode 1  
        versionName "1.0"  
    }  
    buildTypes {  
        release {  
            minifyEnabled false  
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
        }  
    }  
}  
  
dependencies {  
    compile fileTree(include: ['*.jar'], dir: 'libs')  
    testCompile 'junit:junit:4.12'  
    compile 'com.android.support:appcompat-v7:23.4.0'  
  
    apt "org.androidannotations:androidannotations:$AAVersion"  //2处添加  
    compile "org.androidannotations:androidannotations-api:$AAVersion"  
}  
  
apt {  //3处添加  
    arguments {  
        androidManifestFile variant.outputs[0]?.processResources?.manifestFile//new style  
        //androidManifestFile variant.processResources.manifestFile           //old style  
        resourcePackageName "com.znke.androidannotations_test"                //your pakcage name  
    }  
} 

说明:apply plugin: 'android-apt'  是引用下面的这部分: 三、接下来在app module下的build.gradle文件中添加配置:

  1. apt {    
  2.     arguments {    
  3.         androidManifestFile variant.outputs[0]?.processResources?.manifestFile//new style    
  4.         //androidManifestFile variant.processResources.manifestFile           //old style    
  5.         resourcePackageName "com.znke.androidannotations_test"                //your pakcage name    
  6.     }    
  7. }  
apt {  
    arguments {  
        androidManifestFile variant.outputs[0]?.processResources?.manifestFile//new style  
        //androidManifestFile variant.processResources.manifestFile           //old style  
        resourcePackageName "com.znke.androidannotations_test"                //your pakcage name  
    }  
}

apt这部分是需要查找和解析androidManifestFile文件吧!切记,需要注意的地方

  1. androidManifestFile variant.outputs[0]?.processResources?.manifestFile  
androidManifestFile variant.outputs[0]?.processResources?.manifestFile

这句话,中间有问号,根据官网的来。有很多博客上都不是这么写的,坑死我了,总是报属性为空错误。里面的resourcePackageName可不写,无大碍。def AAVersion = '4.0.0'部分,就是定义了一个变量名AAVersion的值是'4.0.0',此处版本号用最新的吧。在dependencies 节点中添加

  1. apt "org.androidannotations:androidannotations:$AAVersion"    
  2. compile "org.androidannotations:androidannotations-api:$AAVersion"  
apt "org.androidannotations:androidannotations:$AAVersion"  
compile "org.androidannotations:androidannotations-api:$AAVersion"

两句,引用上面的版本号,貌似也可直接把$AAVersion替换成4.0.0版本号,去掉def AAVersion = '4.0.0',试过可行。算了还是根据上面官网的配置来吧。 def AAVersion = '4.0.0'里面的版本号根据org.androidannotations:androidannotations去搜索查询(依照最开始说的方式查询)。

配置完毕。接下来做个小例子试试手。第一个界面,一个输入框和一个按钮。点击按钮获取值,跳转到第二个页面并显示值。就这么简单,看看AA注解怎么用。上代码:

  1. AndroidManifest.xml  
  2. <application    
  3.         android:allowBackup="true"    
  4.         android:icon="@mipmap/ic_launcher"    
  5.         android:label="@string/app_name"    
  6.         android:supportsRtl="true"    
  7.         android:theme="@style/AppTheme">    
  8.         <activity android:name=".MainActivity_">    
  9.             <intent-filter>    
  10.                 <action android:name="android.intent.action.MAIN" />    
  11.     
  12.                 <category android:name="android.intent.category.LAUNCHER" />    
  13.             </intent-filter>    
  14.         </activity>    
  15.         <activity android:name=".Main2Activity_"/>//对,没错,最后面加下划线    
  16.     </application>  
AndroidManifest.xml
<application  
        android:allowBackup="true"  
        android:icon="@mipmap/ic_launcher"  
        android:label="@string/app_name"  
        android:supportsRtl="true"  
        android:theme="@style/AppTheme">  
        <activity android:name=".MainActivity_">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
        <activity android:name=".Main2Activity_"/>//对,没错,最后面加下划线  
    </application>
  1. MainActivity    
  2. @EActivity(R.layout.activity_main)  //注入页面    
  3. public class MainActivity extends AppCompatActivity {    
  4.     
  5.     @ViewById    
  6.     public EditText username;//如果页面的id与此处对象名一样,@ViewById后面可以省略  
  7.     
  8.     @Override    
  9.     protected void onCreate(@Nullable Bundle savedInstanceState) {    
  10.         super.onCreate(savedInstanceState);    
  11.         //setContentView(R.layout.activity_main);//这儿不要了    
  12.     
  13.         //跟UI无关的数据初始化可以写在这里    
  14.     
  15.     }    
  16.     
  17.     @Click    
  18.     void submit(){//如果页面button的id和方法名一样,@Click后面可以省略    
  19.         //跳转到Main2Activity界面,看看这个跳转牛逼不,包含了intent,extra带参数,start启动。    
  20.         Main2Activity_.intent(this).extra("msg",username.getText().toString()).start();    
  21.     }    
  22. }  
MainActivity  
@EActivity(R.layout.activity_main)  //注入页面  
public class MainActivity extends AppCompatActivity {  
  
    @ViewById  
    public EditText username;//如果页面的id与此处对象名一样,@ViewById后面可以省略
  
    @Override  
    protected void onCreate(@Nullable Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //setContentView(R.layout.activity_main);//这儿不要了  
  
        //跟UI无关的数据初始化可以写在这里  
  
    }  
  
    @Click  
    void submit(){//如果页面button的id和方法名一样,@Click后面可以省略  
        //跳转到Main2Activity界面,看看这个跳转牛逼不,包含了intent,extra带参数,start启动。  
        Main2Activity_.intent(this).extra("msg",username.getText().toString()).start();  
    }  
}
  1. R.layout.activity_main    
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     xmlns:tools="http://schemas.android.com/tools"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent">    
  6.     
  7.     <EditText    
  8.         android:id="@+id/username"    
  9.         android:layout_width="match_parent"    
  10.         android:layout_height="200dp"    
  11.         android:gravity="left" />    
  12.     
  13.     <Button    
  14.         android:id="@+id/submit"    
  15.         android:layout_width="match_parent"    
  16.         android:layout_height="50dp"    
  17.         android:layout_alignParentBottom="true"    
  18.         android:text="提交" />    
  19.     
  20. </RelativeLayout>  
R.layout.activity_main  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
  
    <EditText  
        android:id="@+id/username"  
        android:layout_width="match_parent"  
        android:layout_height="200dp"  
        android:gravity="left" />  
  
    <Button  
        android:id="@+id/submit"  
        android:layout_width="match_parent"  
        android:layout_height="50dp"  
        android:layout_alignParentBottom="true"  
        android:text="提交" />  
  
</RelativeLayout>
  1. Main2Activity    
  2. @EActivity(R.layout.activity_main2)    
  3. public class Main2Activity extends AppCompatActivity {    
  4.     
  5.     @ViewById    
  6.     public TextView text;    
  7.     
  8.     //获取intent里面的扩展值    
  9.     @Extra    
  10.     public String msg;    
  11.     
  12.     @Override    
  13.     protected void onCreate(Bundle savedInstanceState) {    
  14.         super.onCreate(savedInstanceState);    
  15.         //@EActivity(R.layout.activity_main2)处已经setContentView    
  16.         //setContentView(R.layout.activity_main2);    
  17.     
  18.         /**    
  19.          * 请关注Main2Activity_源码分析    
  20.          * 懂我的意思吗?    
  21.          */    
  22.         /*    
  23.         Main2Activity_类的定义    
  24.         public final class Main2Activity_ extends Main2Activity    
  25.                 implements HasViews, OnViewChangedListener {*/    
  26.     
  27.         /*    
  28.         Main2Activity_类的onCreate方法    
  29.         @Override    
  30.         public void onCreate(Bundle savedInstanceState) {    
  31.             ......    
  32.             super.onCreate(savedInstanceState);    
  33.             ......    
  34.             setContentView(R.layout.activity_main2);    
  35.         }*/    
  36.     }    
  37.     
  38.     @Override    
  39.     protected void onStart() {    
  40.         super.onStart();    
  41.     
  42.         text.setText(msg);    
  43.     
  44.     }    
  45. }  
Main2Activity  
@EActivity(R.layout.activity_main2)  
public class Main2Activity extends AppCompatActivity {  
  
    @ViewById  
    public TextView text;  
  
    //获取intent里面的扩展值  
    @Extra  
    public String msg;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //@EActivity(R.layout.activity_main2)处已经setContentView  
        //setContentView(R.layout.activity_main2);  
  
        /**  
         * 请关注Main2Activity_源码分析  
         * 懂我的意思吗?  
         */  
        /*  
        Main2Activity_类的定义  
        public final class Main2Activity_ extends Main2Activity  
                implements HasViews, OnViewChangedListener {*/  
  
        /*  
        Main2Activity_类的onCreate方法  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            ......  
            super.onCreate(savedInstanceState);  
            ......  
            setContentView(R.layout.activity_main2);  
        }*/  
    }  
  
    @Override  
    protected void onStart() {  
        super.onStart();  
  
        text.setText(msg);  
  
    }  
}

此文件中,我为什么没有吧text.setText(msg);放在onCreate方法中,而放在了onStart中?你试试,根据错误排查。

  1. R.layout.activity_main2    
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     xmlns:tools="http://schemas.android.com/tools"    
  4.     android:layout_width="match_parent"    
  5.     android:layout_height="match_parent">    
  6.     
  7.     <TextView    
  8.         android:id="@+id/text"    
  9.         android:layout_width="match_parent"    
  10.         android:layout_height="match_parent"    
  11.         android:gravity="center"    
  12.         android:textSize="30sp" />    
  13. </RelativeLayout>  
R.layout.activity_main2  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent">  
  
    <TextView  
        android:id="@+id/text"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:gravity="center"  
        android:textSize="30sp" />  
</RelativeLayout>

代码使用部分,初略的贴出来了,有注释。 本文重点说配置,正确引导初学者。  

demo下载

http://blog.csdn.net/fesdgasdgasdg/article/details/51835461

原文地址:https://www.cnblogs.com/chen110xi/p/6616463.html