{"id":19712584,"url":"https://github.com/evgenyneu/js-evaluator-for-android","last_synced_at":"2025-04-05T03:11:41.503Z","repository":{"id":15037299,"uuid":"17763202","full_name":"evgenyneu/js-evaluator-for-android","owner":"evgenyneu","description":"A library for running JavaScript in Android apps.","archived":false,"fork":false,"pushed_at":"2022-12-03T00:30:35.000Z","size":1796,"stargazers_count":484,"open_issues_count":22,"forks_count":84,"subscribers_count":24,"default_branch":"master","last_synced_at":"2025-03-29T02:07:47.135Z","etag":null,"topics":["android","javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"TheRealMJP/DeferredTexturing","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evgenyneu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-14T23:04:02.000Z","updated_at":"2025-01-31T09:22:03.000Z","dependencies_parsed_at":"2023-01-11T19:00:00.395Z","dependency_job_id":null,"html_url":"https://github.com/evgenyneu/js-evaluator-for-android","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evgenyneu%2Fjs-evaluator-for-android","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evgenyneu%2Fjs-evaluator-for-android/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evgenyneu%2Fjs-evaluator-for-android/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evgenyneu%2Fjs-evaluator-for-android/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evgenyneu","download_url":"https://codeload.github.com/evgenyneu/js-evaluator-for-android/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247280272,"owners_count":20912967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","javascript"],"created_at":"2024-11-11T22:17:43.302Z","updated_at":"2025-04-05T03:11:41.489Z","avatar_url":"https://github.com/evgenyneu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JsEvaluator library for running JavaScript in Android apps\n\n[![](https://jitpack.io/v/evgenyneu/js-evaluator-for-android.svg)](https://jitpack.io/#evgenyneu/js-evaluator-for-android)\n\n\nJsEvaluator may help you run JavaScript in an Android app and get the results. This is an alternative to `evaluateJavascript` method of the WebView. Supports Android version 4.0 (Ice Cream Sandwich) and newer.\n\n## Setup\n\nThere are two ways your can add JsEvaluator to your project:\n\n1. From a remote Maven repository.\n1. From a local .aar or .jar file.\n\n### 1. Setup from Maven repository in Android Studio\n\n1) Add `maven { url \"https://jitpack.io\" }` into `settings.gradle` file, in **dependencyResolutionManagement** / **repositories** section. For example:\n\n```gradle\ndependencyResolutionManagement {\n    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)\n    repositories {\n        google()\n        mavenCentral()\n        maven { url \"https://jitpack.io\" } // \u003c----- Add this\n    }\n}\n```\n\n2) Add `implementation 'com.github.evgenyneu:js-evaluator-for-android:v6.0.0'` into **dependencies** section of your **module** build.gradle file. For example:\n\n```gradle\ndependencies {\n    // ...\n    implementation 'com.github.evgenyneu:js-evaluator-for-android:v6.0.0'\n}\n```\n\nSee [Legacy setup instructions](https://github.com/evgenyneu/js-evaluator-for-android/wiki/Legacy-setup) if you are using Gradle older than version 7.\n\n### 2. Setup from local .aar file\n\nDownload [jsevaluator-1.0.aar](https://github.com/evgenyneu/js-evaluator-for-android/blob/master/jsevaluator/build/outputs/aar/jsevaluator-1.0.aar?raw=true). You can also build it yourself into *jsevaluator/build/outputs/aar/* directory in Android Studio with this command:\n\n```\n./gradlew :jsevaluator:aR\n```\n\n#### Add aar file to Android Studio project\n\nTo add JsEvaluator to your app in Android Studio:\n\n1. Copy the `jsevaluator-1.0.aar` to your **app/libs** folder.\n1. Add `compile(name:'jsevaluator-1.0', ext:'aar')` to **dependencies** block of your **module** build.gradle file.\n\n\n```Gradle\ndependencies {\n    compile(name:'jsevaluator-1.0', ext:'aar')\n}\n```\n\n1. Add the following code to the **allprojects/repositories** block of your **project** build.gradle file.\n\n```Gradle\nallprojects {\n    repositories {\n        jcenter()\n        flatDir {\n            dirs 'libs'\n        }\n    }\n}\n```\n\n#### Add jar file to Eclipse project\n\n1. Unzip the `jsevaluator-1.0.aar` and get the classes.jar from it. You may want to rename it to jsevaluator.jar.\n1. Open properties for **your** app project.\n1. Select `Libraries` tab in `Java Build Path`.\n1. Click `Add JARs...` button and select the .jar file.\n1. In your project properties click `Order and export` tab.\n1. Tick the .jar file.\n\n## Usage\n\nCreate evaluator instance variable in your activity:\n\n```Java\npublic class MainActivity extends AppCompatActivity {\n    JsEvaluator jsEvaluator = new JsEvaluator(this);\n```\n\n`this` is a reference to your activity.\n\n## Evaluate JavaScript\n\n```Java\njsEvaluator.evaluate(\"2 * 17\", new JsCallback() {\n  @Override\n  public void onResult(String result) {\n    // Process result here.\n    // This method is called in the UI thread.\n  }\n\n  @Override\n  public void onError(String errorMessage) {\n    // Process JavaScript error here.\n    // This method is called in the UI thread.\n  }\n});\n```\n\nNote: make sure to call `evaluate` method in UI thread.\n\n## Call a JavaScript function\n\n```Java\njsEvaluator.callFunction(\"function myFunction(a, b, c, d) { return 'result'; }\",\n  new JsCallback() {\n\n  @Override\n  public void onResult(String result) {\n    // Process result here.\n    // This method is called in the UI thread.\n  }\n\n  @Override\n  public void onError(String errorMessage) {\n    // Process JavaScript error here.\n    // This method is called in the UI thread.\n  }\n}, \"myFunction\", \"parameter 1\", \"parameter 2\", 912, 101.3);\n```\n\nAny number of string, int or double parameters can be supplied.\n\nNote: make sure to call `callFunction` method in UI thread.\n\n## JavaScript is evaluated asynchronously\n\nJavaScript is evaluated asynchronously without blocking UI thread. Result is returned in the UI thread. It is required to call `evaluate` and `callFunction` in UI thread.\n\n## JavaScript is evaluated in new context\n\nEach time the JavaScript is evaluated in the new context. It can not access the result of a previous evaluation.\nPlease concatenate all your JavaScript to one string and evaluate it in one go.\n\nFor example, if you need to load jQuery libary and then use it:\n\n```Java\nString jQuery = \"/*! jQuery JavaScript Library v2.1.1 ...\";\njsEvaluator.evaluate(jQuery + \"; $.isNumeric(123)\", new JsCallback() { ...\n```\n\n## Advanced functionality\n\n### Destroying the evaluator\n\nCalling the `destroy()` method will destroy the Web View used by JsEvaluator and clear the memory. JsEvaluator can not be used after it is destroyed.\n\n```Java\njsEvaluator.destroy();\n```\n\n### Accessing the WebView\n\nHere is how to get the instance to the web view used by the JsEvaluator.\n\n```Java\nWebView webView = jsEvaluator.getWebView();\n```\n\n\n## Known limitations\n\nThis library is suitable for evaluating only small amounts of JavaScript within hundreds of KB. [It has been reported](https://github.com/evgenyneu/js-evaluator-for-android/issues/24) that the library can not evaluate a megabyte of JavaScript. If you run into similar problems you can try [ericwlange/AndroidJSCore](https://github.com/ericwlange/AndroidJSCore) library instead.\n\n## How it works\n\nBehind the scenes it creates a `WebView` and feeds it JavaScript code for evaluation:\n\n```Java\nmWebView = new WebView(context);\nString javascript = \"\u003cscript\u003emyObj.returnResult('Hello World')\u003c/script\u003e\";\nbyte[] data = javascript.getBytes(\"UTF-8\");\nString base64 = Base64.encodeToString(data, Base64.DEFAULT);\nmWebView.loadUrl(\"data:text/html;charset=utf-8;base64,\" + base64);\n```\n\nThe result of evaluation is sent back into Android activity:\n\n```Java\npublic class JavaScriptInterface {\n   public void returnResult(String result) {\n      // result from JavaScript\n   }\n}\n\nmWebView.addJavascriptInterface(new JavaScriptInterface(), \"myObj\");\n```\n\n## Catching JavaScript errors\n\nThis library catches errors by wrapping the whole JavaScript code in a try-catch block. The errors are then returned to Java in the `onError` method of the callback object.\n\n```Java\njsEvaluator.evaluate(\"4 * octapod\", new JsCallback() {\n   @Override\n   public void onResult(String result) { }\n\n   @Override\n   public void onError(String errorMessage) {\n      // errorMessage =\u003e \"ReferenceError: octapod is not defined\"\n   }\n});\n```\n\nPlease note that this method only catches runtime JavaScript errors, like undefined variables or properties. It will not, however, catch errors resulted from malformed JavaScript code, like missing a `}` bracket.\n\n\n## Using with ProGuard\n\nIf you are using ProGuard (`minifyEnabled true`) you can add these rules to your *proguard-rules.pro* file.\n\n```\n# js-evaluator-for-android\n-keepattributes JavascriptInterface\n-keepclassmembers class * {\n    @android.webkit.JavascriptInterface \u003cmethods\u003e;\n}\n```\n\nYou will most likely not need to modify your proguard file because JSEvaluator uses [consumer proguard rules](https://github.com/evgenyneu/js-evaluator-for-android/issues/30).\n\n\n## Tests\n\nTests are located in `app` module of this project. The app can be run for manual testing as well.\n\n\u003cimg src='https://raw.githubusercontent.com/evgenyneu/js-evaluator-for-android/master/js_evaluator_screenshot_1_2014_08_30.png' width='270' alt='JsEvaluator library for Android screenshot 1'\u003e\n\n\u003cimg src='https://raw.github.com/evgenyneu/js-evaluator-for-android/master/js_evaluator_screenshot_2.png' width='270' alt='JsEvaluator library for Android screenshot 2'\u003e\n\n\nOr run as `Android JUnit Test` for unit testing.\n\nAndroid versions tested:\n\n* 4.0.3 (Ice Cream Sandwich)\n* 4.1.2, 4.2.2, 4.3 (Jelly Bean)\n* 4.4.2 (KitKat)\n* 5.0, 5.1 (Lollipop)\n* 6.0 (Marshmallow)\n* 8.0 (Oreo)\n\n## Thanks 👍\n\n* [codebymikey](https://github.com/codebymikey) for adding error handling.\n* [xbao](https://github.com/xbao) for adding a Gradle file.\n* [clydzik](https://github.com/clydzik) for fixing a [memory leak](https://github.com/evgenyneu/js-evaluator-for-android/pull/40) and simplifying the code.\n\n\n## Feedback is welcome\n\nIf you have any issues or need help please do not hesitate to create an issue ticket.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevgenyneu%2Fjs-evaluator-for-android","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevgenyneu%2Fjs-evaluator-for-android","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevgenyneu%2Fjs-evaluator-for-android/lists"}