Android applications often access the device's Contacts Provider to retrieve names, phone numbers, email addresses, and other contact information. This functionality is commonly used in messaging applications, contact management tools, CRM applications, dialers, and social applications.
The Contacts Provider has been available for many Android versions, so developers may have applications that have used the same queries for years without any problems.
However, Android 17 introduces changes to Contacts Provider 2 (CP2) that can cause some existing queries to fail when an application targets API level 37 or higher.
These changes are mainly related to two areas:
- Certain account-related columns are no longer available through the ContactsContract.Data view.
- Queries against ContactsContract.Data are subject to stricter SQL validation when the application does not have READ_CONTACTS permission.
The important thing is that Android has not removed the Contacts Provider or stopped applications from accessing contact information. Instead, it has tightened access to certain personal information and introduced stricter rules for queries made with limited contact access.
In this article, we will understand how the Contacts Provider stores information, why existing queries may fail, how to retrieve account information from the correct table, and how to update Kotlin code for Android 17.
Understanding the Android Contacts Provider
Before changing existing queries, it is important to understand how Android organizes contact information.
Android does not store every piece of contact information in one simple table. The Contacts Provider uses multiple related tables because a single person may have information from different accounts.
For example, suppose a user has the following contact:
Name: John Smith
Phone: +1 555 123 4567
Email: [email protected]
This contact might be available through a Google account, a work account, or another synchronization service.
Android needs to maintain the original information from each account while also presenting a combined contact to the user.
To support this, the Contacts Provider mainly uses three tables.
ContactsContract.Contacts
This table represents the people shown in the device's contacts application.
A contact can contain information from multiple sources. Android combines related raw contacts into a single contact when they appear to represent the same person.
For example, the same person may have one raw contact from a Google account and another from a work account.
The Contacts table represents the combined person.
Some important columns include:
| Column | Purpose |
|---|
| _ID | Identifies the aggregated contact |
| DISPLAY_NAME | Displays the person's name |
| LOOKUP_KEY | Provides a more stable way to locate a contact |
| HAS_PHONE_NUMBER | Indicates whether the contact has phone information |
The ID value identifies the current aggregated contact, while LOOKUPKEY is useful when an application needs to maintain a reference to a contact over time.
ContactsContract.RawContacts
A raw contact represents the original contact information associated with a particular account.
For example, imagine John Smith exists in both a Google account and a company account.
Android may store two raw contact records:
Both raw contacts may be associated with the same aggregated contact in ContactsContract.Contacts.
The RawContacts table contains account-related information such as:
- ACCOUNT_NAME
- ACCOUNT_TYPE
- DELETED
- CONTACT_ID
The ACCOUNT_NAME and ACCOUNT_TYPE columns identify the account associated with a raw contact.
This table is important for applications that need to understand where a contact originated.
ContactsContract.Data
This table stores the actual contact details.
It contains information such as:
- Display names
- Phone numbers
- Email addresses
- Postal addresses
- Organization details
- Website information
- Other contact data
A single raw contact can have multiple Data rows.
For example:
| DATA row ID | RAW_CONTACT_ID | MIME type | DATA1 |
|---|
| 501 | 101 | Phone | +1 555 123 4567 |
| 502 | 101 | Email | [email protected] |
| 503 | 101 | StructuredName | John Smith |
The RAW_CONTACT_ID column connects each Data row to its parent record in RawContacts.
This relationship is particularly important for understanding the Android 17 changes.
The Data table contains the actual contact details, while the RawContacts table contains information about the account that owns those details.
What Changes in Android 17?
Android 17 introduces changes to Contacts Provider 2 for applications targeting API level 37 or higher.
The changes affect how applications access the Data table.
There are two separate restrictions, and developers should understand the difference.
| Change | When it applies | What happens |
|---|
| Restriction of account-related columns | Apps targeting API 37 or higher | Certain PII columns are removed from the Data view |
| Strict SQL validation | Apps targeting API 37 or higher that query Data without READ_CONTACTS | Queries that do not meet strict column and grammar rules are rejected |
These changes are designed to reduce unnecessary exposure of personal information and make limited contact access safer.
Change 1: Account-Related Columns Are Restricted in the Data View
One common implementation pattern is to retrieve contact details and account information using a single query against ContactsContract.Data.
For example, an application might use the following projection:
val projection = arrayOf(
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.ACCOUNT_NAME,
ContactsContract.Data.ACCOUNT_TYPE
)
This approach may have worked on earlier Android versions.
However, Android 17 restricts the following columns when they are accessed through the Data view:
- ACCOUNT_NAME
- ACCOUNT_TYPE
- ACCOUNT_TYPE_AND_DATA_SET
These columns contain personally identifiable information about the account associated with a contact.
When an application targeting API 37 or higher requests these columns from ContactsContract.Data, they are no longer available through that view.
This means developers should not continue relying on the Data table to retrieve account information.
Why Did Android Make This Change?
Consider an application that only needs to display a phone number selected by the user.
The application does not necessarily need to know which Google account or work account stores that phone number.
Providing account details through a general contact-data view can expose information that is not required for the application's feature.
Android now separates these responsibilities more clearly.
The Data table provides contact details, while RawContacts provides account-related information.
How to Fix the Restricted Column Problem
The recommended solution is to retrieve the required contact data from ContactsContract.Data and retrieve account information separately from ContactsContract.RawContacts.
The relationship between these tables is maintained through RAW_CONTACT_ID.
For example:
ContactsContract.Data
|
| RAW_CONTACT_ID
|
v
ContactsContract.RawContacts
|
| ACCOUNT_NAME
| ACCOUNT_TYPE
|
v
Account Information
Instead of requesting account information directly from the Data table, first retrieve the RAW_CONTACT_ID.
Then use that ID to query the corresponding RawContacts record.
Step 1: Query Contact Details From Data
The following example retrieves phone numbers and their associated raw contact IDs.
import android.content.Context
import android.provider.ContactsContract
data class ContactPhone(
val rawContactId: Long,
val phoneNumber: String
)
fun getContactPhones(context: Context): List<ContactPhone> {
val result = mutableListOf<ContactPhone>()
val projection = arrayOf(
ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER
)
context.contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
null,
null,
null
)?.use { cursor ->
val rawIdIndex = cursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID
)
val numberIndex = cursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
while (cursor.moveToNext()) {
val rawContactId = cursor.getLong(rawIdIndex)
val phoneNumber = cursor.getString(numberIndex)
result.add(
ContactPhone(
rawContactId = rawContactId,
phoneNumber = phoneNumber
)
)
}
}
return result
}
This query retrieves only the information required for the phone-number feature.
The important part is that the query does not request ACCOUNT_NAME or ACCOUNT_TYPE from the Data view.
The RAW_CONTACT_ID value is retained so that account information can be retrieved separately when the application is authorized to access it.
Step 2: Retrieve Account Information From RawContacts
Now use the raw contact ID to retrieve the corresponding account information.
import android.content.Context
import android.provider.ContactsContract
data class ContactAccount(
val accountName: String?,
val accountType: String?
)
fun getContactAccount(
context: Context,
rawContactId: Long
): ContactAccount? {
val projection = arrayOf(
ContactsContract.RawContacts.ACCOUNT_NAME,
ContactsContract.RawContacts.ACCOUNT_TYPE
)
val selection =
"${ContactsContract.RawContacts._ID} = ?"
val selectionArgs = arrayOf(
rawContactId.toString()
)
context.contentResolver.query(
ContactsContract.RawContacts.CONTENT_URI,
projection,
selection,
selectionArgs,
null
)?.use { cursor ->
if (cursor.moveToFirst()) {
val accountName = cursor.getString(0)
val accountType = cursor.getString(1)
return ContactAccount(
accountName = accountName,
accountType = accountType
)
}
}
return null
}
This query uses the raw contact ID to locate the correct record.
The account information is now retrieved from the table that owns those columns.
There is one important permission consideration: moving a query to RawContacts does not automatically grant access to every raw contact. The application must still have the necessary authorization to read the requested information. A temporary Contact Picker grant should not be treated as unrestricted access to account metadata.
Step 3: Combine the Results
The application can combine the phone information and account information when both are available.
fun printContactDetails(context: Context) {
val contacts = getContactPhones(context)
contacts.forEach { contact ->
val account = getContactAccount(
context,
contact.rawContactId
)
println("Phone: ${contact.phoneNumber}")
println("Account: ${account?.accountName}")
println("Account Type: ${account?.accountType}")
}
}
For a production application, avoid executing a separate database query for every phone number if the contact list is large.
Instead, collect unique raw contact IDs and retrieve the required RawContacts records in a controlled way.
Also, do not display account details unless the feature genuinely needs them.
Change 2: Strict SQL Validation for Data Queries Without READ_CONTACTS
The second Android 17 change is different from the restricted-column change.
For applications targeting API level 37 or higher, Contacts Provider 2 enforces strict SQL query validation when the application accesses ContactsContract.Data without READ_CONTACTS permission.
Android enables two validation options:
- StrictColumns
- StrictGrammar
If a query does not satisfy these checks, the Contacts Provider rejects it and throws an exception.
This is especially important for applications that use the system Contact Picker or receive temporary access to selected contact information.
What Is StrictColumns?
StrictColumns checks which columns an application requests from the provider.
For example, an application may request a specific set of columns:
val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.Data.MIMETYPE,
ContactsContract.Data.DATA1
)
This is a straightforward projection.
However, applications should review queries that use:
- Columns that are not part of the requested data view.
- Custom SQL expressions in projection fields.
- Unnecessary or restricted account-related fields.
- Dynamic column names constructed from external input.
The safest approach is to request only the documented columns required by the feature.
What Is StrictGrammar?
StrictGrammar validates the SQL structure used by the query.
The ContentResolver.query() method accepts several query arguments:
contentResolver.query(
uri,
projection,
selection,
selectionArgs,
sortOrder
)
The selection and sort order are translated into SQL-like query components by the provider.
A query that relies on complex or nonstandard SQL syntax may be rejected when strict validation is enabled.
For example, applications should avoid building arbitrary SQL fragments using user input or constructing complicated selection expressions when a simple query can achieve the same result.
Use documented columns, simple conditions, and selection arguments.
Why Does This Affect Applications Using the Contact Picker?
The Android Contact Picker allows an application to access only the contact information selected by the user.
The application does not need unrestricted access to the entire contacts database.
In some cases, the system grants temporary access to the selected contact data.
When an application queries the Data table using this limited access without READ_CONTACTS, the new strict SQL validation applies.
This is why a query that worked when the application had broad contacts permission may fail when the same query runs using a temporary picker grant.
The application should be designed around the actual access it has received.
How to Write Safer Contacts Provider Queries
The best way to avoid strict-query failures is to keep queries simple and predictable.
Consider a feature that needs to retrieve a selected contact's phone number.
A clean query looks like this:
fun getPhoneNumbers(
context: Context
): List<String> {
val numbers = mutableListOf<String>()
val projection = arrayOf(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
context.contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
null,
null,
null
)?.use { cursor ->
val numberIndex = cursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
while (cursor.moveToNext()) {
numbers.add(
cursor.getString(numberIndex)
)
}
}
return numbers
}
This example retrieves only phone numbers.
It does not request unrelated account information or construct custom SQL expressions.
For a selected contact, the application should also use the URI and permissions returned by the picker rather than assuming that it can query the entire contacts database.
Use Selection Arguments Instead of String Concatenation
Avoid building query conditions by inserting values directly into SQL strings.
For example, do not construct a selection like this:
val selection =
"${ContactsContract.Data.RAW_CONTACT_ID} = $rawContactId"
Instead, use a parameter placeholder:
val selection =
"${ContactsContract.Data.RAW_CONTACT_ID} = ?"
val selectionArgs = arrayOf(
rawContactId.toString()
)
This keeps values separate from the query structure and avoids SQL injection-style mistakes.
It also makes queries easier to read and maintain.
Keep Projection Lists Explicit
Do not request every available column when the application only needs a few values.
For example, if the feature needs a contact name and phone number, request those fields rather than using a large projection containing account information, synchronization metadata, and unrelated contact properties.
A smaller projection makes the application's requirements clearer and reduces unnecessary data access.
Using the Android 17 Contact Picker
Android 17 also introduces a system Contact Picker that allows applications to request selected contact information without requiring broad READ_CONTACTS permission.
The new intent action is ACTION_PICK_CONTACTS.
This is useful for applications that need a user to select one or more contacts for a specific operation.
Examples include:
- Selecting a contact to invite.
- Choosing a person to share a document with.
- Selecting a recipient for a transaction.
- Choosing a contact for a message.
- Selecting a person to add to a task.
For these use cases, broad access to the entire contacts database is often unnecessary.
Launching the Contact Picker
The following example launches the system Contact Picker.
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContracts
private val contactPickerLauncher =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val selectedContactUri = result.data?.data
if (selectedContactUri != null) {
readSelectedContact(selectedContactUri)
}
}
}
fun openContactPicker() {
val intent = Intent(
Intent.ACTION_PICK_CONTACTS
)
contactPickerLauncher.launch(intent)
}
The picker allows the user to select contact information through a system interface.
The returned URI represents the selected contact information. The application should use that URI and the permissions provided by the system rather than assuming it has general access to Contacts Provider data.
Reading the Selected Contact
The application can use the returned URI to retrieve the selected contact information.
The exact data available depends on the picker selection and the access granted by the system.
For example, a query can request a phone number when the picker has granted access to that information.
fun readSelectedContact(
context: Context,
contactUri: Uri
): String? {
val projection = arrayOf(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
context.contentResolver.query(
contactUri,
projection,
null,
null,
null
)?.use { cursor ->
if (cursor.moveToFirst()) {
val index = cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
if (index >= 0) {
return cursor.getString(index)
}
}
}
return null
}
The URI returned by the picker must be treated as the source of access. Do not replace it with the general ContactsContract.Data.CONTENT_URI and expect the same limited permission to apply to the entire contacts database.
Also, the exact projection must match the type of URI and data returned by the picker. Test the selected-data contract on the target Android release.
When Should an Application Use READ_CONTACTS?
The Contact Picker is suitable for many user-initiated selection scenarios.
However, some applications genuinely need broad access to the contact database.
Examples include:
| Application type | Why broad access may be needed |
|---|
| Contact management application | Displays and manages the complete contact list |
| Dialer application | Matches incoming and outgoing phone numbers with saved contacts |
| Backup application | Backs up the user's contacts |
| Accessibility application | Provides contact-related accessibility features |
| CRM application | Manages contact information for business workflows |
| Keyboard application | Provides contact-name and email autocomplete |
| Social application | Performs server-side friend matching across the user's contacts |
Google Play introduced a Contacts Permission policy in 2026.
For applications targeting Android 17 or later, READ_CONTACTS may only be requested when the system Contact Picker is not sufficient for the application's core functionality.
Applications requesting this permission must complete the relevant Play Console declaration and explain why limited picker access cannot support their feature.
For example, an application that only needs to invite one selected person should generally use the Contact Picker rather than requesting access to the entire address book.
An application that provides a complete contact-management experience may have a stronger technical reason to request broad access, subject to the applicable policy review.
Handling Permission Checks Correctly
Even when an application declares READ_CONTACTS in its manifest, the user may not have granted the permission.
Always check the permission before performing a broad contacts query.
Manifest Configuration
<uses-permission
android:name="android.permission.READ_CONTACTS" />
Only include this permission if the application genuinely requires broad access and its use is permitted under Google Play policy.
Runtime Permission Check
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
fun hasContactsPermission(
context: Context
): Boolean {
return ContextCompat.checkSelfPermission(
context,
Manifest.permission.READ_CONTACTS
) == PackageManager.PERMISSION_GRANTED
}
Before reading the full contacts list, check the result.
if (hasContactsPermission(context)) {
val contacts = getAllContacts(context)
} else {
// Use the Contact Picker when suitable.
}
A permission check should happen immediately before the operation that requires the permission. Users can change application permissions through system settings, so an earlier permission result should not be treated as permanent.
A Practical Migration Example
Consider an existing application that reads phone numbers and account information from the Data table.
The original implementation might look like this:
val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.Data.DATA1,
ContactsContract.Data.ACCOUNT_NAME,
ContactsContract.Data.ACCOUNT_TYPE
)
contentResolver.query(
ContactsContract.Data.CONTENT_URI,
projection,
null,
null,
null
)
This implementation has two problems for an Android 17 migration.
First, it requests account-related columns that are restricted in the Data view.
Second, if the application does not have READ_CONTACTS, its Data query is subject to strict SQL validation.
A better implementation separates the responsibilities.
data class ContactRecord(
val rawContactId: Long,
val value: String,
val accountName: String?,
val accountType: String?
)
fun loadContactRecords(
context: Context
): List<ContactRecord> {
val records = mutableListOf<ContactRecord>()
val projection = arrayOf(
ContactsContract.Data.RAW_CONTACT_ID,
ContactsContract.Data.DATA1
)
context.contentResolver.query(
ContactsContract.Data.CONTENT_URI,
projection,
null,
null,
null
)?.use { cursor ->
val rawIdIndex = cursor.getColumnIndexOrThrow(
ContactsContract.Data.RAW_CONTACT_ID
)
val valueIndex = cursor.getColumnIndexOrThrow(
ContactsContract.Data.DATA1
)
while (cursor.moveToNext()) {
val rawId = cursor.getLong(rawIdIndex)
val value = cursor.getString(valueIndex) ?: continue
records.add(
ContactRecord(
rawContactId = rawId,
value = value,
accountName = null,
accountType = null
)
)
}
}
if (!hasContactsPermission(context)) {
return records
}
return records.map { record ->
val account = getContactAccount(
context,
record.rawContactId
)
record.copy(
accountName = account?.accountName,
accountType = account?.accountType
)
}
}
This example demonstrates the separation between contact details and account information.
There are two important production considerations.
First, DATA1 is a generic Contacts Provider column. Its meaning depends on the row's MIME type. A production query should filter by the relevant MIME type, such as Phone.CONTENT_ITEM_TYPE or Email.CONTENT_ITEM_TYPE, before treating DATA1 as a phone number or email address.
Second, this example is suitable only when the application has the appropriate access to the queried records. It does not turn a limited Contact Picker grant into broad contact access. When the application only has a picker grant, query the returned URI and process only the selected data.
Common Errors and How to Troubleshoot Them
Android 17 query changes can appear as different errors depending on the query and the access available to the application.
The following table describes the problems developers should investigate.
| Problem | Possible reason | Recommended fix |
|---|
| Query fails after targeting API 37 | Query uses unsupported SQL patterns without READ_CONTACTS | Simplify the projection and query conditions |
| Account columns are unavailable | Account fields are requested from ContactsContract.Data | Retrieve them from RawContacts when authorized |
| Query throws an exception | StrictColumns or StrictGrammar rejected the query | Review the projection, selection, and sort order |
| Contact Picker returns limited data | Application received access only to selected information | Request only the fields supported by the picker result |
| Account information is missing | The application does not have access to RawContacts metadata | Check permissions and redesign the feature if broad access is unnecessary |
| Cursor contains no rows | The URI, selection, or available permission does not match the requested data | Verify the returned URI and query requirements |
Check the Actual Exception
Do not assume every failed Contacts Provider query is caused by Android 17 strict validation.
A query may also fail because of:
- Missing runtime permission.
- An invalid content URI.
- An unsupported projection.
- A missing selection argument.
- A provider-specific restriction.
- An incorrect assumption about the returned data.
Log the exception during development.
try {
// Perform the Contacts Provider query.
} catch (exception: SecurityException) {
Log.e(
"Contacts",
"Contacts permission or access was denied",
exception
)
} catch (exception: IllegalArgumentException) {
Log.e(
"Contacts",
"Invalid Contacts Provider query",
exception
)
}
Avoid catching every exception and returning an empty contact list without recording the failure. That approach can hide a real compatibility problem.
Testing Contacts Provider Changes on Android 17
Testing should cover both the new query restrictions and the application's permission behavior.
Step 1: Build With API Level 37
Update the project configuration:
android {
compileSdk = 37
defaultConfig {
targetSdk = 37
}
}
The behavior change is associated with the target SDK, not simply the compile SDK.
Step 2: Test With READ_CONTACTS Granted
Run the application with broad contacts permission granted.
Verify that:
- Normal contact queries work.
- Account information is retrieved from RawContacts.
- The application does not request restricted account columns from Data.
Step 3: Test Without READ_CONTACTS
Revoke the permission and test the same features.
Verify that Data queries use supported projections and query conditions.
Step 4: Test Contact Picker Access
Launch ACTION_PICK_CONTACTS and select a contact.
Verify that the application can read the selected data through the returned URI.
Also verify that it does not attempt to read unrelated contacts.
Step 5: Test Older Android Versions
Run the application on earlier Android versions to confirm that the updated implementation continues to work.
Use runtime checks for Android 17-only APIs, and keep the normal Contacts Provider implementation compatible with the application's minimum supported Android version.
Step 6: Review Google Play Permission Requirements
If the application still requests READ_CONTACTS, review the Contacts Permission policy and complete the required declaration when applicable.
Best Practices for Android 17 Contacts Integration
Request Only the Data Your Feature Needs
A phone-number selection feature does not need access to every account field or the entire address book.
Use the smallest amount of contact information necessary.
Keep Data and Account Queries Separate
Use ContactsContract.Data for contact details and ContactsContract.RawContacts for account-related information.
Maintain the relationship through RAW_CONTACT_ID.
Prefer the System Contact Picker for Selection Workflows
If users only need to select one or more contacts, the system picker provides a more privacy-friendly solution than broad contacts access.
Use Parameterized Queries
Use selection arguments rather than inserting values directly into SQL strings.
This improves safety and makes the query easier to maintain.
Filter by MIME Type
The Data table stores different types of contact information.
When reading DATA1, filter by the relevant MIME type so that the application does not accidentally interpret an email address as a phone number or another value.
Handle Missing Data
Not every contact contains a phone number, email address, or account record.
The application should handle missing values without crashing.
Avoid Unnecessary Account Information
Account names and account types may reveal personal information.
Retrieve and process these fields only when they are genuinely required by the feature.
Test With Different Permission States
Test with broad permission granted, broad permission denied, and temporary picker access.
These are different access situations and should not be treated as interchangeable.
Conclusion
Android 17 changes how applications can query Contacts Provider 2, especially when accessing ContactsContract.Data.
The most important change is that ACCOUNT_NAME, ACCOUNT_TYPE, and ACCOUNT_TYPE_AND_DATA_SET are no longer available through the Data view for applications targeting API level 37 or higher. Developers that genuinely need this information should retrieve it from ContactsContract.RawContacts using RAW_CONTACT_ID, provided they have the required access.
Android 17 also applies StrictColumns and StrictGrammar validation to Data queries made without READ_CONTACTS permission. Applications using temporary Contact Picker access should therefore keep their queries simple, request only the required fields, and use the URI returned by the system.
The best migration strategy is to review existing Contacts Provider queries, separate contact details from account metadata, reduce unnecessary permission requests, and test every permission scenario before releasing the Android 17-targeted version.
These changes are not just API compatibility requirements. They encourage developers to build contact features that request less personal information and give users more control over their data.
Join the conversation! Your thoughts help the community grow.