Android – Cannot run Android app through adb or Appium because of SecurityException: Permission Denial

adbandroidappiummanifest

I try to do test automation of Android app. I have activity name and package name from the developer, so it is correct. I have a problem with access to activity.

I researched a lot and I've seen a few similar threads, like those:

Android: java.lang.SecurityException: Permission Denial: start Intent
https://discuss.appium.io/t/error-a-new-session-could-not-be-created-for-android-appium/2571

When I type ./adb shell am start com.companyname.appname/com.companyname.appname.activities.MainActivity I get following message:

Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.companyname.appname/.activities.MainActivity }
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.companyname.appname/.activities.MainActivity } from null (pid=13786, uid=2000) not exported from uid 10101
    at android.os.Parcel.readException(Parcel.java:1465)
    at android.os.Parcel.readException(Parcel.java:1419)
    at android.app.ActivityManagerProxy.startActivityAsUser(ActivityManagerNative.java:2133)
    at com.android.commands.am.Am.runStart(Am.java:680)
    at com.android.commands.am.Am.onRun(Am.java:270)
    at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
    at com.android.commands.am.Am.main(Am.java:76)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
    at dalvik.system.NativeStart.main(Native Method)

When I try to run the app using Appium, it throws following:

[31mMessage: [0m[31morg.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Permission to start activity denied.) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 9.86 seconds
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
System info: host: 'mareks-Air', ip: '192.168.1.21', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.2', java.version: '1.8.0_31'
Driver info: io.appium.java_client.AppiumDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:95)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:153)
    at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:49)
    at com.companyname.appname.core.AndroidConfig.initDriver(AndroidConfig.java:45)
    at com.companyname.appname.stepdefinitions.android.SetUpStepDefs.beforeScenario(SetUpStepDefs.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at cucumber.runtime.Utils$1.call(Utils.java:34)
    at cucumber.runtime.Timeout.timeout(Timeout.java:13)
    at cucumber.runtime.Utils.invoke(Utils.java:30)
    at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:59)
    at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:222)
    at cucumber.runtime.Runtime.runHooks(Runtime.java:210)
    at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:200)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
    at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
    at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:89)
    at cucumber.api.junit.Cucumber.runChild(Cucumber.java:40)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at cucumber.api.junit.Cucumber.run(Cucumber.java:94)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
[0m

My manifest file looks like this:

<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- GCM Permisions -->
<permission
    android:name="com.companyname.appname.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.companyname.appname.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- &lt;!&ndash; To auto-complete the email text field in the login form with the user's emails &ndash;&gt; -->
<!-- <uses-permission android:name="android.permission.GET_ACCOUNTS" /> -->
<!-- <uses-permission android:name="android.permission.READ_PROFILE" /> -->
<!-- <uses-permission android:name="android.permission.READ_CONTACTS" /> -->
<application
    android:name=".MyAppName"
    android:allowBackup="true"
    android:configChanges="locale"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:logo="@mipmap/ic_launcher"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="io.fabric.ApiKey"
        android:value="a602ec90eb6f41f23ad19b0cd98aeb92fe3170a2" />

    <activity
        android:name=".activities.fragments.otherActivities.LoginActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:windowSoftInputMode="adjustResize|stateVisible">
        <intent-filter android:label="@string/app_name">
            <action android:name="android.intent.action.MAIN"
                android:exported="true"/>


            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".activities.MainActivity"
        android:configChanges="locale"
        android:label="@string/app_name">
    </activity>
    <activity
        android:name=".activities.fragments.SubActivities.CalendarActivity"
        android:configChanges="orientation|keyboardHidden|screenSize" />
    <activity
        android:name=".activities.fragments.SubActivities.SurveyActivity"
        android:configChanges="orientation|keyboardHidden|screenSize" />
    <activity
        android:name=".activities.fragments.SubActivities.ShowContentActivity"
        android:configChanges="orientation|keyboardHidden|screenSize" />
    <activity
        android:name=".activities.fragments.otherActivities.SettingsActivity"
        android:label="Settings" />
    <activity
        android:name=".database.sql.AndroidDatabaseManager"
        android:theme="@style/Theme.AppCompat.Light" />

    <provider
        android:name=".database.sql.providers.SurveyProvider"
        android:authorities="com.companyname.appname.survey"
        android:enabled="true"
        android:exported="false" />
    <provider
        android:name=".database.sql.providers.MainProvider"
        android:authorities="com.companyname.appname.main"
        android:enabled="true"
        android:exported="false" />

    <!-- GCM Stuff -->
    <receiver
        android:name=".gcm.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="com.companyname.appname" />
        </intent-filter>
    </receiver>

    <service android:name=".gcm.GcmIntentService" />

    <activity
        android:name=".activities.fragments.SubActivities.PrivacyPolicyActivity"
        android:label="@string/title_activity_privacy_policy"></activity>
</application>

Best Solution

After some research I've found there reason of my exception.

The developers used fragments for developing the app, so com.companyname.appname.activities.MainActivity that I used was not main activity anymore.

Instead, I used first used activity:

com.companyname.appname.activities.fragments.otherActivities.LoginActivity
Related Question