using android studio,Using GreenDao with Android Studio

Step 1 - 建立 Android Application

這裡假設你已經在 Android Studio 中建立一個新的 Android Application,如果還沒有的話,請先建立一個新的 Android Application。

新建立好的 Android Application 的結構應如下:

├── app

│   ├── build.gradle

│   ├── libs

│   └── src

│      ├── androidTest

│      └── main

│          ├── AndroidManifest.xml

│          ├── java

│          │   └── cloudchen

│          │      └── com

│          │          └── daodemoexample

│          │              └── MainActivity.java

│          └── res

├── build.gradle

└── settings.gradle

* created under Android Studio 1.1 perview 1

Step 2 - 建立 DaoGenerator Module

File-->New Module...

選擇More Modules中的Java Library

61e4b1d1e5a1

填入對應的資訊

61e4b1d1e5a1

Step 3 - 編輯 DaoGenerator

執行到 Step 2 後,現在的資料夾結構應該像下圖這樣,有一個我們原本的 app module (Android Application),以及一個剛剛建立好的 daogenerator module (Java Library),並且新增了一個屬於 daogenerator module 的build.gradle檔案

61e4b1d1e5a1

編輯 build.gradle

因為這個daogeneratormodule 必須用到DaoGenerator這個由greenrobot所開發的 library,故我們必須編輯我們的daogenerator/build.gradle檔案,加入下面的相依套件:

dependencies{compilefileTree(dir:'libs',include:['*.jar'])// add DaoGeneratorcompile'de.greenrobot:DaoGenerator:1.3.0'}

編輯 MyDaoGenerator.java

設定好build.gradle檔案之後,就可以開始利用DaoGenerator來編寫我們的 Schema 了,譬如建立一個 User 資料表:

packagecom.cloudchen;

importjava.io.IOException;

importjava.nio.file.Files;

importjava.nio.file.LinkOption;

importjava.nio.file.Path;

importjava.nio.file.Paths;

importjava.nio.file.attribute.FileAttribute;

importjava.nio.file.attribute.PosixFilePermission;

importjava.nio.file.attribute.PosixFilePermissions;

importjava.util.Set;

importde.greenrobot.daogenerator.DaoGenerator;

importde.greenrobot.daogenerator.Entity;

importde.greenrobot.daogenerator.Schema;

publicclassMyDaoGenerator{

privatestaticfinalStringGENERATED_SRC_DIR="./app/gen-src";

publicstaticvoidmain(String[]args)throwsException{

Schemaschema=newSchema(1,MyDaoGenerator.class.getPackage().getName());

Entityentity=schema.addEntity("User");

entity.addLongProperty("UserId").notNull();

entity.addStringProperty("Password").notNull();

entity.addStringProperty("Name").notNull();

// need to create the folder by ourself, or the generateAll() method will cause I/O error

createDirs(Paths.get(GENERATED_SRC_DIR));

newDaoGenerator().generateAll(schema,GENERATED_SRC_DIR);

}

privatestaticvoidcreateDirs(Pathpath)throwsIOException{

if(Files.notExists(path,LinkOption.NOFOLLOW_LINKS)) {

Setperms=PosixFilePermissions.fromString("rwxr-x---");

FileAttribute>attrs=PosixFilePermissions.asFileAttribute(perms);

Files.createDirectories(path, attrs);

}

}

}

Run Configuration

選擇Run --> Edit Configurations...替我們的DaoGenerator新增一個執行設定

點選視窗左上角的+並新增一個Application的設置

設定所需參數

Name:這個設定檔的名稱,可任意命名

Main Class:你的 MyDaoGenerator.java 的位置

Use classpath of module:選擇 daogenerator module

設定完成後,選擇剛剛編輯好的設定,並且執行:

61e4b1d1e5a1

若是執行過程中沒有出錯,我們應該可以在我們的主要 module - 也就是app module中,找到新建立的gen-src資料夾,並且發現 DaoGenerator 已經幫我們把這些自動產生的類別給建立好了!

61e4b1d1e5a1

新增 greendao library

現在若是您點擊利用 DaoGenerator 所產生的DaoMaster.java或是其他自動產生的檔案,你會發現 Android Studio 的編譯器沒有辦法解析某些檔案內所使用到類別,那是因為這些類別是包在greenDao這個 library 的,我們還沒有引用它,所以再度編輯app/build.gradle,並且加上

dependencies{compilefileTree(dir:'libs',include:['*.jar'])compile'com.android.support:appcompat-v7:21.0.2'//add greendao librarycompile'de.greenrobot:greendao:1.3.7'}

再次重新 Sync Gradle 的設置之後,我們就可以開始使用 GreenDao 帶給我們的便利囉!

http://cloudchen.com/using-greendao-with-android-studio/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值