# android-contentprovider-generator
**Repository Path**: duan-yitao/android-contentprovider-generator
## Basic Information
- **Project Name**: android-contentprovider-generator
- **Description**: A tool to generate Android ContentProviders.
- **Primary Language**: Java
- **License**: GPL-3.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2021-08-30
- **Last Updated**: 2021-08-30
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
Android ContentProvider Generator (acpg)
========================================
[](https://android-arsenal.com/details/1/111)
A tool to generate Android ContentProviders.
It takes a set of entity (a.k.a "table") definitions as the input, and generates:
- a `ContentProvider` class
- an `SQLiteOpenHelper` class
- one `Columns` class per entity
- one `Cursor` class per entity
- one `ContentValues` class per entity
- one `Selection` class per entity
- one `Model` interface per entity
- one `Bean` class per entity (optionally)
Usage
-----
There are two possible ways to generate the code:
1. as part of the build script (with a Gradle plugin)
2. as a one-time step (using a command line tool)
The Gradle plugin is perhaps the 'cleaner' way in the sense that the generated
code won't be part of the source (not checked into VCS). The configuration is declared inside
the Gradle script which allows to update it easily.
Alternatively, a one-time generation can be done (typically at the beginning of the project.)
The generated code is part of the source and checked into VCS: this allows you
to modify it if you need to.
You can decide which option is the best for your project :)
### Option 1: Gradle plugin
Add this to your app's `build.gradle`:
```groovy
buildscript {
dependencies {
classpath 'org.jraf:acpg-gradle-plugin:1.13.1'
}
}
apply plugin: 'org.jraf.acpg.gradleplugin'
(...)
// This is where you declare a few parameters used to generate the code
acpg {
// Where to find the entity files (see 'Entity files' below)
// Optional - default value: 'etc/acpg' in the root project
entitiesDir file('etc/acpg-entities')
// Java package in which all the code will be generated
providerJavaPackage 'com.example.app.provider'
// ContentProvider authority
// "${applicationId}" will be substituted by BuildConfig.APPLICATION_ID in the generated code
authority '${applicationId}.provider'
// Name of the provider class
providerClassName 'ExampleProvider'
// Name of the db file
databaseFileName 'example.db'
// Version of the db
databaseVersion 1
// Name of the SQLiteOpenHelper class
// Optional - default value: providerClassName + "SQLiteOpenHelper"
sqliteOpenHelperClassName 'ExampleSQLiteOpenHelper'
// Name of a subclass of BaseSQLiteOpenHelperCallbacks
// Optional - this allows you to get called when the db is opened/created/upgraded
sqliteOpenHelperCallbacksClassName 'ExampleSQLiteOpenHelperCallbacks'
// Whether to enable foreign keys support (see 'Advanced usage' below)
// Optional - default value: false
enableForeignKeys true
// Whether @Nullable/@NonNull annotations will be used in the generated code
// Optional - default value: false
useAnnotations true
// Whether support library classes are used or the Android SDK ones (e.g. CursorLoader)
// Optional - default value: false
useSupportLibrary true
// Whether to generate a 'Beans' class for each entity
// Optional - default value: true
generateBeans true
// Name of a boolean field in BuildConfig to enable/disable debug logging in the generated code
// Optional - default value: "DEBUG"
debugLogsFieldName 'LOG_DEBUG_PROVIDER'
// Version of the tool syntax (must be 4)
// The allows to break the build immediately if an incompatible version of the tool is used. Safety first!
// Optional - default value: 4
syntaxVersion 4
}
```
### Option 2: Command line tool
The configuration is the same, except you declare it in a file named `_config.json`
in the same folder as the entity files.
Here is an example:
```json
{
"syntaxVersion": 4,
"packageName": "com.example.app",
"providerJavaPackage": "com.example.app.provider",
"authority": "${applicationId}.provider",
"providerClassName": "ExampleProvider",
"databaseFileName": "example.db",
"databaseVersion": 1,
"sqliteOpenHelperClassName": "ExampleSQLiteOpenHelper",
"sqliteOpenHelperCallbacksClassName": "ExampleSQLiteOpenHelperCallbacks",
"enableForeignKeys": true,
"useAnnotations": true,
"useSupportLibrary": true,
"generateBeans": true,
"debugLogsFieldName": "LOG_DEBUG_PROVIDER"
}
```
About `packageName`: this must be the same as the value of the `package` attribute in your manifest.
Not to be confused with the `applicationId` (see https://developer.android.com/studio/build/application-id.html)
#### Get and run the tool
Download the `acpg-cli-1.13.1.jar` file here:
https://github.com/BoD/android-contentprovider-generator/releases/latest
`java -jar acpg-cli-1.13.1.jar -i -o