无法通过 adb 或 Appium 运行 Android 应用程序,因为 SecurityException: Permission Denial

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

我尝试对 Android 应用程序进行测试自动化。我有开发者提供的 activity 名称和包名称,所以它是正确的。我在访问 activity 时遇到问题。

我进行了很多研究,并且看到了一些类似的话题,例如:

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.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.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:53)
    at org.junit.runners.ParentRunner.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.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:53)
    at org.junit.runners.ParentRunner.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>

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

开发人员使用 fragments 来开发应用程序,所以我使用的 com.companyname.appname.activities.MainActivity 不再是主要的 activity。

相反,我首先使用 activity:

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

如果您的android设备已经root,您可以通过ADB命令启动您的任何Activity应用程序;如果不是root,你就可以启动应用程序的MainActivity。 例如:

D:>adb shell su(输入键)

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

我遇到过这个问题,发现appium取错了activity。选择正确的一项。它会 运行 没问题。

要选对 activity : 查看 appium 日志文件。 IE。在你的包裹名称之后。

而且我相信它是有效的。

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

在终端中,运行 adb shell

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

您应该在终端

中找到启动器activity

首先,运行命令在cmd上;

adb shell

dumpsys window windows | grep -E 'mCurrentFocus'

在运行ning之后,如果你确定你的appPackage和appActivity名字是正确的;检查是否有启动您正在尝试的 activity 的条件。

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

尝试启动 com.test.activities.MainActivity 会出现权限错误。 您需要先启动 com.test.activities.LoginActivity 才能到达 MainActivity。

在我们的示例中,启动器activity 是初始屏幕。更改它解决了问题,因为它是出现在我们应用程序登录页面之前的 activity。