<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: CJDARKLORD</title>
    <description>The latest articles on DEV Community by CJDARKLORD (@cjdarklord).</description>
    <link>https://dev.to/cjdarklord</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4245397%2F5156bca7-4884-4de7-bf97-cf94cdf666df.jpg</url>
      <title>DEV Community: CJDARKLORD</title>
      <link>https://dev.to/cjdarklord</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cjdarklord"/>
    <language>en</language>
    <item>
      <title>Android 14 foreground services: the specialUse migration that saved our VPN</title>
      <dc:creator>CJDARKLORD</dc:creator>
      <pubDate>Mon, 16 Nov 2026 04:05:45 +0000</pubDate>
      <link>https://dev.to/cjdarklord/android-14-foreground-services-the-specialuse-migration-that-saved-our-vpn-2df7</link>
      <guid>https://dev.to/cjdarklord/android-14-foreground-services-the-specialuse-migration-that-saved-our-vpn-2df7</guid>
      <description>&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt; — On Android 14 a regular app cannot start a foreground service with &lt;code&gt;systemExempted&lt;/code&gt;: &lt;code&gt;startForeground()&lt;/code&gt; throws &lt;code&gt;SecurityException&lt;/code&gt;, the service silently dies, and the user just sees "VPN is not working". For a VPN client that isn't a system app, the working path is &lt;code&gt;specialUse&lt;/code&gt; + &lt;code&gt;FOREGROUND_SERVICE_SPECIAL_USE&lt;/code&gt; + a &lt;code&gt;vpn&lt;/code&gt; subtype in the manifest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Context
&lt;/h2&gt;

&lt;p&gt;I maintain an Android VPN client built on &lt;code&gt;VpnService&lt;/code&gt;. Since API 26 it runs as a foreground service: persistent notification, &lt;code&gt;startForegroundService()&lt;/code&gt;, &lt;code&gt;startForeground()&lt;/code&gt; inside the service. That worked fine on Android 8–13.&lt;/p&gt;

&lt;p&gt;Android 14 (API 34) tightened the rules: &lt;strong&gt;every&lt;/strong&gt; foreground service must declare a type, and the type must be allowed for the app. Depending on what exactly the system considers violated, you get &lt;code&gt;MissingForegroundServiceTypeException&lt;/code&gt; or &lt;code&gt;SecurityException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For a VPN, &lt;code&gt;FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED&lt;/code&gt; looked logical:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;fgsType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERSION&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SDK_INT&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ServiceInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nf"&gt;startForeground&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NOTIF_VPN_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;notif&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fgsType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works constantly like an antivirus or a system daemon, so "system exempted" seemed right. The manifest got &lt;code&gt;android:foregroundServiceType="systemExempted"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On Android 12 this worked perfectly — the type wasn't enforced that strictly. On Android 14 it did not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symptoms
&lt;/h2&gt;

&lt;p&gt;On an API 34 emulator the app started, showed "Connecting…", and a few seconds later dropped into an error state. Logcat and the service's own log looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;E/AppVpnService: startForeground failed: SecurityException: ...
I/AppVpnService: SVC_FATAL reason=foreground_fail
I/AppVpnService: SVC_STOP reason=exhausted last=foreground_fail
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;startForeground()&lt;/code&gt; threw &lt;code&gt;SecurityException&lt;/code&gt;, the service never got the foreground flag, retries were exhausted (the app has a 3-attempt limit), and the user saw "Disconnected (error)".&lt;/p&gt;

&lt;p&gt;The important detail: &lt;strong&gt;the app did not crash&lt;/strong&gt;. The exception was caught, the UI kept working — but the VPN did not. This kind of bug is easy to miss if you only check "does the app crash?".&lt;/p&gt;

&lt;p&gt;The exact message varies between versions, but the root cause is the same: &lt;code&gt;systemExempted&lt;/code&gt; requires &lt;code&gt;android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED&lt;/code&gt;, which has &lt;code&gt;signature|privileged&lt;/code&gt; protection level — a regular app can't get it at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Android 12 didn't reproduce it
&lt;/h2&gt;

&lt;p&gt;Before Android 14 the FGS type was only needed for some permissions and wasn't enforced that hard. On Android 12 (API 31) the old code worked, so the bug only appeared on an API 34 emulator. Lesson: &lt;strong&gt;don't test foreground services only on older APIs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Another emulator surprise: after fixing FGS the native tunnel still didn't come up — the app is arm64-only and the x86 emulator uses ARM translation. So &lt;code&gt;SVC_FOREGROUND ok&lt;/code&gt; is a real win, but "full VPN on emulator" is not a valid test. You need a real arm64 device. That's a separate story.&lt;/p&gt;

&lt;h2&gt;
  
  
  What actually needs to change
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Manifest
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.FOREGROUND_SERVICE"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;uses-permission&lt;/span&gt; &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.FOREGROUND_SERVICE_SPECIAL_USE"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;service&lt;/span&gt;
    &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;".AppVpnService"&lt;/span&gt;
    &lt;span class="na"&gt;android:exported&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"false"&lt;/span&gt;
    &lt;span class="na"&gt;android:permission&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.permission.BIND_VPN_SERVICE"&lt;/span&gt;
    &lt;span class="na"&gt;android:foregroundServiceType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"specialUse"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;property&lt;/span&gt;
        &lt;span class="na"&gt;android:name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"&lt;/span&gt;
        &lt;span class="na"&gt;android:value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"vpn"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/service&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;android:foregroundServiceType="specialUse"&lt;/code&gt; — the type;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;FOREGROUND_SERVICE_SPECIAL_USE&lt;/code&gt; — a normal (not runtime) permission;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE" ...&amp;gt;&lt;/code&gt; — the subtype that explains &lt;em&gt;why&lt;/em&gt;. Google Play reviewers read it when the app is published there.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;fgsType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERSION&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SDK_INT&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;ServiceInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FOREGROUND_SERVICE_TYPE_SPECIAL_USE&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERSION&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SDK_INT&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nc"&gt;Build&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;VERSION_CODES&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Q&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;fgsType&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;startForeground&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NOTIF_VPN_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;notif&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fgsType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;startForeground&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NOTIF_VPN_ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;notif&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;foregroundStarted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SVC_FOREGROUND ok"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Throwable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;e&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;TAG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"startForeground failed: ${e.javaClass.simpleName}: ${e.message}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SVC_FOREGROUND fail error=${e.javaClass.simpleName}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things worth calling out:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Wrap &lt;code&gt;startForeground&lt;/code&gt; in try/catch.&lt;/strong&gt; Different APIs throw different exceptions: &lt;code&gt;SecurityException&lt;/code&gt;, &lt;code&gt;MissingForegroundServiceTypeException&lt;/code&gt;, &lt;code&gt;ForegroundServiceStartNotAllowedException&lt;/code&gt;. The app must not crash because the system denied foreground.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log into your own black box.&lt;/strong&gt; My service writes key events to its own log (&lt;code&gt;SVC_FOREGROUND ok&lt;/code&gt;, &lt;code&gt;SVC_FATAL reason=...&lt;/code&gt;). On a real device debugging these issues without it is much harder.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to verify FGS actually started
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Logs:&lt;/strong&gt; &lt;code&gt;SVC_FOREGROUND ok&lt;/code&gt; in the service log / logcat;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;dumpsys&lt;/code&gt;:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;adb shell dumpsys activity services com.example.vpnclient &lt;span class="o"&gt;|&lt;/span&gt; grep -i foreground
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Behaviour:&lt;/strong&gt; on Android 14 the service must not die 1–2 minutes after the screen turns off; the notification must stay visible.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Pitfalls
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;systemExempted&lt;/code&gt; is not for regular apps.&lt;/strong&gt; The &lt;code&gt;FOREGROUND_SERVICE_SYSTEM_EXEMPTED&lt;/code&gt; permission is &lt;code&gt;signature|privileged&lt;/code&gt;. Being a VPN is not a reason.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Play and &lt;code&gt;specialUse&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;specialUse&lt;/code&gt; itself is a valid path, but Play asks you to justify it: fill the FGS declaration in Play Console, otherwise the release is rejected. For RuStore and direct APK distribution there's no such declaration, but filling in the subtype still helps manual review.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;specialUse&lt;/code&gt; is not a free pass.&lt;/strong&gt; Reviewers check that the activity can't be classified as another type. For VPNs it's usually fine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time.&lt;/strong&gt; After &lt;code&gt;startForegroundService()&lt;/code&gt; you have roughly 5 seconds to call &lt;code&gt;startForeground()&lt;/code&gt;. Do heavy init after it, not before.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't use &lt;code&gt;connectedDevice&lt;/code&gt;/&lt;code&gt;dataSync&lt;/code&gt; for VPN.&lt;/strong&gt; If unsure — &lt;code&gt;specialUse&lt;/code&gt; with a clear subtype.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The silent regression.&lt;/strong&gt; If &lt;code&gt;startForeground&lt;/code&gt; throws and you swallow it, the app keeps running without foreground and Android kills the service in the background. Always check the result, not just the absence of a crash.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Checklist for VPN apps on Android 14+
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Manifest: &lt;code&gt;foregroundServiceType="specialUse"&lt;/code&gt;, &lt;code&gt;FOREGROUND_SERVICE_SPECIAL_USE&lt;/code&gt;, &lt;code&gt;PROPERTY_SPECIAL_USE_FGS_SUBTYPE=vpn&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;[ ] Code: on SDK ≥ 34 call &lt;code&gt;startForeground(id, notif, FOREGROUND_SERVICE_TYPE_SPECIAL_USE)&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;[ ] &lt;code&gt;startForeground&lt;/code&gt; wrapped in try/catch, error logged and handled;&lt;/li&gt;
&lt;li&gt;[ ] FGS declaration filled in Play Console (if publishing to Google Play);&lt;/li&gt;
&lt;li&gt;[ ] Tested on a real &lt;strong&gt;Android 14+&lt;/strong&gt; device, not only on older APIs;&lt;/li&gt;
&lt;li&gt;[ ] Logs show the equivalent of &lt;code&gt;SVC_FOREGROUND ok&lt;/code&gt;, not just "the app didn't crash".&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Android 14 forces you to pick an FGS type explicitly and verify it's allowed. For a VPN client that isn't a system app, &lt;code&gt;systemExempted&lt;/code&gt; is a dead end: the exception is thrown inside &lt;code&gt;startForeground&lt;/code&gt;, the service quietly dies, and the user sees "VPN is not working". &lt;code&gt;specialUse&lt;/code&gt; with the &lt;code&gt;vpn&lt;/code&gt; subtype is the supported, working option. And the most useful addition besides the fix is logging key service events into your own log — without it these bugs look like "it sometimes doesn't work".&lt;/p&gt;

</description>
      <category>android</category>
      <category>mobile</category>
      <category>networking</category>
      <category>security</category>
    </item>
  </channel>
</rss>
