Integration of MonkeyTalk agent into the APK by the instrumentation with Ant (outside of Android Studio)

1.1k Views Asked by At

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'
}
1

There are 1 best solutions below

15
Suman On

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