Cannot make custom android plugin work

plugins
android

#1

I am trying to create custom android plugin which wraps OkHttp library. I know there is already similar plugin but just wanted to do for learning purposes. But i couldn’t succeed. here is what i tried:

include.gradle

dependencies {
    // Describe plugin native Android dependencies like
	// compile "groupName:pluginName:ver" 
    compile "com.squareup.okhttp3:okhttp:3.10.0"
}

In .ts file

declare var com;
const okhttp = com.squareup.okhttp3.okhttp;
export class test extends Common {
    constructor() {
        super();
        console.log('--------------------constructor--------------');
        console.log(okhttp);
    }
}

But here okhttp is throwing undefined error
TypeError: Cannot read property 'okhttp3' of undefined


#2

Hi @pak437337, did you put dependencies in you NS package.json file?


#3

This happens mostly when your gradle dependency is not installed. Try a clean build.

Besides this kind of syntax may break snapshot builds. But you can do same if you are inside a function, just not in global scope of the file.

const okhttp = com.squareup.okhttp3.okhttp;

#4

i will give it a try on clean build but take a look at the @Eddy plugins https://github.com/EddyVerbruggen/nativescript-health-data/blob/master/src/health-data.android.ts. They are all done the same way…


#5

What dependencies ? I didn’t understand properly. My demo folder dependencies looks like this:

  "dependencies": {
    "@angular/animations": "~5.2.0",
    "@angular/common": "~5.2.0",
    "@angular/compiler": "~5.2.0",
    "@angular/core": "~5.2.0",
    "@angular/forms": "~5.2.0",
    "@angular/http": "~5.2.0",
    "@angular/platform-browser": "~5.2.0",
    "@angular/platform-browser-dynamic": "~5.2.0",
    "@angular/router": "~5.2.0",
    "nativescript-angular": "~5.3.0",
    "nativescript-http-formdata": "../src",
    "nativescript-theme-core": "~1.0.4",
    "reflect-metadata": "~0.1.8",
    "rxjs": "~5.5.2",
    "tns-core-modules": "~4.0.0",
    "zone.js": "~0.8.2"
  },

#6

Here is my logs. I re-created platform but still didn’t succeed.

Preparing project...
Executing before-prepareJSApp hook from E:\Idrees\Works\HttpFormData\nativescript-http-formdata\demo-ng\hooks\before-prepareJSApp\nativescript-dev-webpack.js
Successfully prepared plugin nativescript-angular for android.
Successfully prepared plugin nativescript-http-formdata for android.
Successfully prepared plugin nativescript-theme-core for android.
Successfully prepared plugin tns-core-modules for android.
Successfully prepared plugin nativescript-intl for android.
Successfully prepared plugin tns-core-modules-widgets for android.
Built aar for nativescript-http-formdata
Project successfully prepared (Android)
Executing after-prepare hook from E:\Idrees\Works\HttpFormData\nativescript-http-formdata\demo-ng\hooks\after-prepare\nativescript-dev-webpack.js
Building project...
Gradle build...
         + applying user-defined configuration from E:\Idrees\Works\HttpFormData\nativescript-http-formdata\demo-ng\app\App_Resources\Android\app.gradle
         Applying settings from E:\Idrees\Works\HttpFormData\nativescript-http-formdata\demo-ng\app\App_Resources\Android\settings.json
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
Configuration 'debugCompile' in project ':app' is deprecated. Use 'debugImplementation' instead.
         + adding nativescript runtime package dependency: nativescript-optimized
         + adding aar plugin dependency: E:\Idrees\Works\HttpFormData\nativescript-http-formdata\demo-ng\node_modules\nativescript-http-formdata\platforms\android\nativescript_http_formdata.aar
         + adding aar plugin dependency: E:\Idrees\Works\HttpFormData\nativescript-http-formdata\demo-ng\node_modules\tns-core-modules-widgets\platforms\android\widgets-release.aar
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Project successfully built.
Installing...

I have added it in function stil getting undefined

    greet() {
        console.log(com.squareup);
        // const okhttp = com.squareup.okhttp3.okhttp;
        console.log('--------------------constructor--------------');
        // console.log(okhttp);
    }

#7

Can you just try to log typeof okhttp3?


#8

I did and getting undefined…


#9

I don’t see the dependency entry of your plugin in your demo project’s package json you posted above. So your plugin is never added neither the gradle dependencies, add your plugin there.


#10

It is there with this line:
"nativescript-http-formdata": "../src",


#11

Sorry my bad, I somehow missed it.


#12

Thanks man! you are really awesome guy. I just console the following and it worked:

    greet() {
        console.log(typeof com.squareup);
        console.log(typeof com.squareup.okhttp);
        console.dir(com.squareup.okhttp);
        console.log('--------------------constructor--------------');
    }

Do you have any idea why it isn’t available as full namespace as i.e com.squareup.okhttp3.okhttp ?


#13

Glad it is working for you. As I mentioned, it’s not advisable to assign name space to variable at top of your file, I guess it may not be available at that time. If you check the framework’s source code, you will find they always write a init method to do this kind of operations.