Javafx with Android - Api

2019-09-01 03:56发布

问题:

I'm trying to use Android specific code in a JavaFx Project. I wrote a litte game on JavaFX and when I use JavaFX-Ports I can run this Game on my Android Handy. Now I want to display some ads and for this I have to use the FXActivity as a context.

I imported the FXActivity

import javafxports.android.FXActivity;

Then I get this Error:

D:\Martin\Programmieren\fx to android\Trio\src\main\java\net   \hagh\Main.java:18:
error: package javafxports.android does not exist
import javafxports.android.FXActivity;
                      ^
1 error
:compileJava FAILED

Now I added the jfxdvk.jar and android.jar (from Android-SDK) to my dependencies and now my build.gradle file looks like:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b9'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

mainClassName = 'net.hagh.Main'

dependencies {
    compile files("D:/Programmieren/fx to android/dalvik-sdk/rt/lib/ext/jfxdvk.jar", "C:/Users/Martin/AppData/Local/Android/android-sdk/platforms/android-22/android.jar")
}

repositories {
    jcenter()
}

The same error as before...

What did I wrong and how can I do it right? I searched long for a solution but there are so many different tutorials and I think most of them are outdated. Can someone give me a correct build.gradle file?

I found this example: https://bitbucket.org/javafxports/samples/src/56e2050ef9e1/HelloPlatform/?at=default and I think this is the right way to do it. But I dont understand how to use the FXActivity in this way

Edit: I opened a new question for this

The Gluon plugin for NetBeans and the HelloPlatform example helped me but now I want to use the Google-Play-Services to show adds. In added this to my build.gradle file:

repositories {
    def androidHome = System.getenv("ANDROID_HOME")
    maven { url "$androidHome/extras/android/m2repository/" }
    maven { url "$androidHome/extras/google/m2repository/"}
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.google.android.gms:play-services-ads:7.5.0'
}

And in my AndroidManifest.xml I added:

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

And their I get my next error when I try to build the apk-file

:processAndroidResources UP-TO-DATE
C:\Users\Martin\Documents\NetBeansProjects\Trio\build\javafxports\tmp\android\AndroidManifest.xml:27: error: Error: No resource found that matches the given name (at 'value' with value '@integer/google_play_services_version').

:processAndroidAndroidResources FAILED

I searched a bit and I found that I should include the google-play-services_lib but I dont know how. Its my first time working with gradle

回答1:

For starters, you don't need to add those dependencies: the jfxmobile plugin does it already for you.

You may have a look at the Gloun plugin for NetBeans, to create a new JavaFX project with JavaFXPorts.

Now, in this new project, if you want to add some Android code, you have to add it on the Android/Java package.

One way to start is, as you have already pointed out, by following HelloPlatform. It includes a PlatformService to load for you the class depending on the platform you are running. Thanks to that, you can load AndroidPlatformProvider if you are on Android. So you would have to provide the methods you want to call from your main class, regardless the platform, and implement them on each platform (at least with dummy content).

You can also have a look at this question, as it provides a full solution with android code.