File tree Expand file tree Collapse file tree 9 files changed +126
-10
lines changed
Expand file tree Collapse file tree 9 files changed +126
-10
lines changed Original file line number Diff line number Diff line change 2020apply plugin : ' com.android.application'
2121
2222android {
23- compileSdkVersion 25
24- buildToolsVersion " 24 .0.3"
23+ compileSdkVersion 27
24+ buildToolsVersion ' 25 .0.3'
2525 defaultConfig {
2626 applicationId " com.sample"
2727 minSdkVersion 15
28- targetSdkVersion 25
28+ targetSdkVersion 27
2929 versionCode 1
3030 versionName " 1.0"
3131 testInstrumentationRunner " android.support.test.runner.AndroidJUnitRunner"
@@ -47,8 +47,10 @@ dependencies {
4747 androidTestCompile(' com.android.support.test.espresso:espresso-core:2.2.2' , {
4848 exclude group : ' com.android.support' , module : ' support-annotations'
4949 })
50- compile ' com.android.support:appcompat-v7:25 .0.0'
50+ compile ' com.android.support:appcompat-v7:27 .0.0'
5151 testCompile ' junit:junit:4.12'
5252 compile ' net.zetetic:android-database-sqlcipher:3.5.7@aar'
53+ compile " android.arch.persistence.room:runtime:1.0.0"
54+ annotationProcessor " android.arch.persistence.room:compiler:1.0.0"
5355 debugCompile project(' :debug-db' )
5456}
Original file line number Diff line number Diff line change 3131import com .sample .database .ContactDBHelper ;
3232import com .sample .database .ExtTestDBHelper ;
3333import com .sample .database .PersonDBHelper ;
34+ import com .sample .database .room .User ;
35+ import com .sample .database .room .UserDBHelper ;
3436import com .sample .utils .Utils ;
3537
38+ import java .util .ArrayList ;
3639import java .util .HashSet ;
40+ import java .util .List ;
3741import java .util .Set ;
3842
3943public class MainActivity extends AppCompatActivity {
@@ -108,6 +112,19 @@ protected void onCreate(Bundle savedInstanceState) {
108112 }
109113 }
110114
115+ // Room database
116+ UserDBHelper userDBHelper = new UserDBHelper (getApplicationContext ());
117+ if (userDBHelper .count () == 0 ) {
118+ List <User > userList = new ArrayList <>();
119+ for (int i = 0 ; i < 20 ; i ++) {
120+ User user = new User ();
121+ user .id = (long ) (i + 1 );
122+ user .name = "user_" + i ;
123+ userList .add (user );
124+ }
125+ userDBHelper .insertUser (userList );
126+ }
127+
111128 Utils .setCustomDatabaseFiles (getApplicationContext ());
112129 }
113130
Original file line number Diff line number Diff line change 1+ package com .sample .database .room ;
2+
3+ import android .arch .persistence .room .Database ;
4+ import android .arch .persistence .room .RoomDatabase ;
5+
6+ /**
7+ * Created by anandgaurav on 12/02/18.
8+ */
9+ @ Database (entities = {User .class }, version = 1 )
10+ public abstract class AppDatabase extends RoomDatabase {
11+
12+ public abstract UserDao userDao ();
13+
14+ }
Original file line number Diff line number Diff line change 1+ package com .sample .database .room ;
2+
3+ import android .arch .persistence .room .Entity ;
4+ import android .arch .persistence .room .PrimaryKey ;
5+
6+ /**
7+ * Created by anandgaurav on 12/02/18.
8+ */
9+ @ Entity (tableName = "users" )
10+ public class User {
11+
12+ @ PrimaryKey
13+ public Long id ;
14+
15+ public String name ;
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .sample .database .room ;
2+
3+ import android .arch .persistence .room .Room ;
4+ import android .content .Context ;
5+
6+ import java .util .List ;
7+
8+ /**
9+ * Created by anandgaurav on 12/02/18.
10+ */
11+
12+ public class UserDBHelper {
13+
14+ private final AppDatabase appDatabase ;
15+
16+ public UserDBHelper (Context context ) {
17+ appDatabase = Room .databaseBuilder (context , AppDatabase .class , "User-Database" )
18+ .allowMainThreadQueries ()
19+ .build ();
20+ }
21+
22+ public void insertUser (List <User > userList ) {
23+ appDatabase .userDao ().insertAll (userList );
24+ }
25+
26+ public int count () {
27+ return appDatabase .userDao ().loadAll ().size ();
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ package com .sample .database .room ;
2+
3+ import android .arch .persistence .room .Dao ;
4+ import android .arch .persistence .room .Delete ;
5+ import android .arch .persistence .room .Insert ;
6+ import android .arch .persistence .room .OnConflictStrategy ;
7+ import android .arch .persistence .room .Query ;
8+
9+ import java .util .List ;
10+
11+ /**
12+ * Created by anandgaurav on 12/02/18.
13+ */
14+
15+ @ Dao
16+ public interface UserDao {
17+
18+ @ Query ("SELECT * FROM users" )
19+ List <User > loadAll ();
20+
21+ @ Query ("SELECT * FROM users WHERE id IN (:userIds)" )
22+ List <User > loadAllByIds (List <Integer > userIds );
23+
24+ @ Insert (onConflict = OnConflictStrategy .REPLACE )
25+ void insert (User user );
26+
27+ @ Insert (onConflict = OnConflictStrategy .REPLACE )
28+ void insertAll (List <User > users );
29+
30+ @ Delete
31+ void delete (User user );
32+
33+ }
Original file line number Diff line number Diff line change 2222buildscript {
2323 repositories {
2424 jcenter()
25+ google()
2526 }
2627 dependencies {
27- classpath ' com.android.tools.build:gradle:2.2.0 '
28+ classpath ' com.android.tools.build:gradle:2.3.3 '
2829 classpath ' com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
2930 classpath ' com.github.dcendents:android-maven-gradle-plugin:1.4.1'
3031
@@ -36,6 +37,7 @@ buildscript {
3637allprojects {
3738 repositories {
3839 jcenter()
40+ google()
3941 }
4042}
4143
Original file line number Diff line number Diff line change 2020apply plugin : ' com.android.library'
2121
2222android {
23- compileSdkVersion 25
24- buildToolsVersion " 24 .0.3"
23+ compileSdkVersion 27
24+ buildToolsVersion ' 25 .0.3'
2525
2626 defaultConfig {
27- minSdkVersion 9
28- targetSdkVersion 25
27+ minSdkVersion 14
28+ targetSdkVersion 27
2929 versionCode 1
3030 versionName " 1.0"
3131
@@ -50,6 +50,7 @@ dependencies {
5050 testCompile ' junit:junit:4.12'
5151 compile ' com.google.code.gson:gson:2.8.0'
5252 compile ' net.zetetic:android-database-sqlcipher:3.5.7@aar'
53+ compile " android.arch.persistence.room:runtime:1.0.0"
5354}
5455
5556// apply from: 'debug-db-upload.gradle'
Original file line number Diff line number Diff line change @@ -22,4 +22,4 @@ distributionBase=GRADLE_USER_HOME
2222distributionPath =wrapper/dists
2323zipStoreBase =GRADLE_USER_HOME
2424zipStorePath =wrapper/dists
25- distributionUrl =https\://services.gradle.org/distributions/gradle-2.14 .1-all.zip
25+ distributionUrl =https\://services.gradle.org/distributions/gradle-4 .1-all.zip
You can’t perform that action at this time.
0 commit comments