由于SecurityException:Permission Denial,无法通过adb或Appium运行Android应用

时间:2015-05-19 10:26:03

标签: android adb manifest appium

我尝试对Android应用进行测试自动化。我有开发人员的活动名称和包名称,所以它是正确的。我有访问活动的问题。

我研究了很多,并且我已经看过一些类似的线程,如:

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

当我输入 ./ adb shell am start com.companyname.appname / com.companyname.appname.activities.MainActivity 时,我收到以下消息:

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)

当我尝试使用Appium运行应用程序时,会抛出以下内容:

[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

我的清单文件如下所示:          

<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>

6 个答案:

答案 0 :(得分:4)

经过一些研究后,我发现了我的例外原因。

开发人员使用片段来开发应用,因此我使用的com.companyname.appname.activities.MainActivity不再是主要活动。

相反,我使用了第一次使用的活动:

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

答案 1 :(得分:4)

首先,在cmd上运行此命令;

adb shell

dumpsys window windows | grep -E 'mCurrentFocus'

运行后,如果您确定您的appPackage和appActivity名称正确,那么请执行以下步骤:检查是否有条件开始您尝试的活动。

例如;可以说,您的应用程序在访问MainActivity之前需要登录操作。

尝试启动com.test.activities.MainActivity会出现权限错误。 您需要先从com.test.activities.LoginActivity开始才能进入MainActivity。

答案 2 :(得分:2)

问题是您运行的活动不是启动器活动。我找到了一种工作方式。

在终端中,运行adb shell

在模拟器或设备中,单击应用程序图标后,立即在终端dumpsys window windows | grep -E 'mCurrentFocus'

中运行

您应该在终端

中找到启动器活动

答案 3 :(得分:0)

如果您的Android设备已植根,您可以通过ADB命令启动应用的任何活动;如果不是root,你就可以启动app的MainActivity。 例如:

D:&gt; adb shell su(输入密钥)

am start -n com.test.myapp / com.test.myapp.ui.AboutActivity

答案 4 :(得分:0)

我遇到过这个问题,发现appium采取了错误的活动。选择正确的一个。它会运行良好。

选择正确的活动:请参阅appium日志文件。即。在您的包裹名称之后。

我确信这是有效的。

答案 5 :(得分:0)

在我们的例子中,启动器活动是“启动”屏幕。更改此设置可以解决问题,因为它是我们应用程序登录页面之前显示的活动。