vipcxj / jasync

make async-await code style available in java just like csharp and es6
Apache License 2.0
129 stars 14 forks source link

Not working on Android #11

Open customautosys opened 2 years ago

customautosys commented 2 years ago

I marked my method @Async and didn't call it in a lambda or anonymous class but still got the following error:

java.lang.UnsupportedOperationException: The method "JPromise#await" should be invoked in an async method. An async method is a method annotated with @JAsync and returning the JPromise object. The method should not be a lambda method or in an anonymous class
    at io.github.vipcxj.jasync.reactive.ReactorPromise.await(ReactorPromise.java:450)

Relevant code below:

@Async
public void loadAd(){
    try{
        AdManager.instance().interstitial().await();
        ...
    }catch(Exception e){
        e.printStackTrace();
    }
}

public class AdManager extends com.customautosys.tuxfight.AdManager{
    @Override
    public JPromise<Void> interstitial(){
        if(AndroidLauncher.getInstance().adView==null) return JAsync.error(new Exception("AdMob not loaded yet"));
        return Promises.from(Mono.create(monoSink->AndroidLauncher.getInstance().runOnUiThread(()->InterstitialAd.load(
            AndroidLauncher.getInstance(),
            BuildConfig.DEBUG?"ca-app-pub-...":"ca-app-pub-...",
            new AdRequest.Builder().build(),
            new InterstitialAdLoadCallback(){
                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd){
                    interstitialAd.show(AndroidLauncher.getInstance());
                    monoSink.success();
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError){
                    monoSink.error(new Exception(loadAdError.getMessage()));
                }
            }
        ))));
    }
}
customautosys commented 2 years ago

I'm also getting this despite including jasync-core-java9:

java.lang.IllegalArgumentException: Unsupported class file major version 61
vipcxj commented 2 years ago

Can you provide me an simple android project? I have not an android project to test it. By the way, this project require jdk8. I know the latest android support java 8, but the old version not. And a valid async method must return a JPromise object. And in you case, you return void.

jzlhll commented 2 years ago

android java8 is similar implements partly by google. It's not full

customautosys commented 2 years ago

Hi, so sorry I haven't had the time recently because I managed to use ea-async instead which could work out of the box with Android.

I'll try to create a sample Android project for you when I have the time.