fingerprintjs / fingerprintjs-android

Swiss army knife for identifying and fingerprinting Android devices. MIT license, no restrictions on usage in production.
https://fingerprint.com/github/
MIT License
586 stars 84 forks source link

Сordova #32

Closed pabloid64 closed 3 years ago

pabloid64 commented 3 years ago

Is it possible to use with Сordova? If yes, tell me how best to do it

makma commented 3 years ago

Hey @pabloid64! From my point if view, I believe it's possible, nevertheless I've never tried it. I've quickly investigated this problem and this approach seems good to me - https://stackoverflow.com/questions/52718004/kotlin-for-a-cordova-ionic-based-plugin. I believe @Alexey-Verkhovsky might have a more detailed answer.

Alexey-Verkhovsky commented 3 years ago

Hi! Since we don't have an iOS version of the library we haven't considered yet to build a cross-platform plugin.

But Cordova 9.0.0 fully supports Kotlin, so it's not a problem to build the plugin yourself.

The Kotlin plugin file should look like following:

package org.apache.cordova.plugin

import com.fingerprintjs.android.fingerprint.FingerprinterFactory
import com.fingerprintjs.android.fingerprint.Configuration
import org.apache.cordova.CallbackContext
import org.apache.cordova.CordovaPlugin
import org.json.JSONArray
import org.json.JSONException

class FingerprintAndroidCordovaPlugin : CordovaPlugin() {

    val fingerprinter = FingerprinterFactory.getInstance(
        cordova.getActivity().applicationContext,
        Configuration(version = 3)
    )

    @Throws(JSONException::class)
    fun execute(action: String, args: JSONArray, callbackContext: CallbackContext): Boolean {
        if (action == "fingerprint") {
            getFingerprint(callbackContext)
            return true
        }
        if (action == "deviceId") {
            getDeviceId(callbackContext)
            return true
        }

        return false
    }

    private fun getFingerprint(callbackContext: CallbackContext) {
        fingerprinter.getFingerprint { fingerprintResult ->
            callbackContext.success(fingerprintResult.fingerprint)
        }
    }

    private fun getDeviceId(callbackContext: CallbackContext) {
        fingerprinter.getDeviceId { result ->
            callbackContext.success(result.deviceId)
        }
    }

}

Also make sure that you imported fingerprint-android library in coresponding *.gradle files of the plugin.

You need to add a repository:

allprojects {   
  repositories {
  ...
  maven { url 'https://jitpack.io' }    
}}

and dependencies:

dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib:1.4.30"
  implementation "com.github.fingerprintjs:fingerprint-android:1.2"
}

After that in JS code will be available two actions: fingerprint and deviceId. Use the namespace you've set in plugin.xml

If there are some problems with building plugin - let us know, we'll try to help.

pabloid64 commented 3 years ago

Hello again. Thank you for your responses. I tried making a plugin. It is currently in the repository https://github.com/pabloid64/cordova-plugin-device-fingerprint. The plugin was installed into the project, it is available in the JS code, but it returns undefined (when trying to assign the result of a function to a variable). To the main project, I added: <preference name="GradlePluginKotlinEnabled" value="true" /> <preference name="GradlePluginKotlinCodeStyle" value="official" /> <preference name="GradlePluginKotlinVersion" value="1.3.50" /> ..... <plugin name="cordova-plugin-device-fingerprint" spec="https://github.com/pabloid64/cordova-plugin-device-fingerprint#master" />

In JS code I use like this: const device = fingerprint.fingerprint(function(msg) { return msg }, function(err) { return err });

I installed the dependencies you mentioned using build-extras.gradle, assuming that I will extend build.gradle in the project itself. I am also sending a screenshot of the final assembly file structure.

image

I'm not a mobile developer, so it's hard for me to quickly figure this out and have to do some things intuitively. If you have any thoughts why such a problem arises, I will be very grateful.

Alexey-Verkhovsky commented 3 years ago

Hey! Sorry for the delay. I took a look, and the only thing I noticed is probably an incorrect android packagename in the plugin.xml file. FingerPrint.ktfile should be in a directory that corresponds to packagename. Packagename is org.apache.cordova.plugin, so the directory should be src/org/apache/cordova/plugin.

Try to change plugin.xml from:

 <platform name="android">
      <config-file target="res/xml/config.xml" parent="/*">
      <feature name="FingerPrint">
        <param name="android-package" value="fingerprint.FingerPrint"/>
      </feature>
    </config-file>

    <source-file src="src/FingerPrint.kt" target-dir="app/src/main/kotlin/fingerprint"/>
    <source-file src="src/build-extras.gradle" target-dir="."/>
  </platform>
</plugin>

to

 <platform name="android">
      <config-file target="res/xml/config.xml" parent="/*">
      <feature name="FingerPrint">
        <param name="android-package" value="org.apache.cordova.plugin"/>
      </feature>
    </config-file>

    <source-file src="src/org/apache/cordova/plugin/FingerPrint.kt" target-dir="app/src/main/src/org/apache/cordova/plugin"/>
    <source-file src="src/build-extras.gradle" target-dir="."/>
  </platform>
</plugin>

Also don't forget to put FingerPrint.kt to src/org/apache/cordova/plugin.

pabloid64 commented 3 years ago

Thanks for the help. Unfortunately, I have not been able to solve this problem. I'll put it off until better times)