HTTP request made on Android 4.4 throws with
System.err: javax.net.ssl.SSLException: Connection closed by peer
System.err: at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
System.err: at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:405)
System.err: at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
System.err: at com.android.okhttp.Connection.connect(Connection.java:107)
System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
System.err: at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:161)
System.err: at org.nativescript.widgets.Async$Http$HttpRequestTask.doInBackground(Async.java:543)
System.err: at org.nativescript.widgets.Async$Http$1.run(Async.java:482)
System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
System.err: at org.nativescript.widgets.Async$PriorityThreadFactory$1.run(Async.java:52)
System.err: at java.lang.Thread.run(Thread.java:841)
JS: ERROR Error: Uncaught (in promise): Response with status: 200 for URL: null
The same works as expected on higher API levels (tested on API21, API 23 , API25).
Possible solution discussed here
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Reproducible with any HTTPS request only on Android API 19 devices with an older version of the gms security provider. As an easy workaround, we can force the device to update the security provider the following way:
app.ts
import { ad } from "tns-core-modules/utils/utils";
const context = ad.getApplicationContext();
androidApp.on(AndroidApplication.activityCreatedEvent, function (args: AndroidActivityBundleEventData) {
// Needed updated Provider for corner cases with HTTP request using TSL on Android API19
com.google.android.gms.security.ProviderInstaller.installIfNeededAsync(context, new com.google.android.gms.security.ProviderInstaller.ProviderInstallListener({
onProviderInstalled: () => {
console.log("onProviderInstalled");
},
onProviderInstallFailed: (errorCode, intent) => {
console.log("onProviderInstalledFailed");
console.log("errorCode: " + errorCode);
}
}))
});
Hi @NickIliev, I am having this problem on my nativescript app when trying to do a http.post request to my https server, my android device version 4.4.2, API 19, however, I have zero knowledge about java and android development, could you please show me how to fix this? To which file should I add these code? Thanks in advance. Choo.
@lanette46 in practice you can implement this on every page in your application - you just need to call the activityCreatedEvent.
As the code would be most likely expected to be one of the first things to happen in your application I would suggest adding it in your app/app.ts if using TypeScript project (or app..js if using plain JavaScript) or in main.ts if using Angular (as done here)
Hi @NickIliev, I'm also having the same error as https://github.com/NativeScript/NativeScript/issues/5605 recently. I've followed the example you gave to add the code into main.ts (as I am using Angular), and I got this error:
Searching for devices...
Executing before-prepare hook from /home/john/pcapp/nativescript/hooks/before-prepare/nativescript-dev-typescript.js
Found peer TypeScript 2.6.2
app/main.ts(15,27): error TS2304: Cannot find name 'android'.
app/main.ts(16,9): error TS2304: Cannot find name 'android'.
Unable to apply changes on device: bb729043. Error is: TypeScript compiler failed with exit code 1.
Executing after-watch hook from /home/john/pcapp/nativescript/hooks/after-watch/nativescript-dev-typescript.js
Here is my code:
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app.module";
import { android as androidApp, AndroidApplication, AndroidActivityBundleEventData } from "application";
import { ad } from "utils/utils";
declare let com: any;
if (androidApp) {
const context = ad.getApplicationContext();
androidApp.on(AndroidApplication.activityCreatedEvent, function (args: AndroidActivityBundleEventData) {
let builder = new android.os.StrictMode.VmPolicy.Builder(); //red squiggly line appears under android here
android.os.StrictMode.setVmPolicy(builder.build()); //red squiggly line appears under android here
// Needed for corner cases with HTTP request using TSL on Android API19
com.google.android.gms.security.ProviderInstaller.installIfNeededAsync(
context,
new com.google.android.gms.security.ProviderInstaller.ProviderInstallListener({
onProviderInstalled: () => {
console.log("Provider Installed!");
},
onProviderInstallFailed: (errorCode, intent) => {
console.log("Error: " + errorCode);
}
}));
});
}
platformNativeScriptDynamic().bootstrapModule(AppModule);
It seems that I did not import android. May I know how to import it? Please advise and thank you.
Hi @NickIliev, I found your solution to my above error on https://stackoverflow.com/questions/37523071/how-can-i-access-native-api-in-nativescript-when-i-use-typescript , thanks again. However, I am getting this error, with the same code as above:
System.err: java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.nativescript/com.tns.NativeScriptActivity}: com.tns.NativeScriptException:
System.err: Calling js method onCreate failed
System.err: TypeError: Cannot read property 'android' of undefined
System.err: File: "file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/ui/frame/frame.js, line: 610, column: 18
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.nativescript/files/app/main.js', line: 15, column: 19
System.err: Frame: function:'Observable.notify', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 103, column: 23
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/application/application.js', line: 150, column: 20
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/application/application.js', line: 169, column: 13
System.err: Frame: function:'ActivityCallbacksImplementation.onCreate', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 610, column: 19
System.err: Frame: function:'NativeScriptActivity.onCreate', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 20, column: 25
System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
System.err: at android.app.ActivityThread.access$800(ActivityThread.java:138)
System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
System.err: at android.os.Looper.loop(Looper.java:136)
System.err: at android.app.ActivityThread.main(ActivityThread.java:5050)
System.err: at java.lang.reflect.Method.invokeNative(Native Method)
System.err: at java.lang.reflect.Method.invoke(Method.java:515)
System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
System.err: at dalvik.system.NativeStart.main(Native Method)
System.err: Caused by: com.tns.NativeScriptException:
System.err: Calling js method onCreate failed
System.err: TypeError: Cannot read property 'android' of undefined
System.err: File: "file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/ui/frame/frame.js, line: 610, column: 18
System.err: StackTrace:
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.nativescript/files/app/main.js', line: 15, column: 19
System.err: Frame: function:'Observable.notify', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 103, column: 23
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/application/application.js', line: 150, column: 20
System.err: Frame: function:'', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/application/application.js', line: 169, column: 13
System.err: Frame: function:'ActivityCallbacksImplementation.onCreate', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 610, column: 19
System.err: Frame: function:'NativeScriptActivity.onCreate', file:'file:///data/data/org.nativescript.nativescript/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 20, column: 25
System.err: at com.tns.Runtime.callJSMethodNative(Native Method)
System.err: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
System.err: at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:957)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:941)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:933)
System.err: at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:13)
System.err: at android.app.Activity.performCreate(Activity.java:5239)
System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
System.err: at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
System.err: ... 11 more
ActivityManager: Process org.nativescript.nativescript (pid 11202) has died.
Am I missing something? Thanks.
@lanette46 the error indicates that you have an undefined object from which you are trying to access android property. Looking at the code you could see that the only place where android is property of an object is here
com.google.android.gms.security.ProviderInstaller
Now we can conclude that the library com.google.android.gms is missing as a dependency from the project. So we could add the native dependency directly in your app.gradle project file as done here
add to _app/App-Resources/Android/app.gradle_
dependencies {
compile "com.google.android.gms:play-services-base:12.0.1"
}
Finally remove the platforms folder (better with tns platform remove android) and rebuild again your application and you should be good to go. A basic test project demostraing the above can be found here
Hello @NickIliev, I have added the code you provided and it solved my previous error and I am able see my login screen again (which I couldn't before that due to the error), then this bring me to more errors (please bear with me), currently there are two errors in the log, the first one appeared when the app was starting and the second one appeared when I pressed the login button, both of them look like the same error and I could not log myself in. Please see the comment in the error log and kindly advise. Thank you.
ActivityManager: Start proc org.nativescript.nativescript for activity org.nativescript.nativescript/com.tns.NativeScriptActivity: pid=1449 uid=10261 gids={50261, 1028, 1015, 3003}
JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
JS: ANGULAR BOOTSTRAP DONE. 14459
System.err: javax.net.ssl.SSLHandshakeException: No enabled protocols; SSLv3 is no longer supported and was filtered from the list
System.err: at com.google.android.gms.org.conscrypt.SslWrapper.initialize(:com.google.android.gms@[email protected] (000306-189987672):7)
System.err: at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@[email protected] (000306-189987672):6)
System.err: at com.google.android.gms.org.conscrypt.KitKatPlatformOpenSSLSocketImplAdapter.startHandshake(:com.google.android.gms@[email protected] (000306-189987672))
System.err: at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
System.err: at com.android.okhttp.Connection.connect(Connection.java:107)
System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
System.err: at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:161)
System.err: at org.nativescript.widgets.Async$Http$HttpRequestTask.doInBackground(Async.java:543)
System.err: at org.nativescript.widgets.Async$Http$1.run(Async.java:482)
System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
System.err: at org.nativescript.widgets.Async$PriorityThreadFactory$1.run(Async.java:52)
System.err: at java.lang.Thread.run(Thread.java:841)
JS: Provider Installed! <<< At this line, I can see and interact with my login screen, the error on the next line showed up when I press the login button, which send a http.post request to my node server's 'users/login' endpoint
System.err: javax.net.ssl.SSLHandshakeException: No enabled protocols; SSLv3 is no longer supported and was filtered from the list
System.err: at com.google.android.gms.org.conscrypt.SslWrapper.initialize(:com.google.android.gms@[email protected] (000306-189987672):7)
System.err: at com.google.android.gms.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(:com.google.android.gms@[email protected] (000306-189987672):6)
System.err: at com.google.android.gms.org.conscrypt.KitKatPlatformOpenSSLSocketImplAdapter.startHandshake(:com.google.android.gms@[email protected] (000306-189987672))
System.err: at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
System.err: at com.android.okhttp.Connection.connect(Connection.java:107)
System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
System.err: at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
System.err: at org.nativescript.widgets.Async$Http$RequestOptions.writeContent(Async.java:313)
System.err: at org.nativescript.widgets.Async$Http$HttpRequestTask.doInBackground(Async.java:536)
System.err: at org.nativescript.widgets.Async$Http$1.run(Async.java:482)
System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
System.err: at org.nativescript.widgets.Async$PriorityThreadFactory$1.run(Async.java:52)
System.err: at java.lang.Thread.run(Thread.java:841)
@lanette46 I guess it is related to your server not supporting TSL (look here)
What is the HTTP client you are using in your application - is it a specific HTTP plugin?
Try enabling the SSLv3 protocol (or restricting its usage) as shown here - of course, you should marshal the code from Java to JavaScript.
For example, something like
const sslContext = javax.net.ssl.SSLContext.getInstance("TSLv1.2");
sslContext.init(null, null, null);
const engine = sslContext.createSSLEngine();
Hi @NickIliev,
What is the HTTP client you are using in your application - is it a specific HTTP plugin?
I'm using this: import { HttpClient } from '@angular/common/http'; in my auth.service.ts
The links you provided above is difficult for me to understand as I have no java/android knowledge and I'm not sure if I do this correctly, this is my current error, am I missing anything?
Found peer TypeScript 2.6.2
ActivityManager: Start proc org.nativescript.nativescript for activity org.nativescript.nativescript/com.tns.NativeScriptActivity: pid=7518 uid=10264 gids={50264, 1028, 1015, 3003}
JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
JS: ANGULAR BOOTSTRAP DONE. 6619
JS: Provider Installed!
System.err: com.tns.NativeScriptException:
System.err: Calling js method onProviderInstalled failed
System.err: Error: java.security.NoSuchAlgorithmException: SSLContext TSLv1.2 implementation not found
System.err: org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
System.err: org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
System.err: javax.net.ssl.SSLContext.getInstance(SSLContext.java:98)
System.err: com.tns.Runtime.callJSMethodNative(Native Method)
System.err: com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
System.err: com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
System.err: com.tns.Runtime.callJSMethod(Runtime.java:957)
System.err: com.tns.Runtime.callJSMethod(Runtime.java:941)
System.err: com.tns.Runtime.callJSMethod(Runtime.java:933)
System.err: com.tns.gen.com.google.android.gms.security.ProviderInstaller_ProviderInstallListener.onProviderInstalled(ProviderInstaller_ProviderInstallListener.java:10)
System.err: com.google.android.gms.security.zza.onPostExecute(Unknown Source)
System.err: android.os.AsyncTask.finish(AsyncTask.java:632)
System.err: android.os.AsyncTask.access$600(AsyncTask.java:177)
System.err: android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
System.err: android.os.Handler.dispatchMessage(Handler.java:102)
System.err: android.os.Looper.loop(Looper.java:136)
System.err: android.app.ActivityThread.main(ActivityThread.java:5050)
System.err: java.lang.reflect.Method.invokeNative(Native Method)
System.err: java.lang.reflect.Method.invoke(Method.java:515)
System.err: com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
System.err: com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
System.err: dalvik.system.NativeStart.main(Native Method)
System.err: File: "file:///data/data/org.nativescript.nativescript/files/app/main.js, line: 17, column: 58
System.err: StackTrace:
System.err: Frame: function:'onProviderInstalled', file:'file:///data/data/org.nativescript.nativescript/files/app/main.js', line: 17, column: 59
System.err: at com.tns.Runtime.callJSMethodNative(Native Method)
System.err: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
System.err: at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:957)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:941)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:933)
System.err: at com.tns.gen.com.google.android.gms.security.ProviderInstaller_ProviderInstallListener.onProviderInstalled(ProviderInstaller_ProviderInstallListener.java:10)
System.err: at com.google.android.gms.security.zza.onPostExecute(Unknown Source)
System.err: at android.os.AsyncTask.finish(AsyncTask.java:632)
System.err: at android.os.AsyncTask.access$600(AsyncTask.java:177)
System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
System.err: at android.os.Looper.loop(Looper.java:136)
System.err: at android.app.ActivityThread.main(ActivityThread.java:5050)
System.err: at java.lang.reflect.Method.invokeNative(Native Method)
System.err: at java.lang.reflect.Method.invoke(Method.java:515)
System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:805)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:621)
System.err: at dalvik.system.NativeStart.main(Native Method)
System.err: Caused by: java.security.NoSuchAlgorithmException: SSLContext TSLv1.2 implementation not found
System.err: at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
System.err: at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
System.err: at javax.net.ssl.SSLContext.getInstance(SSLContext.java:98)
System.err: ... 19 more
ActivityManager: Process org.nativescript.nativescript (pid 7518) has died.
16:59:26 - Compilation complete. Watching for file changes.
And here is my code:
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app.module";
import { android as androidApp, AndroidApplication, AndroidActivityBundleEventData } from "application";
import { ad } from "utils/utils";
declare let com: any;
if (androidApp) {
const context = ad.getApplicationContext();
androidApp.on(AndroidApplication.activityCreatedEvent, function (args: AndroidActivityBundleEventData) {
let builder = new android.os.StrictMode.VmPolicy.Builder();
android.os.StrictMode.setVmPolicy(builder.build());
// Needed for corner cases with HTTP request using TSL on Android API19
com.google.android.gms.security.ProviderInstaller.installIfNeededAsync(
context,
new com.google.android.gms.security.ProviderInstaller.ProviderInstallListener({
onProviderInstalled: () => {
console.log("Provider Installed!");
const sslContext = javax.net.ssl.SSLContext.getInstance("TSLv1.2");
sslContext.init(null, null, null);
const engine = sslContext.createSSLEngine();
},
onProviderInstallFailed: (errorCode, intent) => {
console.log("Error: " + errorCode);
}
}));
});
}
Found the solution for previous error at https://stackoverflow.com/questions/4457503/android-sslcontext-ssl-implementation-not-found,
Fix: const sslContext = javax.net.ssl.SSLContext.getInstance("TLSv1.2");
But still I'm getting: System.err: javax.net.ssl.SSLHandshakeException: No enabled protocols; SSLv3 is no longer supported and was filtered from the list. Same as the previous error. I don't understand why.
Hello @NickIliev, do you have time to look at this and comment, I'm still stuck here, please help. Thanks.
@lanette46 the thing is that I am not able to test your issue as I do not have a server that requires SSL v3 connection. In my case, the issue was resolved by simply including this code and updating the provider resolved the issue.
Tip: Try this solution
Hi @NickIliev, just a quick question, how do you know which protocol my server support, is there a way to check it? Thanks.
@lanette46 I don't :) I am speculating based on SSLv3 is no longer supported and was filtered from the list
@NickIliev
I've tried your code :
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app/app.module";
import {
android as androidApp,
AndroidApplication,
AndroidActivityBundleEventData,
} from "tns-core-modules/application";
import { ad } from "tns-core-modules/utils/utils";
declare let com: any;
if (androidApp) {
const context = ad.getApplicationContext();
androidApp.on(AndroidApplication.activityCreatedEvent, (
args: AndroidActivityBundleEventData
) => {
const builder = new android.os.StrictMode.VmPolicy.Builder();
android.os.StrictMode.setVmPolicy(builder.build());
// Needed for corner cases with HTTP request using TSL on Android API19
com.google.android.gms.security.ProviderInstaller.installIfNeededAsync(
context,
new com.google.android.gms.security.ProviderInstaller.ProviderInstallListener(
{
onProviderInstalled: () => {
console.log("Provider Installed!");
},
onProviderInstallFailed: (errorCode, intent) => {
console.log("Error: " + errorCode);
}
}
)
);
});
}
platformNativeScriptDynamic().bootstrapModule(AppModule);
But the compiler has an issue with
const builder = new android.os.StrictMode.VmPolicy.Builder();
It doesn't know 'android'
Can you help me with this?
@fransyozef when you need to access the native APIs (Android or iOS) via TypeScript you will need a typings for the used namespaces, classes, properties, etc.
You can either cast to any (not recommended) or use tns-platform-declarations (recommended) which will not only resolve the TypeScript compilation errors but also provide IntelliSense for the converted APIs.
To enable the declarations do the following:
npm i tns-platform-declarations --save-dev
reference.d.ts file in the root project folder and place the following/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
e.g. like done here
Now you will have the declaration files with all the typings and will also have intelliSense for the android namespace. More details in the documentation section or here
Note: the plugin won't resolve the com namespace for which I am using manual casting to any
declare let com: any;
@NickIliev ah kudos on that plugin declarations. At least my editor doesn't give me the error, but ...
// this import should be first in order to load some required settings (like globals and reflect-metadata)
import { platformNativeScriptDynamic } from "nativescript-angular/platform";
import { AppModule } from "./app/app.module";
import {
android as androidApp,
AndroidApplication,
AndroidActivityBundleEventData,
} from "tns-core-modules/application";
import { ad } from "tns-core-modules/utils/utils";
declare let com: any;
if (androidApp) {
const context = ad.getApplicationContext();
androidApp.on(AndroidApplication.activityCreatedEvent, (
args: AndroidActivityBundleEventData
) => {
const builder = new android.os.StrictMode.VmPolicy.Builder();
android.os.StrictMode.setVmPolicy(builder.build());
// Needed for corner cases with HTTP request using TSL on Android API19
com.google.android.gms.security.ProviderInstaller.installIfNeededAsync(
context,
new com.google.android.gms.security.ProviderInstaller.ProviderInstallListener(
{
onProviderInstalled: () => {
console.log("Provider Installed!");
},
onProviderInstallFailed: (errorCode, intent) => {
console.log("Error: " + errorCode);
}
}
)
);
});
}
platformNativeScriptDynamic().bootstrapModule(AppModule);
And ofcourse the reference.d.ts file.
Still gives me an error on runtime (and even dies) when I do
tns run android --bundle
I get something like
System.err: Calling js method onCreate failed
System.err: TypeError: Cannot read property 'android' of undefined
System.err: File: "file:///data/data/nl.coreelements.weethetsnel/files/app/vendor.js, line: 106571, column: 18
and it only happens when I use the
com.google.android.gms.security.ProviderInstaller.installIfNeededAsync(
context,
new com.google.android.gms.security.ProviderInstaller.ProviderInstallListener(
{
onProviderInstalled: () => {
console.log("Provider Installed!");
},
onProviderInstallFailed: (errorCode, intent) => {
console.log("Error: " + errorCode);
}
}
)
);
block .. help?
@fransyozef the snippet looks fine and if the TypeScript transpilation is passing it should not fail runtime. However, the error log is pointing to the bundled vendor file which is hard to investigate - can you please run the project without webpack (tns run android) and post the error log so I could see what exactly is causing the issue.
One thing that I think of is that you are probably crashing at
com.google.android.gms.security.ProviderInstaller
So if you are missing com.google that would probably mean that you are missing the Google Play Services Library from your local SDK setup. Open the SDK manager go to SDK Tools and verify that you have installed the latest version of Google Play Services. Then remove the platform file and rebuild the project.
@NickIliev : thank you very much! Your solution works. Great!
Hi all
@NickIliev With your code and NS 6.2, I've got this error
ns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: TypeError: Cannot read property 'security' of undefined
System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3123)
System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3266)
System.err: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
System.err: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
System.err: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1957)
System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
System.err: at android.os.Looper.loop(Looper.java:214)
System.err: at android.app.ActivityThread.main(ActivityThread.java:7099)
System.err: at java.lang.reflect.Method.invoke(Native Method)
System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
System.err: Caused by: com.tns.NativeScriptException: Calling js method onCreate failed
System.err: TypeError: Cannot read property 'security' of undefined
System.err: at com.tns.Runtime.callJSMethodNative(Native Method)
System.err: at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1286)
System.err: at com.tns.Runtime.callJSMethodImpl(Runtime.java:1173)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1160)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1138)
System.err: at com.tns.Runtime.callJSMethod(Runtime.java:1134)
System.err: at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:19)
System.err: at android.app.Activity.performCreate(Activity.java:7327)
System.err: at android.app.Activity.performCreate(Activity.java:7318)
System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
System.err: ... 11 more
I assume it's because my SDK isn't loading properly, isn't it?
However, I have a app.gradle configured like this
android {
compileSdkVersion 29
defaultConfig {
minSdkVersion 17
targetSdkVersion 29
generatedDensities = []
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
and a before-plugins.gradle as following
android {
project.ext {
googlePlayServicesVersion = "16.+"
}
}
What did I miss?
Thx a lot !
@phenric you are most likely missng the Google Play library in your project. If none of the plugins is not explicitly installing the library then you could require it via app.gradle
app.gradle
dependencies {
compile 'com.google.android.gms:play-services-gcm:16.0.0'
}
It is even better to use the version variable googlePlayServicesVersion so that if you are using a plugin that requires the same libray it won't go in conflict (about the versioning)
@NickIliev thx for your answer.
You're right. I'm building a small project from another and I did not include the _nativescript-geolocation_ which installs the required library from Google Play.
However, concerning,
It is even better to use the version variable googlePlayServicesVersion so that if you are using a plugin that requires the same library it won't go in conflict (about the versioning)
inside before-plugins.gradle, I added android { project.ext { googlePlayServicesVersion = "16.+" } } as you seem to suggest.
Did I miss something ?
Most helpful comment
Reproducible with any HTTPS request only on Android API 19 devices with an older version of the gms security provider. As an easy workaround, we can force the device to update the security provider the following way:
app.ts