I am an Android Developer and have a general Kotlin question. Does Android Studio support all Kotlin features and APIs? I am certain that all basic types and operators are fully supported and Android Studio can compile. Is it safe to also assume that Functions, Lambdas, Coroutines, etc. are fully supported? For example, when Java 8 support was announced for Android Studio, there was a documentation website explaining which Java 8 features were supported and which were not:
https://developer.android.com/studio/write/java8-support#supported_features
But this does not exist for Kotlin. I'm assuming if it's a line of standard Kotlin code, it will compile in Android Studio, is that correct?
The reason for my question is that I work with a group of Java server (mostly spring boot) and JavaFX developers, and we like to share as much code as possible. The lack of Java 8 compatibility in Android caused some problems for us. We're looking to convert most of our code into Kotlin now. I'd like to assume that all of the standard features:
https://kotlinlang.org/api/latest/jvm/stdlib/index.html
And hopefully all of the coroutines features:
https://kotlinlang.org/docs/reference/coroutines/coroutines-guide.html
Will be fully supported for both let's say an Android app built with Android Studio and a Java desktop app built with IntelliJ.
Within reason, you control the version of Kotlin that gets used and therefore what features are available.
If you have a Kotlin-enabled Android Studio project, and you look in the top-level build.gradle file, you may see code like this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.20'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext.kotlin_version = '1.3.20' controls the Kotlin version that you will use for the Kotlin plugin (via the interpolated string in the classpath directive), and similarly for the Kotlin runtime dependency in a module's build.gradle file.
So, a project with the above code can use Kotlin/JVM features that were supported in Kotlin 1.3.20.
The runtime dependency that we use today is org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version. The jdk7 indicates that the runtime does not depend on any Java 8+ stuff that Android lacks.
However, changes in future versions of Kotlin will require you to upgrade the Kotlin version in your project. For example, a project using 1.2.71 might not have access to all Kotlin 1.3 syntax.
Kotlin is fully supported by Android and Android Studio and even advertised on the Android website:
Kotlin is production-ready for your Android app development.
Source
Related
I create my Android Project on Android Studio 3.0.1 , and Now I try to Open it in
Android Studio 3.5.3 to do that I Added this :
mavenCentral()
maven { url 'https://maven.google.com' }
to build.gradle file for Project in repositories two Parts (buildscript / repositories) and (allprojects/repositories) .
My Question is : Does the app need to test all its features again??
For manual update:
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
}
}
No need to test anything because of gradle version change! But if downgrade the version then you might be change your some api,dependencies for lower gradle version.
Gradle:
Gradle is an advanced build toolkit for android that manages dependencies and allows you to define custom build logic. features are like. Customize, configure, and extend the build process. Create multiple APKs for your app with different features using the same project. Reuse code and resources.
And more about gradle: Link
I am using Android Data Binding, and while things were simple it was working very well. However, once I added a BindingAdapter annotation, my project stopped building in Android Studio with an error Execution failed for task ':app:compileSaferesourceDebugJavaWithJavac', but it didn't give me any more detail. Running gradlew build on the command line showed that the actual error was java.lang.ClassNotFoundException: javax.xml.bind.JAXBException. This makes sense, because this development machine has only Java 11 installed, not Java 8.
I found this answer, which says to add the following dependencies to gradle:
implementation "javax.xml.bind:jaxb-api:2.2.11"
implementation "com.sun.xml.bind:jaxb-core:2.2.11"
implementation "com.sun.xml.bind:jaxb-impl:2.2.11"
implementation "javax.activation:activation:1.1.1"
The problem I have is that I don't know where to add them. Adding them as implementation dependencies to the app/build.gradle doesn't work, because JAXB is a dependency of the build tools, not of my application itself.
I tried adding them as buildscript.dependencies too, but that didn't work either:
buildscript {
repositories {
google()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "javax.xml.bind:jaxb-api:2.3.1"
classpath "com.sun.xml.bind:jaxb-core:2.3.0"
classpath "com.sun.xml.bind:jaxb-impl:2.3.1"
classpath "javax.activation:activation:1.1.1"
}
}
I also tried adding them as buildscript.dependencies in the project root build.gradle file, but that also did not help:
buildscript {
repositories {
google()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "javax.xml.bind:jaxb-api:2.3.1"
classpath "com.sun.xml.bind:jaxb-core:2.3.0"
classpath "com.sun.xml.bind:jaxb-impl:2.3.1"
classpath "javax.activation:activation:1.1.1"
}
}
I know that I can use Java 8 to build this code, but I really don't want to have to deal with multiple Java versions and I have other projects that require Java 11.
Is there a place in the gradle configuration that I can put these build dependencies to get them to work?
Configurations I tested:
Operating Systems: Tested on Windows 10 and Windows Server 2016
Build Environments: Tested in Android Studio 3.4.1, Android Studio 3.5.0-beta04, and using Gradle Wrapper on the command line
Android Gradle Plugin: tested with 3.4.1 and 3.5.0-beta04
Android Build Tools: tested with 28.0.3 and 29.0.0
Note: comment asks for Databinding version, which is no longer relevant since Databinding is now built-in and does not have a separate version number.
Fail with error show above:
Java 11.0.1 x64
Working properly:
Java 1.8.0_212 x64
After extensive testing, it is clear that the Java version is the only thing that makes any difference here.
UPDATE As of 2020-07-21, the Android bug tracker now states:
Studio will also start using JDK 11 starting in 4.2, approx in 1 to 2 months.
According to a member of the Android Project on the Android Bug Tracker as of 2019-03-07:
Java 11 is not supported by Android studio and its tools.
Hello AndroidStudio Users,
Recently I have updated my Android Studio with 2.2 version, No so many Gradle and Instant Run update I got, that I have updated.
But whenever I open a new project, I am getting the following dialog to convert the project.
Is it required to convert? What it will convert actually? Does anyone know?
It just update your build.gradle file for new features of Android Studio.
buildscript
for Android Studio Version 2.1.3
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
to Latest Android Studio Version 2.2
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Here you would find all changes had been made with - the newest version of Android Studio: http://tools.android.com/recent/androidstudio22andemulator2522arestable
This release is focused on bug fixes, performance, and the following
new features:
Design
Layout Editor
Constraint Layout
Layout Inspector (Experimental)
PSD File Support in Vector Asset Studio
Develop
Firebase Plugin
Updated Code Analysis & Lint checks
Enhanced accessibility support
Improved C++ Support Edit & Debugging
IntelliJ 2016.1.3 platform update
Samples Browser
Improved Font Rendering
Build
Jack Compiler Improvements
Java 8 Language Support
C++ ndk-build or CMake
Merged Manifest Viewer
Build cache (Experimental)
OpenJDK Support
Instant Run Improvements
Test
Espresso Test Recorder (Beta)
APK Analyzer
GPU Debugger (Beta)
Virtual Sensors in the Android Emulator
Check also: http://android-developers.blogspot.com/2016/09/android-studio-2-2.html
As you see there are many changes which force you to talk with your team mates. If they would decide to stick with older version 2.1.3, please unnistall the latest one and the most important: don't convert your project, as you may have issues with opening converted project on older Android Studio version.
The problems I alraedy see are:
- new android sdk version 25.+
- new Gradle plugin version
- constraint-layout
Hope it will help
changed it just an hour back.. you can convert it !!
If you are working on VCs tell your team mate to update sdk 25.2.2 .
Convert the project to new sdk and you gotta feel whole new experience.
Surprise :xml preview has been changed :)
I just developed a JavaFx application which use RXTX library for serial communication and in the future will use Bluecove for bluetooth. It was my understanding that there are some ways to run a JavaFx app on Android devices, so I began my research.
The first option was running the "jar" packaged app with the Android app "Java Manager". The problem here is that I wouldn't know what to do with the external libraries, since there isn't a standard JVM on the android devices where I could place them.
I found this project "http://v-lad.org/projects/gnu.io.android/", but it seems it's oriented to Android applications.
So when I found "http://javafxports.org/", I tried to make my first steps with it. This project seems to be what I need, but I'm a newbie in Android and I find documentation a little bit confusing, so I'm not sure where to start. Moreover, I'm still not sure that I could use those java libraries in Android with this approach.
Does anyone know if what I pretend is doable?? In that case, which steps should I follow??
You can already try JavaFXPorts plugin. It's in continuos development, but the recent versions are mature enough to use it without problems.
Have a look at the basic requirements to get started here. You will need JDK8u40+, Gradle 2.2+, Android SDK and Android Build tools.
Once you have everything ready, you can try the samples.
Also I suggest you have a look at Gluon-plugin for NetBeans. Basically it will create a new empty JavaFX project for you, and you will be able to build it and deploy it on your desktop, on Android and on iOS platforms.
Have a look at the build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b4'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
}
mainClassName = 'org.test.javafxports.TestJavaFX'
jfxmobile {
ios {
forceLinkClasses = [ 'org.test.javafxports.**.*' ]
}
}
First of all, you just need to update the plugin version. Check here for the last one: 1.0.0-b8.
Build and run on your desktop, or run the tasks like androidInstall to generate the apk and deploy it on your Android mobile.
Once you have tested it, and everything is working properly, you can start adding the code of your project.
And back to your question, yes, you can add any third party jar to the project.
Basically you just need to add the dependency on the gradle file. You can use compile or runtime with local files or from external repositories:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b8'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
}
dependencies {
compile files('lib/<your local jar>.jar')
compile 'org.glassfish:javax.json:1.0.4'
androidCompile 'org.glassfish:javax.json:1.0.4'
}
mainClassName = 'org.test.javafxports.TestJavaFX'
jfxmobile {
ios {
forceLinkClasses = [ 'org.test.javafxports.**.*' ]
}
}
Note you can add dependencies that only are added to one single platform, like androidCompile or androidRuntime.
Since the apk will run on Dalvik VM, only Java 7 features are allowed. You can use lambdas, though, since the plugin uses Retrolambda project internally on your code. Be aware that it is not applied on the added jars.
As an example of using jars on your project, you can use Gluon-Charm-Down open source library, that already provides access to some native services on your device, like local storage or GPS.
dependencies {
compile 'com.gluonhq:charm-down-common:0.0.1'
androidRuntime 'com.gluonhq:charm-down-android:0.0.1'
desktopRuntime 'com.gluonhq:charm-down-desktop:0.0.1'
}
In fact, with jfxmobile plugin and this library, the 2048FX game has been successfully ported to Android (Google Play) and iOS (Apple Store).
I'm wondering what is the best / intended way of defining build script repositories and dependencies for Android library projects.
Out in the wild (like Github) most of the repos define the following in there build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
} }
But if I create a library project in Android Studio this buildscript block does not get created, as it is already in the root build.gradle file, and therefore not necessary (I assume).
So what is the proper way, only defining it once in the root build.gradle or for each library project (which also means updating the version number for each library in case of new plugin versions). Or does it depend whether I want to release the library independently from my main application?
If you're developing a library, there isn't really a top-level build.gradle: that top level applies to all modules in a multimodule project, but a library is best implemented as a standalone single module.
Probably the best practice would be to have a single build.gradle file for your library module, and include a buildscript block in it with the repository and Android plugin Gradle version. That way it stands alone better, and it's not implicitly dependent on being in a project that has a top-level build.gradle file.
There's a huge caveat which kind of renders a lot of this moot, however -- if you include this module, with its own buildscript block, in a project that has a top-level build.gradle with a competing buildscript block, the top-level build file will win and it will ignore the block in your module.
This probably isn't much of a problem, but if your module relied on a specific version of the Android Gradle plugin, for example, and it was in a project that used a different version of the plugin, then your module would lose and it could run into problems. Another way of saying it is that it can only use one version of a plugin for all of the modules in a single build, and I believe the first one to specify it sets it for everyone.
In practice, this is only likely to be an issue if you rely on some feature specific to a later-than-1.0 version of the Android Gradle plugin, and it's included in a project that uses 1.0.
There are some subtleties here that I'll mention but won't answer (and I apologize for answering your question with more questions) -- if anyone else knows for sure, please answer yourself, or edit my answer, or mention it in the comments:
What if your buildscript block adds new plugins to the classpath? Do they get added or are they ignored?
What if your buildscript block adds new repositories to the search path? Will they get picked up?