Issues using monkeyrunner startActivity

蹲街弑〆低调 提交于 2019-12-07 20:32:02

问题


I have read several posts online, including Android dev website http://developer.android.com/tools/help/monkeyrunner_concepts.html

I can't start an Activity through the startActivity method. I have tried several options. Here's an example code:

package = 'com.mydomain.mypackage'
activity = '.MyActivity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)

Also tried the following code:

package = 'com.mydomain.mypackage'
activity = 'com.mydomain.mypackage.MyActivity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)

But, nothing seems to launch the activity. I am expecting the money tool to start the activity meaning that I should be able to see the screen.

Any help shall be appreciated.

Thanks.


回答1:


Well, I'm glad you have solved your problem, but it's not a general solution, so I'd like to add a few things for people that come looking for an answer here. It took me so long to figure this out myself, so I hope I can help someone to save time:

My package name (the value of the "package" attribute of the element in my AndroidManifest.xml, as you have also mentioned) is: com.companyname.android.tv

and my main(launcher) activity is: com.companyname.android.tv.TvActivity

still this one doesn't work:

package = 'com.companyname.android.tv'
activity = 'com.companyname.android.tv.TvActivity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)

then I tried to run the activity from android studio, which succeeds and check the logs. that's what I found:

Launching application: com.companyname.android_tv_app/com.companyname.android.tv.TvActivity.

so I tried this one, which worked:

package = 'com.companyname.android_tv_app'
activity = 'com.companyname.android.tv.TvActivity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)

"android_tv_app" is our application name. it's not written on your manifest file. it occurs only in one place, build.gradle file of app, like this:

applicationId "com.companyname.android_tv_app"

So if you keep failing with startActivity with monkeyrunner, you can try replacing your package name with application id. and it makes sense, since monkeyrunner is trying to tell the system: "go find this application, run this activity" and without application id, looks like it can't find the package either.

Probably most of the people have the same package name and application id, because wizard helps you to keep it that way, but when you rename your application and/or package names, it's not the case.




回答2:


I fixed it...the solution was found here https://groups.google.com/forum/#!topic/android-developers/FvlBxSmNrk0

For example, if your (the value of the "package" attribute of the element in your AndroidManifest.xml) is "com.example.test.application" and your main Activity class name "MainActivity" in the Java package "com.example.myapp" then the component name is

com.example.test.application/com.example.myapp.MainActivity



来源:https://stackoverflow.com/questions/24215965/issues-using-monkeyrunner-startactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!