AndroidStudio 3.0: Java 8 stream not compile (Call requires API level 24) - android-studio-3.0

Java 8, and I install new Android Studio 3.0.
Add code:
List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");
myList
.stream()
.filter(s -> s.startsWith("c"))
.map(String::toUpperCase)
.sorted()
.forEach(System.out::println);
But I get compile error:
Call requires API level 24 (current min is 15) java.util.stream.Collection

In order to use Streams and other java 8 API without requiring minimum API level 24, you need to do the following:
Upgrade Android Gradle Plugin to 4.0.0 or higher
classpath 'com.android.tools.build:gradle:4.0.0
Then add this dependency to your build.gradle file:
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.6'
....
}
Finally Add this to your build.gradle file:
compileOptions {
// Flag to enable support for new language APIs
coreLibraryDesugaringEnabled true
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
Enjoy Java 8 Streams and Time APIs :)

While there is a bunch of Java 1.8 support in Android Studio 3.0 (lambdas, method references, default interface methods), not all of 1.8 is supported for all Android APIs. In particular, java.util.stream is not supported until API 24.
See the developer docs: https://developer.android.com/studio/write/java8-support.html#supported_features
A good library to get backported Java 1.8 libraries such as java.util.stream is StreamSupport: https://github.com/streamsupport/streamsupport

Related

weird import issues - Jung2

I've been trying to get Jung 2.1.1 to work successfully but whatever breaking changes were made are just not making sense.
After importing the 2.1.1 jars, I get the error:
The constructor VisualizationViewer(Network, LayoutAlgorithm, Dimension) is undefined
for the line:
VisualizationViewer vv = new
VisualizationViewer(g, layoutAlgorithm, new Dimension(900, 900));
where
Network g = NetworkBuilder.undirected().build(); // and other load steps
There are other imports that aren't working, like
import edu.uci.ics.jung.visualization.decorators.PickableNodePaintFunction;
import edu.uci.ics.jung.visualization.layout.LayoutAlgorithmTransition;
Edit: It appears the classes in the Jung 2.1.1 JAR still use the old definitions, for example
VisualizationViewer(Layout<V,E>,Dimension)
and not
VisualizationViewer(Network<N,E>,Dimension,Dimension)
This question was answered in https://github.com/jrtom/jung/issues/201
The short version is that you should not be cloning the HEAD version on the JUNG website; that's the 3.0 version in development, which is not yet ready for release, and is incompatible with 2.1.1. Use the 2.1.1 version explicitly for both the jars and the samples.

F# error array slicing when using 4.3.1 fsharp core version

I get the error when build my project with latest fharp core(4.3.1) for .Net 4.0.
Error 20 The value, constructor, namespace or type
'GetArraySlice2DFixed1' is not
defined C:\dev\dev\PrimitiveTemplatesApp\SvgPreprocess\BinaryParsers.fs 113 36 SvgPreprocess
Code:
let width, height = arr.[0,*].Length, arr.[*,0].Length
I want to use fharp 4.3.1 for using some libraries but also I need to use .Net 4.0 - can't update on .Net 4.5+ now for backward compatibility reason. Is there solution other than not use array slicing? Thanks.

Using JavaCV with Kotlin

I'm currently attempting to start a project using JavaCV in Kotlin. I'm using IntelliJ Idea as my IDE. I'm using JavaCV 1.3.2 and OpenCV 3.20.
This is my setup for the module's dependancies for OpenCV:
and for JavaCV:
I have opencv before javacv in the dependancies.
To test that OpenCV is present and valid, I wrote the below to test. As I can loadLibrary and the version number shows correctly I can assume that OpenCV is actually working.
import org.opencv.core.Core
import org.opencv.core.Mat
import org.opencv.core.CvType
import org.opencv.core.Scalar
import org.bytedeco.javacv.OpenCVFrameGrabber
fun main(args : Array<String>) {
println("Test Built Successfully")
System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
println("Running OpenCV Version ${Core.VERSION}")
val grabber = OpenCVFrameGrabber(1)
}
The line where we initialize the grabber rasies the following exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniopencv_core in java.library.path
Caused by: java.lang.UnsatisfiedLinkError: no opencv_imgproc320 in java.library.path
Perhaps I am missing something here, but I have followed the instructions provided by the README.md in the Git repository for JavaCV. I haven't seen anybody else trying to use this library with Kotlin on StackOverflow, though have checked out some posts about the same exception being raised using java.
Handling this error is no different in Kotlin from Java; you need to specify the path to the native libraries for opencv. By default on Windows it will look for the native libraries in whatever is set in your PATH environment variable.
You can also explicitly specify which directory to look for the native libraries by specifying the system property java.library.path (as indicated by the error message).
For example, you can add a run configuration like this:
Where ${PATH_TO_DYNAMIC_LIB} would be where ever the native lib opencv_imgproc320.dll is - I think in your case it would be C:/Users/ms/IdeaProjects/CVTest/opencv/build/java/x64.
I was able to resolve this by leaving my VM options blank and adding all necessary OpenCV libraries as native library paths in my JavaCV library configuration.
on MacOS, you don't need to install a native library. On OpenCV3.41, as fetched from
<!-- https://mvnrepository.com/artifact/org.openpnp/opencv -->
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<version>3.4.2-1</version>
</dependency>
I looked in the library (jar tf ~/.m2/repository/org/openpnp/opencv/3.4.2-1/opencv-3.4.2.-1.jar) and found dlls and whatnot in it:
jar tf *1.jar | grep nu
...
nu/pattern/opencv/osx/
nu/pattern/opencv/osx/x86_64/
nu/pattern/opencv/osx/x86_64/README.md
nu/pattern/opencv/osx/x86_64/cmake.log
nu/pattern/opencv/osx/x86_64/libopencv_java342.dylib
...
This led me to this StackOverflow question on nu.pattern which show how to use the nu.pattern in code.
static {
nu.pattern.OpenCV.loadShared();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
}
This prologue code enabled sample apps which used to fail as above to run.

dartium (chromium) error on yaml/src/loader.dart

I keep getting this
Internal error: 'package:yaml/src/loader.dart': error: line 197 pos 32: unexpected token '?'
_tryParseScalar(scalar) ?? new YamlScalar.internal(scalar.value, scalar)
?? is a quite new extension (null-aware operators) to the Dart syntax. You probably use a Dart version that doesn't support it.
Null aware operators were introduced with Dart 1.12
See https://github.com/dart-lang/sdk/blob/master/CHANGELOG.md#1120
You can check your current Dart version with dart --version on the command line.
The package using this new feature should have a SDK constraint that limits the package version to SDK versions that support this feature.

Android Studio Opencv - face detection sample

I'm student. I used Android Studio in new version, because I wanted to compile the face detection sample of Opencv library.
At first problem, I didn't resolve the ndk path.
Show that error:
Error:Execution failed for task
':openCVSamplefacedetection:compileDebugNdk'.
NDK not configured. Download the NDK from http://developer.android.com/tools/sdk/ndk/.Then add
ndk.dir=path/to/ndk in local.properties. (On Windows, make sure you
escape backslashes, e.g. C:\ndk rather than C:\ndk)
so I download the ndk and add the path like this.
ndk.dir=d\:\\android-ndk-r10e
Then, the new error is showing:
Error:Execution failed for task
':openCVSamplefacedetection:compileDebugNdk'.
A problem occurred starting process 'command 'd:\android-ndk-r10e\ndk-build.cmd'
Some people say that please add the sourceset.main in build.gradle, so I add the source code.
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "org.opencv.samples.facedetect"
minSdkVersion 15
targetSdkVersion 22
ndk {
moduleName "detection_based_tracker"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':openCVLibrary2410')
}
Finally built complete but my phone showed "FaceDetecter is stopped" and exit the app.
Don't you know the problem? I really don't know about Opencv's open source.
One possible solution to try it out with Task NdkCompile into your build.gradle file.
Also I do not think you need to add OPENCV as a dependency instead simply add JNI directories to configuration file as noted above (the jniLibs.srcDir is mapped to the proper location).
I know this question was asked 6 months ago, but I hope isn't late or others may find it useful for quick troubleshooting.
Best of luck.

Resources