如何在NativeScript中访问Google Play服务 [英] How to access Google Play Services in NativeScript

查看:127
本文介绍了如何在NativeScript中访问Google Play服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在从NativeScript开始。
我想将某些Google Play服务包含到android项目中。
我将相关的.aar文件放入我的应用程序项目的node_modules /文件夹中并构建它。
$ b


:config阶段:copyAarDependencies

我可以看到它们已成功添加到项目中。

 :config阶段:addAarDependencies 
+添加依赖项:C:\Users\pstueven\NativeScript\sample-Groceries\platforms\android\libs \ aar\play-services-9.0.0.aar
+添加依赖项:C:\ Users \pstueven\NativeScript\sample-Groceries\platforms\android\libs\aar \play-services-base-9.0.0.aar
+添加依赖关系:C:\Users\pstueven\NativeScript\sample-Groceries\platforms\android\libs\aar\\ \\ Play-services-basement-9.0.0.aar


但是我似乎无法找到教程告诉我如何从NativeScript访问它们的类和模块。
我试过这样的东西:

  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();

但它们都不起作用。它们如何包含在NativeScript的上下文中?或者他们不是?
有没有人有提示如何实现这一点?

解决方案

在NativeScript中为Angular开发时,如果库/插件可以在 Jcenter / Maven Repository(Android插件的存储库,非常类似于所有JavaScript的npmjs)或你的本地Android SDK安装,那么你可以在你的 App_Resources / Android / app.gradle 文件中引用该依赖项。



示例:

 依赖关系{
compile'c​​om.google.android.gms:play-services:9.0.0 '
}

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

构建时间这些依赖关系将被下载并正确添加到项目中,以便您可以在应用程序中使用它们。

下一步,您可能需要阅读您要使用的库的用法。这很可能是API Docs站点或Android / Java代码示例。在NativeScript中,您可以像访问本机应用程序那样访问Native API。这意味着翻译您在样本中找到的代码几乎毫不费力。您可以在官方文档站点找到足够的信息来帮助您入门。



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



<说了这么多 - 回到你的代码示例 - 你试图实例化的类是抽象的,这意味着你只能创建该类的非抽象非静态后裔的实例( https://developers.google.com/android/reference/com/google / android / gms / common / api / GoogleApiClient )。



如果您要在NativeScript中编写该代码,则为了举例,假设我们可以创建实例s,我们会这样写 - >
var apiClient = new com.google.android.gms.common.api.GoogleApiClient();



祝你好运!


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?

解决方案

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!

这篇关于如何在NativeScript中访问Google Play服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆