Search code examples
androidgradleandroid-manifestpreferenceactivity

Using PreferenceScreen with applicationIdSuffix


I'm having some problems making PreferenceScreen and applicationIdSuffix work together.

Supposing my app package/applicationId is com.myapp in Gradle and in AndroidManifest.xml, and applicationIdSuffix is defined as:

buildTypes {
    debug {
        applicationIdSuffix '.dev'
    }
}

If I define a PreferenceScreen as the following

<PreferenceScreen
    android:key="key_about" android:summary="something" android:title="About">
    <intent
        android:targetClass="com.myapp.activities.AboutActivity"
        android:targetPackage="com.myapp" />
</PreferenceScreen>

when launching the debug version of the app I get an exception

java.lang.SecurityException: Permission Denial: starting Intent { (...) } from ProcessRecord{(...)} (pid=13658, uid=10105) not exported from uid 10067

which makes sense, as I'm trying to launch an activity from a different application. The problem is that I can't find a way to Android launch the right activity.

If I change targetClass to ".activities.AboutActivity" it still can't find the activity

ActivityNotFoundException: Unable to find explicit activity class {com.myapp.dev/.activities.AboutActivity}

I even tried to define a value for each of the versions with the correct package:

android:targetClass="@string/classname"
android:targetPackage="@string/packagename"

but it can't find the right activity:

ActivityNotFoundException: Unable to find explicit activity class
    {com.myapp.dev/com.myapp.dev.activities.AboutActivity};
have you declared this activity in your AndroidManifest.xml?

So how can I make this work?


Solution

  • Your confusion comes from the fact that applicationIdSuffix only changes the package name of the application (its unique id), but does not change the java package name of your classes inside the application, including your Activity.

    This means that you should declare in your preferences:

    android:targetClass="com.myapp.activities.AboutActivity"
    android:targetPackage="@string/packagename"
    

    Where targetClass is always the same, while targetPackage depends on your build type and can be com.myapp or com.myapp.dev.