Check android version java

Requirements check failed for JDK 8 (‘1.8.*’)! Detected version: null #1040

Comments

maniveltvl commented Aug 7, 2020 •

Issue Type

Requirements check failed for JDK 8 (‘1.8.*’)! Detected version: null

  • Bug Report
  • Feature Request
  • Support Question

I want to upgrade my Android SDK version API Level 28 to 29. So I have added Android platform version 9.0.0. Then I try to build the APK but I am getting the below error.
‘Requirements check failed for JDK 8 (‘1.8.*’)! Detected version: null
Check your ANDROID_SDK_ROOT / JAVA_HOME / PATH environment variables.’

Information

  1. cordova create hello com.example.hello HelloWorld
  2. cd hello
  3. cordova platform add android@9.0.0
  4. cordova build android

Command or Code

I found that check_req.js files line no 223 returns output as null.

Environment, Platform, Device

Version information

What are relevant versions you are using?
For example:
Cordova: 8.1.0

The text was updated successfully, but these errors were encountered:

breautek commented Aug 7, 2020

This suggests you don’t have a java JDK installed. If you do, you should ensure the JAVA_HOME environment variable is pointing to the proper location.

paulodetarsofm commented Aug 9, 2020

I’m facing the same problem. I have everything configured, the weird is I started to get this issue only after restart my computer, during the installation of Android Studio/SDK and creating the Cordova app for the first time, worked fine. I’m looking for a solution, no success. I reinstalled everything: Android Studio, JDK, Cordova, but nothing.

I have all these environment variables defined, but the Java version can’t be detected by Cordova. If I run java -version, I get:

java version «1.8.0_261»
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)

So, I have the Java installed! What is happening? What is the problem?

breautek commented Aug 9, 2020

For linux/mac try running:

For windows i think it’s:

But I’m not familiar with windows shell programming, so I may be recalling incorrectly.

If the shell prints out empty string/line, then you should set the JAVA_HOME environment variable.

paulodetarsofm commented Aug 9, 2020 •

I’m using Windows 10. The variables are defined correctly:

System Variables:

Path variable:

The error:

maniveltvl commented Aug 10, 2020 •

I have installed JDK and i have set JAVA_HOME variable in environment variable but still it is not working. Then I have found that some issue in check_req.js.

I have changed code from
return execa(‘javac’, [«-version»], < all: true >)
to
return execa(‘javac -version’, [], < all: true >) in line no 223

After changed to the above code able to get java version and working fine.

Can you please change to the above working code in check_req.js and commit.

breautek commented Aug 10, 2020 •

Are you sure you’re up to date?

Try using cordova@10 , the current latest of the cli, then reinstall the android platform:

maniveltvl commented Aug 11, 2020 •

Please find my Cordova version below
cordova -v 8.1.0

«npm info cordova

cordova@10.0.0 | Apache-2.0 | deps: 13 | versions: 1314
Cordova command line interface tool
https://github.com/apache/cordova-cli#readme

keywords: cordova, client, cli

dist
.tarball: https://registry.npmjs.org/cordova/-/cordova-10.0.0.tgz
.shasum: 8563934f5207896efb7ea517f43065a3d0a4eaf1
.integrity: sha512-00wMcj3X9ILhKtvRG2iEwO2qly4B+vgXFhH4WhVepWg2UVbD1opl1q9jSZ+j2AaI/vsBWW8e6M2M5FAHasnuWw==
.unpackedSize: 145.4 kB

dependencies:
configstore: ^5.0.1 editor: ^1.0.0 loud-rejection: ^2.2.0 update-notifier: ^4.1.0
cordova-common: ^4.0.2 execa: ^4.0.3 nopt: ^4.0.3
cordova-create: ^3.0.0 fs-extra: ^9.0.1 semver: ^7.3.2
cordova-lib: ^10.0.0 insight: ^0.10.3 systeminformation: ^4.26.10

dist-tags:
3.6.0-0.2.8: 3.6.0-0.2.8 nightly: 10.0.1-nightly.2020.8.11.4e1cada0
latest: 10.0.0 rc: 5.3.3

published a week ago by erisu erisu.dev@outlook.jp«

But I want to install android@9.0.0 to get Android API level 29. When I install android@9.0.0, i am getting the above error. So as of now i am changing the below code to work

Читайте также:  Куноичи трейнер последняя версия код патрона андроид

return execa(‘javac -version’, [], < all: true >)

breautek commented Aug 11, 2020

This should only work if executing in shell mode. It doesn’t follow the documentation of execa

In general we refrain from using shell modes because it’s slower, not cross-platform, and easy to introduce security vulnerabilities if care is not taken.

Additionally I haven’t been able to reproduce this using the latest versions of the software.

The latest versions of cordova to date are:
cordova@10
cordova-android@9

cordova 8.1.0 is an old and no longer maintained version. If you can reproduce your issue on cordova 10 / cordova-android@9, then do let me know.

MatHackDev commented Aug 11, 2020

I had the same problem on my Mac; after analyzing my package-lock.json; I had a version of execa:1.0.0 ! I don’t know where it comes from.

I removed my package-lock.json and the node_modules, then I did «npm i -D execa». Which will now be in 4.0.3.

That solved my problem.

erisu commented Aug 12, 2020 •

@MatHackDev I think all you needed to do was remove the package-lock.json & node_modules directory and run npm i to re-checkout the new package. I don’t think you need to run npm i -D execa because this would install it at the project level and most likely not be used by cordova-android build scripts.

I also usually remove the platforms and plugins directory when upgrade and run cordova prepare to rebuild them.

MatHackDev commented Aug 12, 2020

Thanks for answer @erisu but «remove the package-lock.json & node_modules directory and run npm i» it’s not working for me. But i will test «remove platforms and run cordova prepare.»

Maybe it’s related to another package that creates a conflict.

ajberasategui commented Aug 31, 2020 •

I experienced the same issue. I’m using CLI v9. Even if it does not make sense to me at all, deleting package-lock and node_modules and reinstalling did the trick. 😕

EDIT: I found the reason. Doing the «force npm modules upgrade» seems to force the update of execa dependency. The version that was being used was returning undefined when Cordova executed javac -version command to obtain the current JDK version.

orkuncaylar commented Sep 28, 2020

I had the same problem on my Mac; after analyzing my package-lock.json; I had a version of execa:1.0.0 ! I don’t know where it comes from.

I removed my package-lock.json and the node_modules, then I did «npm i -D execa». Which will now be in 4.0.3.

That solved my problem.
thanks.

breautek commented Sep 28, 2020

I think it’s time to close this issue. The problem seems to be in the execa package, which is a sub-dependency and the problem is already fixed, but depending on when you installed Cordova, you may have an old version of execa causing this problem. As many others stated, removing the node_modules and package-lock.json file to force update all modules on npm install .

thibaudsowa commented Oct 6, 2020 •

Hi, here is my contribution.

I have exactly the same problem, after hours of research I think I have more information about this.
Actually, I discovered that the problem came from execa installed by other dependencies, there is a conflict between different versions.

Here is my dependency tree for execa :

When there is different version of a library, npm create sub node_modules directory:

The version of execa in /node_modules/execa is 0.10.0
The version of execa in /node_modules/cordova-android/node_modules/execa is 4.0.3

I don’t know why but when I execute cordova build android the execa used is the first one (the bad one. )
I figured out by editing both lib file with some log.

Of course I did the classic «delete node_modules + package-lock.json » with no success.
The only workaround for me is to add execa in my own dependency, but I’m not happy with that because I don’t use it and I’m afraid to delete it some day when I will do some dependency cleaning. )

Thanks for your help!

I’m on windows 10, cordova 10.0.0, cordova-android 9.0.0, npm 6.14.5, node 10.18.1

breautek commented Oct 6, 2020

I don’t know why but when I execute cordova build android the execa used is the first one (the bad one. )
I figured out by editing both lib file with some log.

I think you should open a new issue and fill out the issue form documenting how cordova-android appears to be using the wrong execa . I also have a theory that you can add.

Читайте также:  Андроид том райдер лара крофт

That theory is because our executing code that depends on execa doesn’t actually live inside node_modules/cordova-android and is instead copied into your project folder ( platforms/android/ ). So this causes the NPM/Node module resolution to break. Consequentially, it also causes the problem to be fixed if you add it as your own dependency.

Источник

Using OkHttp

OkHttp is a third-party library developed by Square for sending and receive HTTP-based network requests. It is built on top of the Okio library, which tries to be more efficient about reading and writing data than the standard Java I/O libraries by creating a shared memory pool. It is also the underlying library for Retrofit library that provides type safety for consuming REST-based APIs.

The OkHttp library actually provides an implementation of the HttpUrlConnection interface, which Android 4.4 and later versions now use. Therefore, when using the manual approach described in this section of the guide, the underlying HttpUrlConnection class may be leveraging code from the OkHttp library. However, there is a separate API provided by OkHttp that makes it easier to send and receive network requests, which is described in this guide.

In addition, OkHttp v2.4 also provides a more updated way of managing URLs internally. Instead of the java.net.URL , java.net.URI , or android.net.Uri classes, it provides a new HttpUrl class that makes it easier to get an HTTP port, parse URLs, and canonicalizing URL strings.

Makes sure to enable the use of the Internet permission in your AndroidManifest.xml file:

Simply add this line to your app/build.gradle file:

Note: If you are upgrading from an older version of OkHttp, your imports will also need to be changed from import com.squareup.okhttp.XXXX to import okhttp3.XXXX .

Note: If you are intending to use Picasso with OkHttp3, you can either upgrade Picasso to the latest snapshot version or use this custom downloader. This change is necessary until the next release of Picasso as described here.

You will then wrap the OkHttpClient with this OkHttp3Downloader . Note: as of OkHttp3 , it is recommended you declare this object as a singleton because changes in OkHttp3 no longer require a global connection pool. See this changelog for more details.

First, we must instantiate an OkHttpClient and create a Request object.

If there are any query parameters that need to be added, the HttpUrl class provided by OkHttp can be leveraged to construct the URL:

If there are any authenticated query parameters, headers can be added to the request too:

We can create a Call object and dispatch the network request synchronously:

Because Android disallows network calls on the main thread, you can only make synchronous calls if you do so on a separate thread or a background service. You can use also use AsyncTask for lightweight network calls.

We can also make asynchronous network calls too by creating a Call object, using the enqueue() method, and passing an anonymous Callback object that implements both onFailure() and onResponse() .

OkHttp normally creates a new worker thread to dispatch the network request and uses the same thread to handle the response. It is built primarily as a Java library so it does not handle the Android framework limitations that only permit views to be updated on the main UI thread.

For this reason, if you try to access or update views from outside the main thread in the Callback , you will probably receive an exception: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views . Read more about the relationship between the main thread and views here.

If you need to update any views from within a response, you will need to use runOnUiThread() or post the result back on the main thread:

See this guide for more context. Alternatively, you could use an extension of Callback such as MainThreadCallback which wraps up this sort of behavior neatly and places you within the main thread from the response callback by default.

Читайте также:  Ватсап не показывает уведомления андроид

Assuming the request is not canceled and there are no connectivity issues, the onResponse() method will be fired. It passes a Response object that can be used to check the status code, the response body, and any headers that were returned. Calling isSuccessful() for instance if the code returned a status code of 2XX (i.e. 200, 201, etc.)

The header responses are also provided as a list:

The headers can also be access directly using response.header() :

We can also get the response data by calling response.body() and then calling string() to read the entire payload. Note that response.body() can only be run once and should be done on a background thread.

Suppose we make a call to the GitHub API, which returns JSON-based data:

We can also decode the data by converting it to a JSONObject or JSONArray , depending on the response data:

Note that the string() method on the response body will load the entire data into memory. To make more efficient use of memory, it is recommended that the response is processed as a stream by using charStream() instead. This approach, however, requires using the Gson library. See this guide for setup instructions.

To use the Gson library, we first must declare a class that maps directly to the JSON response:

We can then use the Gson parser to convert the data directly to a Java model:

OkHttp has a mechanism to modify outbound requests using interceptors. A common use case is the OAuth protocol, which requires requests to be signed using a private key. The OkHttp signpost library works with the SignPost library to use an interceptor to sign each request. This way, the caller does not need to remember to sign each request:

We can setup network caching by passing in a cache when building the OkHttpClient :

We can control whether to retrieve a cached response by setting the cacheControl property on the request. For instance, if we wish to only retrieve the request if data is cached, we could construct the Request object as follows:

We can also force a network response by using noCache() for the request:

We can also specify a maximum staleness age for the cached response:

To retrieve the cached response, we can simply call cacheResponse() on the Response object:

OkHttp can be hard to troubleshoot when trying to step through the various layers of abstraction in the libraries. You can add the HttpLogInterceptor that can be added when using the OkHttp3 library, which will print HTTP requests/responses through LogCat. You can also leverage Facebook’s Stetho project to use Chrome to inspect all network traffic.

To use HttpLogInterceptor , add this dependency to your Gradle configuration:

You will need to add a network interceptor for HttpLogInterceptor. See this doc for the different options that can be used.

Use Facebook’s Stetho plugin to monitor network calls with Chrome:

Add this line to your Gradle configuration:

When instantiating OkHttp , make sure to pass in the StethoInterceptor .

Finally, make sure to initialize Stetho in your Application:

OkHttp v3.5 now includes support for bidirectional web sockets. The URL that should be used should be prefixed with ws:// or wss:// for the secure version. Although the connection ports are the same as HTTP (port 80 and port 443), the server still needs to be configured to support WebSockets since they are a completely different protocol.

To send a message, simply use the send() function:

Note that OkHttp handles all the work on a separate thread, so you don’t have to worry about making Websocket calls on the main thread.

If you need to close the connection properly, make sure to use a status code of 1000. See this link for the different status codes.

If you are seeing SSL handshake terminated errors and using Android 4.0 devices, you will need to enable TLS v1.2 explicitly. Android has supported TLS 1.2 since API 16 (Android 4.1). You also should ensure you are using the latest OpenSSL by using the ProviderInstaller as described here.

This should be in the first line of your Application (or at least before OkHttp initializes):

Check out Square’s official recipe guide for other examples of using OkHttp.

Источник

Оцените статью