nativescript / android-dts-generator Goto Github PK
View Code? Open in Web Editor NEWA tool that generates TypeScript declaration files (.d.ts) from Jars
A tool that generates TypeScript declaration files (.d.ts) from Jars
Currently the generated files do not reflect any base types. For example:
declare module android {
export module view {
export class ViewGroup {
Should be:
declare module android {
export module view {
export class ViewGroup extends android.view.View{
I'm trying to write a plugin for PDFBox-Android.
I am running the tool with a slightly modified jar (because namespace functions is invalid).
When I compile my typescript files, everything is fine.
But when I run the plugin (with npm run demo.android), I am getting the following errors:
../src/platforms/android/typings/pdfbox-android.d.ts:1611:19 - error TS2720: Class 'PairData0Format0' incorrectly implements class 'Comparator<Array<number>>'. Did you mean to extend 'Comparator<Array<number>>' and inherit its members as a subclass?
Property 'equals' is missing in type 'PairData0Format0'.
1611 export class PairData0Format0 extends com.tom_roush.fontbox.ttf.KerningSubtable.PairData implements java.util.Comparator<native.Array<number>> {
~~~~~~~~~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:4714:18 - error TS2720: Class 'COSArray' incorrectly implements class 'Iterable<COSBase>'. Did you mean to extend 'Iterable<COSBase>' and inherit its members as a subclass?
Property 'wait' is missing in type 'COSArray'.
4714 export class COSArray extends com.tom_roush.pdfbox.cos.COSBase implements java.lang.Iterable<com.tom_roush.pdfbox.cos.COSBase> {
~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:5034:18 - error TS2720: Class 'COSName' incorrectly implements class 'Comparable<COSName>'. Did you mean to extend 'Comparable<COSName>' and inherit its members as a subclass?
Property 'wait' is missing in type 'COSName'.
5034 export class COSName extends com.tom_roush.pdfbox.cos.COSBase implements java.lang.Comparable<com.tom_roush.pdfbox.cos.COSName> {
~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:7309:33 - error TS2314: Generic type 'List<E>' requires 1 type argument(s).
7309 public writeTokens(param0: java.util.List): void;
~~~~~~~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:7463:32 - error TS2314: Generic type 'List<E>' requires 1 type argument(s).
7463 public setThreads(param0: java.util.List): void;
~~~~~~~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:11087:31 - error TS2314: Generic type 'List<E>' requires 1 type argument(s).
11087 public setPages(param0: java.util.List): void;
~~~~~~~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:11099:38 - error TS2314: Generic type 'List<E>' requires 1 type argument(s).
11099 public setEmbeddedFDFs(param0: java.util.List): void;
~~~~~~~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:11192:33 - error TS2314: Generic type 'List<E>' requires 1 type argument(s).
11192 public setOptions(param0: java.util.List): void;
~~~~~~~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:17568:33 - error TS2314: Generic type 'List<E>' requires 1 type argument(s).
17568 public static sort(param0: java.util.List): void;
~~~~~~~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:17569:33 - error TS2314: Generic type 'List<E>' requires 1 type argument(s).
17569 public static sort(param0: java.util.List, param1: java.util.Comparator): void;
~~~~~~~~~~~~~~
../src/platforms/android/typings/pdfbox-android.d.ts:17569:57 - error TS2314: Generic type 'Comparator<T>' requires 1 type argument(s).
17569 public static sort(param0: java.util.List, param1: java.util.Comparator): void;
~~~~~~~~~~~~~~~~~~~~
Can someone tell me how to fix them? I really need the typings.
You'll see errors with classes like java.lang.Enum
because there is no default generic type.
I wonder if we can determine the right generic type
Replacing getMethodParamSignature with the below (it's a drop-in backwards-compatible replacement) means that most parameter names are recovered (rather than being param0, param1 etc).
I'd do a pull request if I knew how :)
private String getMethodParamSignature(JavaClass clazz, TypeDefinition typeDefinition, Method m) {
LocalVariableTable table = m.getLocalVariableTable();
StringBuilder sb = new StringBuilder();
sb.append("(");
int idx = 0;
for (Type type : this.getArgumentTypes(m)) {
if (idx > 0) {
sb.append(", ");
}
int localVarIndex;
if (!m.isStatic()) {
localVarIndex = idx + 1;
} else {
localVarIndex = idx;
}
if (table != null && table.getLocalVariable(localVarIndex, 0) != null) {
sb.append(table.getLocalVariable(localVarIndex, 0).getName());
} else {
sb.append("param");
sb.append(idx);
}
idx+=1;
sb.append(": ");
String paramTypeName = getTypeScriptTypeFromJavaType(type, typeDefinition);
// TODO: Pete:
if (paramTypeName.startsWith("java.util.function")) {
sb.append("any /* " + paramTypeName + "*/");
} else {
addReference(type);
sb.append(paramTypeName);
}
}
sb.append(")");
String sig = sb.toString();
return sig;
}
The option with multiple definition files makes it harder to integrate with TS projects. My suggestion is to enable generation of single file with all the types and make it the default option. We may add a command-line arg that switches between the two approaches.
Recently updated to the latest generator and I'm noticing that many times java.*
classes do not include periods.
I notice you create a reference outside the module
declarations but most of the time they are not included:
import javaniocharsetCharset = java.nio.charset.Charset;
declare module okhttp3 {
export class MediaType extends javalangObject { // SHOULD BE java.lang.Object
public type(): string;
public subtype(): string;
public charset(): javaniocharsetCharset;
public hashCode(): number;
public charset(param0: javaniocharsetCharset): javaniocharsetCharset;
public static parse(param0: string): okhttp3.MediaType;
public equals(param0: javalangObject): boolean; // SHOULD BE java.lang.Object
public toString(): string;
}
}
It would be best to remove offuscated classes because they create a lot of TS errors because they use private words.
How would I go about using an aar
file instead of jar
? I'd like to input play-services-maps-9.4.0.aar
into the dts-generator.
When generating deprecated methods we should add @deprecated JSDoc, so that their usages can be detected using tslint.
example implementation 'com.drivequant.drivekit:drivekit-trip-analysis:1.27.2'
As of now the generator does not create constructors for interface types. We need the constructor to enable the extend
syntax through TypeScript. Like so:
var impl = new com.myPgk.Interface1({
method1: function(args) {
// implementation code
}
});
I'm trying to update my tns-platform-declarations to use the latest android sdk.
I've generated the definitions for the respective sdks and replaced the include in tns-platform-declarations/android/android.d.ts
to use the generated file (currently android23.d.ts).
Problem is I now get - among others, including a lot of duplicate identifiers - a million error TS2503: Cannot find namespace 'native'.
, which tells me not everything has been extracted. Where do I go from here? The sdk declaration files have wildly differing sizes:
-rw-rw-r-- 1 yule yule 13777754 Apr 25 13:17 android17.d.ts # Original file
-rw-rw-r-- 1 yule yule 4686196 Apr 25 14:20 android20.d.ts
-rw-rw-r-- 1 yule yule 5191200 Apr 25 14:20 android21.d.ts
-rw-rw-r-- 1 yule yule 5214515 Apr 25 14:20 android22.d.ts
-rw-rw-r-- 1 yule yule 5006948 Apr 25 14:20 android23.d.ts
-rw-rw-r-- 1 yule yule 17178798 Apr 25 14:19 android24.d.ts
-rw-rw-r-- 1 yule yule 5730638 Apr 25 13:17 android25.d.ts
Which tells me I'm probably doing something wrong.
Do I have to include anything else?
How do I import all the dependencies required for a
jar
file I'm setting as input?
First I want to say, this repository is a game changer for debugging native Android plugins. I'm using it to see exactly what APIs are exposed to TypeScript for any .jar
file.
For example, here's a gist of the BoltsFramework/Bolts-Android bolts.Task.d.ts.
I'm having an issue with dts-generator.jar
and detecting dependentcies for the input jar
file.
Example:
java -jar dts-generator.jar -input bolts-tasks-1.4.0.jar
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: Couldn't find class: java.lang.Exception required by class: bolts.AggregateException. You need to provide the jar containing the missing class: java.lang.Exception
What I've been doing so far is adding android.jar
from $ANDROID_HOME/platforms/android-23/android.jar
and adding it to an input like so:
java -jar dts-generator.jar -input android.jar -input bolts-tasks-1.4.0.jar -generate-multiple
Which most of the time results in a working output of *.d.ts
files.
Although sometimes for example,
java -jar dts-generator.jar -input android.jar -input OneSignalSDK.jar -generate-multiple
Errors
Exception in thread "main" java.lang.NoClassDefFoundError: Couldn't find class: com.amazon.device.messaging.ADMMessageHandlerBase required by class: com.onesignal.ADMMessageHandler. You need to provide the jar containing the missing class: com.amazon.device.messaging.ADMMessageHandlerBase
Since the android.jar
file does not contain com.amazon.device
classes.
How do I import all the dependencies required for a jar
file I'm setting as input?
Windows only issue. Steps to reproduce:
Set a library with testCompileOnly
testCompileOnly "com.android.support:support-v4:27.0.1"
Execute
$ ./gradlew extractAllJars
Result:
FAILURE: Build failed with an exception.
* Where:
Build file 'D:\repositories\android-dts-generator\dts-generator\build.gradle' line: 62
* What went wrong:
Execution failed for task ':extractAllJars'.
> Illegal char <:> at index 45: jar-files\support-v4.aar (com.android.support:support-v4:27.0.1)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
1 actionable task: 1 executed
Java class with static member named B
, and a nested/inner class of the same name - B
will collide, and not let TSC to compile.
Consider the following Java class, which is completely valid:
public class A {
public static A.B B;
public class B {
// ...
}
}
While the Java compiler can tell whether the static member is used, or the class, that same intention cannot be translated easily to JavaScript, and the TypeScript compiler complains with Duplicate identifier
errors.
Real scenario - the com.google.android.gms.cast.Cast
API:
declare module com {
export module google {
export module android {
export module gms {
export module cast {
export class Cast extends javalangObject {
// ...
public static CastApi: com.google.android.gms.cast.Cast.CastApi;
}
export module Cast {
export class CastApi extends javalangObject {
// ...
}
}
}
}
}
}
}
We could try to read if the current class has nested/inner classes, and omit writing methods/fields with the same names.
I've been using this for development with Rhino (JS engine in Java) which provides access to Java libraries.
I had to manually hack the output somewhat (there's no concept of Native.Array for example)
It would be great if there was an option to make the generator produce more "vanilla" Typescript definitions. Any chance?
I can possibly help with the implementation.
I'm trying to generate definitions like described in the docs:
Generate definitons for Android SDK
cd dts-generator ./gradlew jar java -jar build/libs/dts-generator.jar -input %ANDROID_HOME%/platforms/android-<Platform Level (21/22/23/24)>/android.jar
[17:22:36] hettiger:android-dts-generator git:(master*) $ cd dts-generator
[17:22:46] hettiger:dts-generator git:(master*) $ ./gradlew jar
BUILD SUCCESSFUL in 0s
3 actionable tasks: 3 up-to-date
[17:22:51] hettiger:dts-generator git:(master*) $ java -jar build/libs/dts-generator.jar -input ${ANDROID_HOME}/platforms/android-28/android.jar
Android d.ts Generator Version : 2.0.0
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/bcel/classfile/FieldOrMethod
at com.telerik.dts.Generator.start(Generator.java:34)
at com.telerik.Main.main(Main.java:37)
Caused by: java.lang.ClassNotFoundException: org.apache.bcel.classfile.FieldOrMethod
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
Am I doing something wrong here?
Hi,
does anybody knows what could be the source issue of this? I tried putting sdk package in the root of dts-generator and even to other folder, trie putting path in double quotes, nothing changed this.
thanks
I generated d.ts file for java.util, and paytm android sdk, both giving few errors in the file
app/item/android.d.ts(18,8): error TS2300: Duplicate identifier 'javalangThrowable'.
app/item/android.d.ts(18,28): error TS2503: Cannot find namespace 'java'.
app/item/android.d.ts(133,22): error TS2503: Cannot find namespace 'java'.
app/item/android.d.ts(153,26): error TS2503: Cannot find namespace 'android'.
app/item/android.d.ts(154,30): error TS2503: Cannot find namespace 'android'.
app/item/android.d.ts(155,35): error TS2503: Cannot find namespace 'android'.
app/item/android.d.ts(181,32): error TS2503: Cannot find namespace 'android'.
app/item/android.d.ts(283,24): error TS2503: Cannot find namespace 'java'.
app/item/android.d.ts(284,29): error TS2503: Cannot find namespace 'java'.
app/item/android.d.ts(340,28): error TS2503: Cannot find namespace 'java'.
app/item/android.d.ts(362,31): error TS2503: Cannot find namespace 'android'.
app/item/android.d.ts(363,32): error TS2503: Cannot find namespace 'android'.
app/item/android.d.ts(364,39): error TS2503: Cannot find namespace 'android'.
app/item/android.d.ts(365,33): error TS2503: Cannot find namespace 'android'.
app/item/items.component.ts(113,24): error TS2339: Property 'java' does not exist on type 'typeof com'.
app/item/java-util/java.util.d.ts(2,8): error TS2300: Duplicate identifier 'javalangThrowable'.
app/item/java-util/java.util.d.ts(2,28): error TS2503: Cannot find namespace 'java'.
app/item/java-util/java.util.d.ts(16,28): error TS2503: Cannot find namespace 'java'.
app/item/java-util/java.util.d.ts(69,22): error TS2503: Cannot find namespace 'java'.
app/item/java-util/java.util.d.ts(85,25): error TS2503: Cannot find namespace 'java'.
app/item/java-util/java.util.d.ts(86,29): error TS2503: Cannot find namespace 'java'.
app/item/java-util/java.util.d.ts(106,29): error TS2503: Cannot find namespace 'java'.
app/item/java-util/java.util.d.ts(107,28): error TS2503: Cannot find namespace 'java'.
app/item/java-util/java.util.d.ts(108,29): error TS2503: Cannot find namespace 'java'.
TypeScript compiler failed with exit code 1
This happens with only a few jar files, but I get this error when throwing them through the dts-generator:
Exception in thread "main" java.lang.NullPointerException
at com.telerik.dts.DtsApi.getInterfaces(DtsApi.java:352)
at com.telerik.dts.DtsApi.generateDtsContent(DtsApi.java:75)
at com.telerik.dts.Generator.generateDts(Generator.java:37)
at com.telerik.dts.Generator.start(Generator.java:29)
at com.telerik.Main.main(Main.java:21)
I've found that adding a check if clazz1 != null allows the generator to work.
JavaClass clazz1 = ClassRepo.findClass(intface);
The fix source:
roblav96@1dae532#diff-84b371273f2d411517c3ef4981518716
Download jars here:
https://drive.google.com/file/d/0BzsNfWSNiUKLRlNOWkctb1JvZ1E/view?usp=sharing
OneSignal
java -jar dts-generator-debug.jar \
-input android24.jar \
-input android-support-v4.jar \
-input android-support-v7-appcompat.jar \
-input amazon-device-messaging-1.0.1.jar \
-input OneSignalSDK.jar \
-generate-multiple
materialdrawer
java -jar dts-generator.jar \
-input android24.jar \
-input android-support-v4.jar \
-input android-support-v7-appcompat.jar \
-input android-support-v7-recyclerview.jar \
-input materialize-1.0.0.jar \
-input materialdrawer-5.7.0.jar \
-generate-multiple
Kotlin can generate namespaces with function
. We end up with export module function
which is forbidden.
Not sure if we can fix this and still keep the typings for that namespace
Hi, thanks for making the project, Could you please add a license to the repo?
Thank you.
Wanted to clone this to geenrate typings for my own Android library (to be used in a NativeScript plugin)
Steps to reproduce
git clone https://github.com/NativeScript/android-dts-generator.git
cd android-dts-generator
cd dts-generator/
./gradlew jar
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/adriann/Developer/android-dts-generator/dts-generator/build.gradle' line: 14
* What went wrong:
A problem occurred evaluating root project 'dts-generator'.
> /Users/adriann/Developer/android-dts-generator/dts-generator/gradle.properties (No such file or directory)
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 483ms
It seems gradle.properties
is missing.
Currently the latest state is in the plamen5kov/dts_tool
branch. Merge it with master and use Git flow from now on.
We do not need to generate entries for methods in inherited types if the signature hasn't changed. For example:
class A {
public void method1() {
}
}
class B extends A {
@Override
public method1() {
super.method1();
// extended logic
}
}
In the above case we do not need to generate signature for method1
in class B
. This will improve the size of the generated definitions.
building typings for androidx :
testCompileOnly "com.google.android.material:material:1.0.0"
then using
java -jar build/libs/dts-generator.jar --skip-declarations -input dts-generator/jar-files
Some genearted classes are missing their parent class. Example com.google.android.material.textfield.TextInputLayout
or com.google.android.material.textfield.TextInputEditText
If you fix it by adding the extends you end up with some incompatible method declarations compared to parent class.
Hello friends ,
I am getting following error while generating typings.
java -jar build/libs/dts-generator.jar -input ../jars/opencv-platform-4.0.1-1.5.jar
Android d.ts Generator Version : 2.0.0
Exception in thread "main" org.apache.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
at org.apache.bcel.classfile.Constant.readConstant(Constant.java:161)
at org.apache.bcel.classfile.ConstantPool.<init>(ConstantPool.java:69)
at org.apache.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:235)
at org.apache.bcel.classfile.ClassParser.parse(ClassParser.java:143)
at com.telerik.dts.JarFile.readJar(JarFile.java:45)
at com.telerik.dts.Generator.loadJavaClasses(Generator.java:98)
at com.telerik.dts.Generator.start(Generator.java:38)
at com.telerik.Main.main(Main.java:37)
Hi,
i tried to generate .d.ts file for java-util-1.3.1, and the command i have given is
java -jar build\libs\dts-generator.jar -input g:\ java-util-1.3.1
Generation of definition took 10ms.
I want to use this tool to generage Android23.d.ts. Then replace my project android17.d.ts that download from https://github.com/NativeScript/NativeScript/blob/master/android17.d.ts
When launching gradlew jar
it gives me the following error. I have changed distributionUrl
to distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
in dts-generator/gradle/wrapper/gradle-wrapper.propeties and now it works.
Source: https://stackoverflow.com/questions/61289461/java-lang-noclassdeffounderror-could-not-initialize-class-org-codehaus-groovy-v
Hi,
I'm using the aforementioned experimental build option and it is quite handy.
What I noticed though is that if there is an inheritance then it is not possible to access the base class field from the derived class. Say, Android has the Telephony.Sms.Inbox.BODY
where Inbox
is
public static final class Inbox implements BaseColumns, TextBasedSmsColumns
and BODY
is defined in the TextBasedSmsColumns
. What ends up generated is:
export class Inbox extends java.lang.Object implements android.provider.BaseColumns, android.provider.Telephony.TextBasedSmsColumns {
public static CONTENT_URI: android.net.Uri;
public static DEFAULT_SORT_ORDER: string;
}
and basically an attempt to use in a native-like way android.provider.Telephony.Sms.Inbox.BODY
fails, instead I need to do android.provider.Telephony.TextBasedSmsColumns.BODY
.
Something you may want to check out and some point :)
Thank you!
P.S.: also keeping the original parameter names instead of paramX would have been nice, though I guess that's in the works too :)
The AppCues SDK demonstrates a great case of conditions to consider to improve the typing generation.
If types are generated the following issues will be seen:
The generated TS definitions are currently generated manually and tested with tsc
, we can automate that work by passing various android/java libraries and testing compilability of the output.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.