0

Important Files to understand the question:

VpnService Java Class

VpnService Translated to Delphi

My goal is to call the public static Intent prepare(Context context) method from VpnService.java and receive the Intent.

I tried several ways to access it, for example:

var
   Intent: JIntent;
   JVC: JVpnService;
begin
   JVC := TJVpnService.JavaClass.init;
   Intent := JVC.prepare(Context); // No Method prepare

or

var
   Intent: JIntent;
   JVC: JVpnServiceClass;
begin
   JVC := JVpnServiceClass.javaClass.init; // Record, Objekt oder Klassentyp erforderlich

or

var
Intent: JIntent;
   JVC: JVpnServiceClass;
begin
   JVC := JVC.javaClass.init; //Inkompatible Typen JVpnServiceClass und JVpnService

I really have no clue how to access this static method.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
r3dstorm4
  • 31
  • 12

2 Answers2

2

JavaClass.init() is a constructor call to create an object instance. Static methods are accessed from the JavaClass directly. So, you should be calling TJVpnService.JavaClass.prepare() instead. If that is not working then TJVpnService is likely not setup correctly to begin with.

Looking at the generated .pas file, I think its [JavaSignature] attribute is declaring the wrong type. That would explain why JNI can't find the prepare() method at runtime.

The generated .pas file does not include any declarations for the VpnService.Builder inner class at all, but yet the [JavaSignaure] attribute for TJVpnService is trying to import methods from the Builder class. prepare() is a method of the VpnService class itself, not the Builder class, so try changing the [JavaSignature] attribute to [JavaSignature('android/net/VpnService')] instead of [JavaSignature('android/net/VpnService$Builder')].

And then file a bug report with Software Union, as its Java2Pas tool imported the VpnService.java file incorrectly.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The Delphi unit was imported by Java2Pas, which is not an Embarcadero product. Having said that, Java2OP does not import it correctly either, however it doesn't import a lot of other classes correctly, often mistaking instance methods for static methods. – Dave Nottage Jul 31 '17 at 20:52
  • 1
    @DaveNottage: yeah, Java2OP is pretty buggy, there are a number of open tickets in QualityPortal about it generating erroneous code. – Remy Lebeau Jul 31 '17 at 21:14
  • Wow! Thank you very much! It helped changing the signature. Got to know its that easy :D – r3dstorm4 Aug 01 '17 at 08:11
  • Is there any coding example of this working? I want to build a GitHub VPN working service for Delphi, but not much documents on the internet that helps. – El Diablo Dec 22 '19 at 12:28
  • Plus a lot of Delphi Android Java information that is on the internet is either old or new and it's hard to tell which is the right information to use. Is there a location on the internet where all the information is kept up-to-date? – El Diablo Dec 22 '19 at 12:40
2

Even if the Java code was imported correctly, you would not be able to use VpnService from Delphi code. As per the documentation:

https://developer.android.com/reference/android/net/VpnService.html

You need to create a descendant of VpnService in order to implement it in your application. Creating a descendant in Delphi code is currently not possible; you would need to do it in Java code, and include it as part of your application.

Dave Nottage
  • 3,411
  • 1
  • 20
  • 57
  • I have Embarcadero Delphi 11 Alexandria Enterprise which was created in 2021. Can creating a descendant possible in Delphi now? – Greg T Mar 24 '23 at 14:18
  • I doubt that it will ever be possible to construct a descendant *directly* from a Java class. Delphi has a descendant of the `Service` class (and `JobIntentService`), however it is extremely complex in how it achieves it, and appears to use undocumented features, as I've tried to replicate it for things like the`Worker` class and failed – Dave Nottage Mar 24 '23 at 22:23
  • I've never done anything in Java. What tools do I need to do what you said to do and is there any source code reference for it, maybe a GitHub open source project or something? – Greg T Mar 24 '23 at 23:28
  • I'm just trying to create my own noroot firewall on Android. I need the VPN ability in order to accept / reject applications from using the internet. – Greg T Mar 24 '23 at 23:31
  • 1
    "I've never done anything in Java". This kind of thing might not be the greatest starting point, then. As for tools, as long as you have the JDK, and an Android SDK installed, you can use a plain text editor to create the `.java` source files and use the command line executables to compile/package the source – Dave Nottage Mar 25 '23 at 00:15
  • 1
    I've automated these kinds of tasks in [Codex](https://github.com/DelphiWorlds/Codex) (an add-in for Delphi). Otherwise, you could use [Android Studio](https://developer.android.com/studio) - I don't use it myself because it's overkill for most of the tasks I do. – Dave Nottage Mar 25 '23 at 00:16
  • 1
    [This SO post](https://stackoverflow.com/questions/30064823/a-firewall-for-android-with-vpnservice-responses-are-delivered-but-a-sockettim) might be of some interest to you, specifically the part about ToyVPN, which apparently could be used as a starting point for a firewall. – Dave Nottage Mar 25 '23 at 00:19
  • I've got one more question. If I create just the extended class of VPN service, I shouldn't need to expose any of it's functions, would I? – Greg T Mar 26 '23 at 14:55
  • If the service is independent of the application, it wouldn't need to interact with it directly, if that is what you mean. – Dave Nottage Mar 26 '23 at 18:33