Using PreferenceScreen with applicationIdSuffix
I am having problems with the things that PreferenceScreen
both applicationIdSuffix
work together.
Let's assume my app package is / applicationId
is equal com.myapp
in Gradle and AndroidManifest.xml and applicationIdSuffix
defined as:
buildTypes {
debug {
applicationIdSuffix '.dev'
}
}
If I define a PreferenceScreen
as follows
<PreferenceScreen
android:key="key_about" android:summary="something" android:title="About">
<intent
android:targetClass="com.myapp.activities.AboutActivity"
android:targetPackage="com.myapp" />
</PreferenceScreen>
when running debug version of application i get exception
java.lang.SecurityException: Permission Denial: starting Intent { (...) } from ProcessRecord{(...)} (pid=13658, uid=10105) not exported from uid 10067
which makes sense as I am trying to launch an activity from another application. The problem is, I can't find a way to launch Android to work properly.
If I change targetClass
to ".activities.AboutActivity" it still cannot 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 cannot find the correct 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?
source to share
Your confusion is due to the fact that applicationIdSuffix
only changes the application package name (its unique identifier), but does not change the java package name for your classes within the application, including your activity.
This means that you must state in your preference:
android:targetClass="com.myapp.activities.AboutActivity"
android:targetPackage="@string/packagename"
Where is targetClass
always the same, but targetPackage
depends on your build type and can be com.myapp
or com.myapp.dev
.
source to share
What I do in my application is attach onPreferenceClickListener
to preference in my host activity and create an intent inside that listener method onPreferenceClick
. By creating an intent with code, you are referring directly to the target class and the question of packages never comes up.
source to share