The Fastest Way to create a sql based ContentProvider in Android using annotations (No reflection)
Gradle:
add the apt plugin as a dependency to your root build gradle as below:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6'
}
}apply the apt plugin to your main project's build.gradle
apply plugin: 'com.neenbedankt.android-apt'add the required dependencies as below
dependencies {
compile 'com.squareup:javapoet:1.2.0'
compile 'ckm.simple:simple_sql_provider_annotation:1.0.6'
compile 'ckm.simple:simple_sql_provider_processor:1.0.6'
}- Create ProviderConfig class that defines your content providers details e.g.
@SimpleSQLConfig(
name = "TestProvider",
authority = "just.some.test_provider.authority",
database = "test.db",
version = 1)
publicclassTestProviderConfigimplementsProviderConfig {
@OverridepublicUpgradeScript[] getUpdateScripts() {
returnnewUpgradeScript[0];
}
}This class file says - Create a ContentProvider called TestProvider - The authority for this Provider is "just.some.test_provider.authority" - The Provider uses a database file named "test.db" - The Current database version is 1 - provider UpdateScripts as a defined by the UpdateScripts class
- Annotate your Pojo file that defines a table in the Database as below.
@SimpleSQLTable(table = "test", provider = "TestProvider")
publicclassTest {
@SimpleSQLColumn("col_str")
publicStringmyString;
@SimpleSQLColumn(value = "col_int", primary = true)
publicintanInt;
@SimpleSQLColumn("col_integer")
publicintmyinteger;
@SimpleSQLColumn("col_short")
publicintmyshort;
@SimpleSQLColumn("col_short2")
publicintmyShort;
@SimpleSQLColumn("col_long")
publiclongmylong;
@SimpleSQLColumn("col_long2")
publicintmyLong;
@SimpleSQLColumn("col_double")
publiclongmydouble;
@SimpleSQLColumn("col_double2")
publicintmyDouble;
@SimpleSQLColumn("col_float")
publiclongmyfloat;
@SimpleSQLColumn("col_float2")
publicintmyFloat;
@SimpleSQLColumn("col_bigdecimal")
publicBigDecimalbigD;
@SimpleSQLColumn("col_bool")
publicbooleanmybool;
@SimpleSQLColumn("col_bool2")
publicbooleanmyBool;
@SimpleSQLColumn("col_date")
publicDatemydateCol;
}
```
3.Therebuildyourproject4.YouwillnowhaveaccesstofilesgeneratedforyoutoaccesstheTableprefixedwith "Table" using the usual Android ContentProvider methods, e.g. TestTable for the above class. The generated files have convinience functions for you to add values to the table, e.g. getContentValues() with an instance of the Test class to insert into db e.g. ```java Test testInstance = new Test(...); getContentResolver().insert(TestTable.CONTENT_URI,TestTable.getContentValues(testInstance,false)); ``` To get data from the database use: ```java Cursor cursor = getContentResolver().query(TestTable.CONTENT_URI,null,null,null,null); //one row Test testRow = TestTable.getRow(cursor,true); //multiple rows List<Test> testRows = TestTable.getRows(cursor,false); ``` 5. add your provider as usual to your AndroidManifest.xml file with a matching authority as defined in ProviderConfig class ```xml <provider android:authorities="just.some.test_provider.authority" android:name="example.kurt.test.TestProvider"/> ```##LicenseApache License (Version 2.0)You may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "ASIS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.