diff --git a/app/build.gradle b/app/build.gradle index 488fc36..7a1b962 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,12 +20,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 25 - buildToolsVersion "24.0.3" + compileSdkVersion 27 + buildToolsVersion '25.0.3' defaultConfig { applicationId "com.sample" minSdkVersion 15 - targetSdkVersion 25 + targetSdkVersion 27 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -47,8 +47,10 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.0.0' + compile 'com.android.support:appcompat-v7:27.0.0' testCompile 'junit:junit:4.12' compile 'net.zetetic:android-database-sqlcipher:3.5.7@aar' + compile "android.arch.persistence.room:runtime:1.0.0" + annotationProcessor "android.arch.persistence.room:compiler:1.0.0" debugCompile project(':debug-db') } diff --git a/app/src/main/java/com/sample/MainActivity.java b/app/src/main/java/com/sample/MainActivity.java index d860940..bc3683d 100644 --- a/app/src/main/java/com/sample/MainActivity.java +++ b/app/src/main/java/com/sample/MainActivity.java @@ -31,9 +31,13 @@ import com.sample.database.ContactDBHelper; import com.sample.database.ExtTestDBHelper; import com.sample.database.PersonDBHelper; +import com.sample.database.room.User; +import com.sample.database.room.UserDBHelper; import com.sample.utils.Utils; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; public class MainActivity extends AppCompatActivity { @@ -108,6 +112,19 @@ protected void onCreate(Bundle savedInstanceState) { } } + // Room database + UserDBHelper userDBHelper = new UserDBHelper(getApplicationContext()); + if (userDBHelper.count() == 0) { + List userList = new ArrayList<>(); + for (int i = 0; i < 20; i++) { + User user = new User(); + user.id = (long) (i + 1); + user.name = "user_" + i; + userList.add(user); + } + userDBHelper.insertUser(userList); + } + Utils.setCustomDatabaseFiles(getApplicationContext()); } diff --git a/app/src/main/java/com/sample/database/room/AppDatabase.java b/app/src/main/java/com/sample/database/room/AppDatabase.java new file mode 100644 index 0000000..ef0ce31 --- /dev/null +++ b/app/src/main/java/com/sample/database/room/AppDatabase.java @@ -0,0 +1,14 @@ +package com.sample.database.room; + +import android.arch.persistence.room.Database; +import android.arch.persistence.room.RoomDatabase; + +/** + * Created by anandgaurav on 12/02/18. + */ +@Database(entities = {User.class}, version = 1) +public abstract class AppDatabase extends RoomDatabase { + + public abstract UserDao userDao(); + +} diff --git a/app/src/main/java/com/sample/database/room/User.java b/app/src/main/java/com/sample/database/room/User.java new file mode 100644 index 0000000..7869ce8 --- /dev/null +++ b/app/src/main/java/com/sample/database/room/User.java @@ -0,0 +1,17 @@ +package com.sample.database.room; + +import android.arch.persistence.room.Entity; +import android.arch.persistence.room.PrimaryKey; + +/** + * Created by anandgaurav on 12/02/18. + */ +@Entity(tableName = "users") +public class User { + + @PrimaryKey + public Long id; + + public String name; + +} diff --git a/app/src/main/java/com/sample/database/room/UserDBHelper.java b/app/src/main/java/com/sample/database/room/UserDBHelper.java new file mode 100644 index 0000000..cc7f54b --- /dev/null +++ b/app/src/main/java/com/sample/database/room/UserDBHelper.java @@ -0,0 +1,30 @@ +package com.sample.database.room; + +import android.arch.persistence.room.Room; +import android.content.Context; + +import java.util.List; + +/** + * Created by anandgaurav on 12/02/18. + */ + +public class UserDBHelper { + + private final AppDatabase appDatabase; + + public UserDBHelper(Context context) { + appDatabase = Room.databaseBuilder(context, AppDatabase.class, "User-Database") + .allowMainThreadQueries() + .build(); + } + + public void insertUser(List userList) { + appDatabase.userDao().insertAll(userList); + } + + public int count() { + return appDatabase.userDao().loadAll().size(); + } + +} diff --git a/app/src/main/java/com/sample/database/room/UserDao.java b/app/src/main/java/com/sample/database/room/UserDao.java new file mode 100644 index 0000000..e07b271 --- /dev/null +++ b/app/src/main/java/com/sample/database/room/UserDao.java @@ -0,0 +1,33 @@ +package com.sample.database.room; + +import android.arch.persistence.room.Dao; +import android.arch.persistence.room.Delete; +import android.arch.persistence.room.Insert; +import android.arch.persistence.room.OnConflictStrategy; +import android.arch.persistence.room.Query; + +import java.util.List; + +/** + * Created by anandgaurav on 12/02/18. + */ + +@Dao +public interface UserDao { + + @Query("SELECT * FROM users") + List loadAll(); + + @Query("SELECT * FROM users WHERE id IN (:userIds)") + List loadAllByIds(List userIds); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insert(User user); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertAll(List users); + + @Delete + void delete(User user); + +} diff --git a/build.gradle b/build.gradle index 43bc990..ba230a0 100644 --- a/build.gradle +++ b/build.gradle @@ -22,9 +22,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.0' + classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4' classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' @@ -36,6 +37,7 @@ buildscript { allprojects { repositories { jcenter() + google() } } diff --git a/debug-db/build.gradle b/debug-db/build.gradle index 93a8738..7e64d75 100644 --- a/debug-db/build.gradle +++ b/debug-db/build.gradle @@ -20,12 +20,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 25 - buildToolsVersion "24.0.3" + compileSdkVersion 27 + buildToolsVersion '25.0.3' defaultConfig { - minSdkVersion 9 - targetSdkVersion 25 + minSdkVersion 14 + targetSdkVersion 27 versionCode 1 versionName "1.0" @@ -50,6 +50,7 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.google.code.gson:gson:2.8.0' compile 'net.zetetic:android-database-sqlcipher:3.5.7@aar' + compile "android.arch.persistence.room:runtime:1.0.0" } //apply from: 'debug-db-upload.gradle' \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 859a010..d103af0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -22,4 +22,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip