How to access Google Play Services in NativeScript

2020-06-06 01:32发布

问题:

I am currently starting with NativeScript. I am trying to include certain Google Play Services to the android project. I put the relevant .aar files into the node_modules/ folder of my app project and built it. I can see they are successfully added to the project.

:config phase: copyAarDependencies

:config phase:  addAarDependencies
        +adding dependency: C:\Users\pstueven\NativeScript\sample-Groceries\platforms\android\libs\aar\play-services-9.0.0.aar
        +adding dependency: C:\Users\pstueven\NativeScript\sample-Groceries\platforms\android\libs\aar\play-services-base-9.0.0.aar
        +adding dependency: C:\Users\pstueven\NativeScript\sample-Groceries\platforms\android\libs\aar\play-services-basement-9.0.0.aar

But I can't seem to find a tutorial telling me how to access their classes and modules from NativeScript. I tried things like:

var test = new main.java.com.google.android.gms.common.api.GoogleApiClient();

var test = new com.google.android.gms.common.api.GoogleApiClient();

var test = new android.gms.common.api.GoogleApiClient();

But none of them works. How are they included into the context of NativeScript? Or aren't they? Does anyone have a hint how to achieve this?

回答1:

In NativeScript when developing for Angular if a library/plugin is available in Jcenter/Maven Repository (Repositories for Android plugins, much like npmjs for all things JavaScript), or in your local Android SDK installation, then you can reference that dependency in your App_Resources/Android/app.gradle file.

Example:

dependencies {
    compile 'com.google.android.gms:play-services:9.0.0'
}

android {  
  defaultConfig {  
    generatedDensities = []
    applicationId = "org.nativescript.myApp"  
  }  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
} 

Build-time those dependencies will be downloaded and properly added to the project, so that you may consume them in your app.

As the next step you may want to read up on the usage of the library that you want to use. That will most likely be API Docs site, or Android/Java code samples. In NativeScript you are able to access Native API as you would if you were writing a native application. That means that "translating" the code you find in samples should be almost effortless. You can find sufficient information in the official docs site to get you started.

https://docs.nativescript.org/runtimes/android/marshalling/java-to-js https://docs.nativescript.org/runtimes/android/metadata/accessing-packages.html

Having said all that - back to your code sample - the class that you are attempting to instantiate is abstract, meaning you can only create instances of a non-abstract non-static descendant of that class (https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient).

If you were to write that in NativeScript however, for the sake of the example, let's assume we CAN create instances, we would write it like this -> var apiClient = new com.google.android.gms.common.api.GoogleApiClient();

Good luck!