3

I am working with Android Studio and I try to integre the MonkeyTalk agent into the APK with Ant.

Basically I have followed the follow steps:

1.Download MonkeyTalk 2.0.1 Pro Beta

2.Create a new empty folder on desktop titled “example” or whatever you like

3.Copy monkeytalkpro/agents/android/monkeytalk-agent-2.0.1.jar into “example” directory

4.Copy monkeytalkpro/ant/monkeytalkpro-ant-2.0.1.beta.jar into “example” directory

5.Copy your apk file into “example” directory (named myapp.apk for this example)

6.Create a new file called build.xml in “example” directory and fill it with the following:

<target name="instru">
    <mt:instrument
        srcfile="myapp.apk"
        destfile="myapp-post-instrumented.apk"
        agent="monkeytalk-agent-2.0.1.jar"
        androidsdk="/path/to/your/sdk"
        androidtarget="android-21"
        log="log.txt"
        verbose="true" />
    </target>

7.Open terminal and cd into your “example” directory

8.Issue command ant instru -lib monkeytalkpro-ant-2.0.1.beta.jar

9.The command should run and then produce a monkeytalk compatible apk in your “example” directory titled “myapp-post-instrumented.apk”

WARNING: There seems to be a bug where the instrumentation process will also place another file in your “example” directory titled “myapp-instrumented.apk”, but this file will be empty. So make sure your destination file is not titled “myapp-instrumented.apk” in your build.xml file or this empty file will overwrite your monkeytalk compatible file.

Source: http://w3facility.org/question/android-studio-and-monkeytalk/#answer-23016422

There are two differences between I have done and the original steps:

  1. The actual version of MonkeyTalk agent and the ant jar is 2.0.10, not 2.0.1

  2. With the compilation tool 21.1.2, the APKTool integrated in the monkeytalkpro-ant-2.0.10.beta.jar is not able to extract the files. So, I have updated the APKTool and generate a new jar. You can find it here: https://www.dropbox.com/s/qsjaz5g15hy9i7k/monkeytalkpro-ant-2.0.10.beta.mod.jar?dl=0

But event with this fix, ant is not capable to "instrument" the APK. Ant has problem to dex the file.

Any help and suggestion is appreciated.

P.d.1: I have tried to compile Android studio with gradle android plugin of aspectj, from uphyca(v0.9.7), but it does not works. It compiles the project but it crashes when I try to launch it.

P.d.2: Here are some logs:

-pre-compile:

-compile:

-post-compile:

-obfuscate:

-dex: [dex] input: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/bin/classes [dex] input: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/libs/monkeytalk-agent-2.0.10.jar [dex] input: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/libs/weaved.jar [dex] Pre-Dexing /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/libs/monkeytalk-agent-2.0.10.jar -> monkeytalk-agent-2.0.10-3ffc60da466c609b3dc28c1c7533ad15.jar [dex] Pre-Dexing /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/libs/weaved.jar -> weaved-0d60e12eb7c45a42662ee42c5398a922.jar [dx] [dx] EXCEPTION FROM SIMULATION: [dx] local 0005: invalid [dx] [dx] ...at bytecode offset 0000000b [dx] locals[0000]: Landroid/support/v7/widget/ActionMenuView; [dx] locals[0001]: Landroid/view/ViewGroup$LayoutParams; [dx] locals[0002]: [dx] locals[0003]: [dx] locals[0004]: [dx] locals[0005]: [dx] ...while working on block 000b [dx] ...while working on method generateLayoutParams:(Landroid/view/ViewGroup$LayoutParams;)Landroid/support/v7/widget/ActionMenuView$LayoutParams; [dx] ...while processing generateLayoutParams (Landroid/view/ViewGroup$LayoutParams;)Landroid/support/v7/widget/ActionMenuView$LayoutParams; [dx] ...while processing android/support/v7/widget/ActionMenuView.class [dx] [dx] 1 error; aborting

BUILD FAILED /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/build_master.xml:892: The following error occurred while executing this line: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/build_master.xml:894: The following error occurred while executing this line: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/build_master.xml:906: The following error occurred while executing this line: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/build_master.xml:283: null returned: 1

Total time: 14 seconds : ERROR: Process exited with an error: 1 (Exit value: 1)

instrumented APK file was not created at: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/bin/app-debug-debug.apk

Error instrumenting apk '/Users/monkeyTalk/app-debug.apk': instrumented APK file was not created at: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/bin/app-debug-debug.apk

com.gorillalogic.monkeytalk.instrumentor.android.AndroidInstrumentor$InstrumentationException: instrumented APK file was not created at: /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423638478563_1/app-debug/bin/app-debug-debug.apk at com.gorillalogic.monkeytalk.instrumentor.android.AndroidInstrumentor.copyInstrumentedApk(AndroidInstrumentor.java:244) at com.gorillalogic.monkeytalk.instrumentor.android.AndroidInstrumentor.beginInstrumentation(AndroidInstrumentor.java:173) at com.gorillalogic.monkeytalk.instrumentor.android.AndroidInstrumentor.instrumentApk(AndroidInstrumentor.java:130) at com.gorillalogic.monkeytalk.ant.InstrumentTask.execute(InstrumentTask.java:62) at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106) at org.apache.tools.ant.Task.perform(Task.java:348) at org.apache.tools.ant.Target.execute(Target.java:390) at org.apache.tools.ant.Target.performTasks(Target.java:411) at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399) at org.apache.tools.ant.Project.executeTarget(Project.java:1368) at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) at org.apache.tools.ant.Project.executeTargets(Project.java:1251) at org.apache.tools.ant.Main.runBuild(Main.java:809) at org.apache.tools.ant.Main.startAnt(Main.java:217) at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280) at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)

And here is the complete log: https://www.dropbox.com/s/y3mo9ifgalpzf29/log.txt?dl=0

Update

This is the content of the build.gradle file.

apply plugin: 'com.android.application'
apply plugin: 'robolectric'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.jiahaoliuliu.monkeytalk"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 52
        versionName "1.9999"

        multiDexEnabled true
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        monkeytalk.initWith(buildTypes.debug)
        monkeytalk {
            applicationIdSuffix ".monkey"
        }
    }

    sourceSets {
        androidTest {
            setRoot('src/test')
        }
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

    dependencies {
        compile fileTree(dir: 'libs', include: '*.jar')
        compile 'com.android.support:appcompat-v7:21.+'
        compile 'com.github.chrisbanes.photoview:library:1.2.3'
        compile 'com.etsy.android.grid:library:1.0.5'
        compile 'com.facebook.android:facebook-android-sdk:3.21.1'
        compile 'com.google.android.gms:play-services-base:6.5.+'
        compile 'com.google.android.gms:play-services-wallet:6.5.+'
        compile 'com.google.android.gms:play-services-ads:6.5.+'
        compile 'com.google.android.gms:play-services-maps:6.5.+'
        compile 'com.google.android.gms:play-services-drive:6.5.+'
        compile 'com.google.android.gms:play-services-appindexing:6.5.+'
        compile 'com.google.android.gms:play-services-location:6.5.+'
        compile 'com.google.android.gms:play-services-identity:6.5.+'
        compile 'com.google.android.gms:play-services-plus:6.5.+'
        compile 'com.android.support:multidex:1.0.0'
        compile project(':Adjust')
        compile project(':aFileChooser-cbccac1d1cb74a6d57d25c5ded61e4bf4fc40c91')
        compile 'com.parse.bolts:bolts-android:1.+'
        compile 'com.android.support:recyclerview-v7:+'

        androidTestCompile 'org.robolectric:robolectric:2.4'
        androidTestCompile('junit:junit:4.12')
        androidTestCompile('org.mockito:mockito-core:1.10.19')
    }
}


robolectric {
    include '**/*Test.class'
}
jiahao
  • 3,373
  • 2
  • 35
  • 36

1 Answers1

2

I was facing the same issue but now I am able to instrument the apk. You should have apk in debug mode. Do NOT add any monkeytalk libs dependencies in your android studio. Add GET_TASKS permission in app/AndroidManifest.xml. In version 21 this api is depreciated so you need to set lower targetsdkversion say 19.

If you compile your sdk with lower version you will be able to instrument the apk successfully. :)

Cheers, Suman

Suman
  • 436
  • 4
  • 10
  • Hi: Thanks for the quick response. I have done what you said but I still get the same error with weaved.jar, almost the same as before: [dex] Pre-Dexing /private/var/folders/fb/xhjph6n17sl_tnc7gfs_0ny80000gp/T/tmp_AndroidInstrumentor1423724334892_1/app-debug-unaligned/libs/weaved.jar -> weaved-42d6d2f45b9eaecde1eed317142620bc.jar [dx] [dx] EXCEPTION FROM SIMULATION: [dx] local 000f: invalid [dx] [dx] ...at bytecode offset 00000055 [dx] locals[0000]: Lorg/codehaus/jackson/map/ObjectMapper; [dx] locals[0001]: Lorg/codehaus/jackson/JsonFactory; – jiahao Feb 12 '15 at 07:02
  • Can I just try enabling AspectJ plugin in Android Studio? – Suman Feb 12 '15 at 09:59
  • You could. It works for small projects but not for my project. Like I said in the question, there is a plugin provided by uphyca which integrates the aspectJ plugin for android studio, but it does not work for my project. It does compile and generates the APK, but the app cannot be executed in Android. When I try to run it, it generates exceptions and it stops. – jiahao Feb 12 '15 at 13:29
  • Could you please share AnrdoidManifest.xml and build.gradle? – Suman Feb 12 '15 at 20:06
  • unfortunately the project is in production and I cannot share with you this information at least it is completely necessary. I just opened a bounty for it. If none can fix the problem then I could share them. – jiahao Feb 15 '15 at 06:56
  • I just shared the build.gradle file. – jiahao Feb 22 '15 at 07:30
  • @Jiahao; couldn't find your grade file. I have switched to Appium tool from Monkeytalk because I was facing issues in detecting web view elements with monkey talk. Appium works just fine for me for hybrid apps. If you want to switch to appium, I can share the steps. – Suman Feb 23 '15 at 17:30
  • The build.gradle is attached with the original question. I just modified it. I have done some research on Appium. The interface to record the test is very ugly, unlike the MonkeyTalk's one. But since we have switched to Android Studio and Monkey Talk does not have a good integration with it, there is not so much choice. (The community of Robotium is switching to Appium, so Robotium is not a good option). – jiahao Feb 24 '15 at 13:51
  • By the way, did you find any good documentation about how to configure and run Appium tests on Android Studio? I try it but there is not so much documentation about it. – jiahao Feb 24 '15 at 13:52
  • I haven't found any good documentation online. just in bits and pieces.I have struggle and finally appium worked with android studio. I can create a document for you. Just give me today's time. You can share your email id, I will mail you the appium integration guide with android studio :) – Suman Feb 25 '15 at 11:09
  • Thanks. I found the sample code here: https://github.com/appium/sample-code and here is the plugin for java for appium, if it helps you: https://github.com/appium/java-client . My mail account is jiahaoliuliu@gmail.com Thanks. – jiahao Feb 25 '15 at 11:37
  • @jiahao Appium installation ------ > brew install node # get node.js > npm install -g appium # get appium > npm install wd # get appium client > appium & # start appium > node your-appium-test.js – Suman Feb 25 '15 at 12:05
  • @jiahao Create AppiumTest directory in your project directory in android studio. AppiumTest > src > test > java > appiumtest > test.java and in your appiumtest grade file add following : apply plugin: 'java' dependencies { def appProject = project(':app') // app is my project dir compile appProject testCompile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'io.appium:java-client:2.1.0' testCompile 'io.selendroid:selendroid-standalone:0.14.0' testCompile 'junit:junit:4.11' } /*adding this will take care of jar dependencies*/ – Suman Feb 25 '15 at 12:06
  • Cool! Seems to work for me. I run the command gradle clean assembleDebug testDebug and it could detect errors on the appium level. Thanks man. – jiahao Feb 25 '15 at 13:11
  • Nope. Full native app. – jiahao Feb 25 '15 at 14:40