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).
Related
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
I have some code I'd like to use across multiple different projects. Let's say it's some e-commerce code that handles things like payments and shopping carts.
It seems inefficient and dangerous to copy-paste everything across different projects. And if I add one feature or fix one bug in the core e-commerce module, I'd like that change to be reflected in other projects using it too.
I would also like to re-use some of the Activities, Fragments, Adapters too.
What is a good approach to this?
When we have a library project that needs to be shared to every project on a local computer, we can make use of Maven.
A. Here the step in your library that we will you for the project:
Make a library project from Android Studio.
Add Gradle Android Maven plugin to root build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
Add apply plugin for step 1 in your library build.gradle. (NOT root build.gradle):
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
Add the following after the apply plugin, this line to determine your library when adding to project:
group = 'com.yourpackage.yourlibrary'
version = '1.0'
Add the following code in your settings.gradle:
rootProject.name = 'yourlibrary'
Then publish it to your local maven with:
./gradlew install
Or you can use gradle option in Android Studio.
Your library will be installed in $HOME/.m2/repository. Remember that to use the library you need to add like this:
Groupid:artifactid:versionid
Artifactid will be package name of your library.
B. Here the step in your Project which using the library:
Add the following code in your root build.gradle:
mavenLocal() // for local maven.
This for getting the local library maven that we have installed in step A
Then in your app project.gradle, add compile for the library:
compile 'com.yourpackage.yourlibrary:yourlibrary:1.0'
Read more:
Gradle: How to publish a Android library to local repository
https://github.com/dcendents/android-maven-gradle-plugin
https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en
From my Knowledge 1. As others said try creating your own Module or Library and use it where ever you need 2.Use Version Control Tools Like Git(If your code changes it will be refleted in your git account)
I am building android library project, which has a dependency on another internal library project.
I am wondering if there is a way to package a single AAR library, which already contains internal library inside it. I would like to share only 1 AAR library package to my application developers.
This is how my build.gradle files look currently, but currently they produce separate AAR files and both needs to be included in Application's build.gradle. As application is being built by another company, we need to share the final AAR file with them and not the complete library projects.
----- internalLib -------->>>>>>>>>>
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
----- externalLib --------
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android-library'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
compile project(':internalLib')
}
There is no mechanism to combine library. It's a bit complicated as you probably want to control which dependencies get merged (for instance you probably don't want to include support-v4 in there). Also you'd need to merge the resources and Android manifest.
At this time there's no way to easily hack something, unless you are sure the resources have no conflicts between the two res folders (for instance you could have strings_a.xml in one lib and strings_b.xml in the other lib). This way you can just "merge" the two res folders by copying them both into the same location (as opposed to do a merge at the android res level).
For the Manifest it'd be more complicated, but doable with some custom code.
Providing a built-in mechanism for this is very low on our priority so don't expect it anytime soon.
For the sake you have to upload each library as separately on maven and use its implementation in parent library modules till the main library module. Only then when you publish your main library on maven will include your all child dependencies.
As far as we have only one option add aar as api dependency inside the module.
For that we have to generate aar file and publish it to Maven and make it accessible by another module and consume it in app.
https://developer.android.com/studio/projects/android-library
As mentioned above android developer document.
The library module with source code is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
If there anything else we can do, please let us know by jot down in the command section.
It is not supported
It is not recommended to include one library into another because it leads to a serious issues with managing versions and complexity of creating and supporting such solution.
You should stick to native approaches like dependency manager or rearchitect your codebase
[iOS Umbrella framework]
I am looking for an easy and somewhat automated way of starting and managing an Android project. I have been working on a few projects here and there and have found that a good amount of the time dependencies are a pain point. I am currently using Eclipse and looking into Maven. Which from what I understand is a dependency manager. I would greatly appreciate if anyone could provide suggestions on good process or tools to make setup and initialization less of a hassle. Thank you!
If you're starting to learn Android, don't waste your time with learning Maven / the Android Maven plugin, but go for Gradle / the Gradle plugin for Android and Android Studio as the IDE. Both Maven and Gradle are dependency managers (and build systems).
Google's docs on the new Gradle build system contain a section about why Gradle was chosen, and examples about how to manage dependencies. Basically, all you need to do is to add repositories and an compile dependencies like
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:11.0.2'
}
android {
...
}
I already saw this question, but it is not helping me. First of all, I tried to add google play services in my project using:
dependencies{
compile 'com.google.android.gms:play-services:6.5.87'
}
It was showing me error:
Then I updated my studio to 1.0.1 and gradle to 1.0.0. And then I again synced the project with gradle. And it worked! It showed me another option despite of two options shown in above screenshot. It was "Install the library"(something like that). I clicked it and it popped up a dialog, and I installed the library(it was like downloadind using SDK manager and not like gradle downloads).
Now, I tried to download this library using:
compile('com.fortysevendeg.swipelistview:swipelistview:1.0-SNAPSHOT#aar') {
transitive = true
}
And it gives me error:
My android repository is updated:
Also, my internet connection is working fine. I tried to sync project many times, but same error all the time. I am not running gradle in offline mode:
How to fix this? And what is the permanent solution? And why is all this happening?
I found this question: Studio failed to download library from gradle repository which describes the exact same error, and that question had this bit of build script that you need to add to the build file that has the dependency statement in question:
repositories {
maven { url 'http://clinker.47deg.com/nexus/content/groups/public' }
}
When I do this, it works for me.
As to the general question of why this happens (and the better question of why the solution is different for different libraries):
Gradle, the build system that Android Studio uses, has the ability to automatically download library dependencies from the Internet. By and large this is a big boon for developers, because instead of having to manually download archive files, put them in the right place in your project, check them into source control, and repeat the process for new versions, now you just have to add a line of build script and the build system takes care of the housekeeping for you. The major downsides are Internet connectivity woes, which affect different developers to different degrees, and some added confusion about what it means when you get an error.
How does Gradle know where to download dependencies? Most Gradle build scripts contain a block that looks like this:
repositories {
jcenter()
}
or it may be mavenCentral() instead of jcenter(). This tells the build system to look in the JCenter or Maven Central global repositories (and JCenter is in a simplistic way of thinking about it a value-added mirror of MavenCentral); these contain archives of many versions of many, many, many libraries and are very convenient to use.
You can specify other repositories as well. This swipelistview library hasn't been uploaded to Maven Central, so the developer has made a repository for it available via a URL: if you add that URL to your repositories block, it will look for it there.
I was worried about the fact that you're accessing a SNAPSHOT version of the library -- these are supposed to be unpublished by definition. But adding a dependency on the snapshot version of the library in my test project worked for me, and looking around that URL in a web browser reveals that there's only a "1.0-" (trailing dash included) version of the library, so there's some subtletly there I'm missing; if you know more, please edit my answer or comment.
In any event, there are a couple caveats to this explanation. Some libraries aren't on Maven Central or on any Internet-accessible archive (at least they're not officially published by Android), but are instead published as part of the Android SDK download and maintained via the SDK manager. The Android support libraries and Google libraries fall under this category. If you get errors about those not being found, you have to fix it via the SDK manager.
How does the build system know to look in the SDK for those, since you didn't tell it via the repositories block? This behavior is hardcoded into the Android Gradle plugin.
The other caveat is that there's a detail that trips up a lot of people, which is that you actually have two repositories blocks, though with the usual Android Studio setup they're often in different files. One is in a buildscript block, which usually lives in the top-level build.gradle file and looks like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
The other often also lives in the top-level build.gradle, but you can augment it with another block in your module's build.gradle file. The top-level one looks like this:
allprojects {
repositories {
jcenter()
}
}
and a module-level one would look like one of the previous examples in this answer. What do all of these mean?
The buildscript block tells Gradle where to find build system plugins. These are plugins that enhance the functionality of the build system itself but don't say anything about your actual project. In Android projects, the Android Gradle plugin is in this category, and unlike the Android/Google libraries, this one does live on Maven Central. The repositories block (in coordination with the dependencies block, which is not the same as the dependencies block for your project, keep reading) in buildscript tells the build system where to go look for these plugins.
The allprojects block in the top-level build file tells the build system to apply the bit of contained script to all build files in the project. In this example, it's telling it to add a repositories block pointing to JCenter to all subprojects. This is a convenience so you don't have to copy/paste it into multiple build files in your modules.
In your modules, you also have a repositories block, which in conjunction with the allprojects thingy, tells the build system where to go to get library dependencies for your project, as was previously discussed.