androidandroid-activityadbactivity-manager

am start cannot find main activity listed in manifest


I am trying to start an activity for an app (which I did not write in case you were wondering) through the adb shell. The manifest has the lines:

    <activity android:label="@string/app_name" android:name="MainActivity$mainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

I have tried using the the commands

adb shell am start -W com.pkg.name/MainActivity$mainActivity
adb shell am start -W com.pkg.name/.MainActivity$mainActivity
adb shell am start -W com.pkg.name/.MainActivity
adb shell am start -a android.intent.action.MAIN -n com.pkg.name/MainActivity$mainActivity
adb shell am start -n com.pkg.name/MainActivity$mainActivity
etc...

and each and every one gives me the error:

Error type 3
Error: Activity class {com.pkg.name/com.pkg.name.MainActivity}
does not exist.

It does however work when clicking on the app icon in the emulator, and by grepping the logcat output I find that the activity being launched is called .MainActivity$mainActivit or .MainActivity, they both show up in the output. Can someone tell me why am start is not working and how to in fact start this activity without manually clicking the icon?

UPDATE: The solution given by laalto is almost right. It turns out it was a problem with the $ getting resolved as an environment variable, however the command he suggested doesn't quite do it. You need to put single quotes around to <pkgname/activityname> in addition to escaping the $.


Solution

  • In unix-like shells, $ is a shell metacharacter so the $mainActivity expands to whatever value the environment variable mainActivity holds, likely an empty value in your case.

    To escape it, use a backslash:

    adb shell am start -W com.pkg.name/.MainActivity\$mainActivity
    

    However, having an inner class as an entry point is sort of a code smell. Consider making the outer class your entry point. Then you wouldn't need $ in any form.