做過JavaEE肯定對Spring不陌生,尤其是spring的IOC,真是太好用了。順著這個思想,Android上有沒有spring來實現IOC。搜尋一下,果然spring已經推出了spring for android,不過可惜的是它並不支援IOC,但是卻在官網發現了這個麼一篇文章http://blog.springsource.org/2011/08/26/clean-code-with-android/,裡面講了android依賴注入(IOC)的實現思想和已經實現依賴注入的幾個項目,自己感覺AndroidAnnotations最為出色,不僅jar包小,而且功能強大,極大的減少了代碼量。本文將會講到AndroidAnnotations的部署和簡單應用。
先看普通代碼和用AndroidAnnotations的比較:
布局檔案:
<LinearLayoutxmlns: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" android:orientation="vertical" tools:context=".MainActivity"> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter thecontent"/> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Content:"/> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="click me"/> </LinearLayout>
普通代碼:
public class MainActivity extends Activity { EditText et; TextView tv; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.et); tv = (TextView) findViewById(R.id.tv); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { tv.setText("Content:" +et.getText()); } }); }}
使用AndroidAnnotations的代碼
@EActivity(R.layout.activity_main)public class MainActivity extends Activity { @ViewById EditText et; @ViewById TextView tv; @Click void btn() { tv.setText("Content:" +et.getText()); } }
超簡潔有木有。
AndroidAnnotations部署
環境:
系統:windows 8 (64bit)
開發工具:Eclipse 3.8
JDK版本:jdk1.6
構建工具:Ant(Eclipse預設的build tool)
步驟:
1. 下載並匯入jar包
2. 配置Ant
3. 配置Eclipse
1. jar包官網https://github.com/excilys/androidannotations/wiki/Download;
解壓後的兩個jar包androidannotations-api-2.7.1.jar和androidannotations-2.7.1.jar分別放在項目的libs檔案夾下和compile-libs檔案夾下(compile-libs需要自己建立,建立在項目的根目錄下就行。如果放在了同一檔案夾下必然出錯,因為兩個包裡存在相同的檔案路徑和檔案名稱)。
2. 配置Ant只需要在項目的根目錄下建立兩個檔案即可(build.xml和custom_rules.xml)
建立build.xml使用cmd命令
android update project --path "$PROJECT_ROOT$"
如果沒有配置android環境變數要進入到..\sdk\tools\目錄下去執行,"$PROJECT_ROOT$"為項目的根路徑,例如:
D:\Program Files\adt-bundle-windows-x86_64\sdk\tools>androidupdate project --path F:\work_in_geekon\workspace\TestAA
至於custom_rules.xml手動建立即可,首先添加如下內容
<propertyname="generated.dir"value=".apt_generated"/><propertyname="generated.absolute.dir"location="${generated.dir}"/><propertyname="java.compilerargs"value="-s'${generated.absolute.dir}'"/><targetname="-pre-compile"> <mkdirdir="${generated.absolute.dir}"/></target>
開啟$ANDROID_SDK_ROOT$/tools/ant/build.xml(例如我的D:\ProgramFiles\adt-bundle-windows-x86_64\sdk\tools\ant\build.xml),找到節點<target name="-compile"…
<targetname="-compile"depends="-build-setup, -pre-build, -code-gen, -pre-compile"> ...</target>
將上述內容全部copy到custom_rules.xml中。找到以下節點(在custom_rules.xml檔案中),並添加
<fileset dir="compile-libs"includes="*.jar"/>
<target name="-compile" ...>
...
<path id="project.javac.classpath">
...
<fileset dir="compile-libs" includes="*.jar"/>
</path>
...
</target>
綠色部分為新增內容。儲存檔案,Ant的配置也就OK了。
3. 配置Eclipse。
選擇項目右鍵,Properties à Java Compiler ,確保編譯器版本為1.6。
Properties >> Java Compiler >> Annotation Processing >> Enable annotation processing(開啟)。
Properties >> Java Compiler >> Annotation Processing >> Factory Path >> 添加jar包,就是之前放在compile-libs目錄下的androidannotations-2.7.1.jar。
重新編譯(Clean)下項目既可以了。
注意:AndroidManifest.xml檔案裡的Activity的名字都要在原來的基礎上加一個底線(”_”)。例如
<activityandroid:name="com.example.testaa.MainActivity">
</activity>
改成
<activityandroid:name="com.example.testaa.MainActivity_"></activity>
在Activity跳轉的時候也要如此new Intent().setClass(this, MainActivity_.class);
除了@Eactivity @ViewById@Click之外還有
@EApplication
@EBean
@EFragment
@EService
@EView
@EviewGroup
@App
@Bean
@Fullscreen
……
更多的應用請參照
官網http://androidannotations.org/
GitHubhttps://github.com/excilys/androidannotations/wiki
PS:androidannotations項目在匯出的時候如果路徑包含中文就會提示錯誤路徑未找到。