A tool that generates TypeScript declaration files (.d.ts) from Jars/Aars
Because there are certain incompatibilities between Java and TypeScript, definitions MAY NOT always be completely accurate. They will however compile and provide auto complete feature inside a modern text editor supporting typings.
new
syntax, as well as show you all its members that need to be implementedjava.util.function
don't have their typings generated, as function
is a reserved keyword in TypeScript and JavaScriptjava.util.function
will return any
insteadjava.util.function
will take any
insteadjava.util.Iterator
, android.animation.TypeEvaluator
, java.lang.Comparable
do not implement those interfaces in the generated definitions for compatibility-related issuesGenerate definitions following any of the approaches described below. Once you have run any of commands, you can find the generated typings in dts-generator/out
directory.
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
cd dts-generator
./gradlew jar
java -jar build/libs/dts-generator.jar -input <Path to your Jar/Aar>
cd dts-generator
./gradlew jar
java -jar build/libs/dts-generator.jar -input <jar1> <jar2> <jar3>
Another option is to pass the folder containing the jars you want to pass
cd dts-generator
./gradlew jar
java -jar build/libs/dts-generator.jar -input <jarFolder>
Note: Check the Makefile for sample usage
In order to test the tool there are some predefined androuidx packages. We are running the tool and comparing the output with the expected output commited in the repo. To run the test:
make test-compare-output
Then check whether the make command will return an error comparing the files.
Note: If the libs folder content is changed, the expected output should also be changed
Pass the aar as input as you would normally do
Generating the typings corresponding to the android and android-support jar files is a bit tricky operation, so here's a detailed explanation how to do it. There are different andoid support versions and they depend on main android classes it is a little bit complicated to generate those typings. One option is to generate a big d.ts file with all the libraries inside. The downside of this approach is that you have to generate a big file for every API level and if the android support version is changed all those d.ts files need to be regenerated. To avoid this there's some functionality in the tool for passing dependencies when generating typings. There are two type of dependencies that can be passed:
extends android.view.ViewGroup
.//Generics information:
.
So to fix this we need to provide a file with all the generic information for the packages the current jar relies on. You need to create a file and copy all the generic informations of the related packages and provide it in the input-generics argument. This will make all the generic classes referenced without passing types to pass any so that the ouput will be valid.There is an option all-generic-implements which is disabled by default which controls whether to add implements for all interfaces (if they are more than one) to generic type declarations. The problem is that in most of the cases one of those interfaces is actually an extend, so it should be manual reviewed and fixed after generation.
If you want to generate typings of a package but you are not sure how you can get all the needed dependencies you can follow the steps bellow:
Open dts-generator/build.gradle file and locate dependencies
part.
Add as a testCompileOnly
dependency the one that you want to generate typings for:
dependencies {
implementation 'org.apache.bcel:bcel:6.2'
implementation 'commons-io:commons-io:2.6'
implementation 'com.google.code.findbugs:findbugs:3.0.1'
// add your dependency bellow
testCompileOnly "com.android.support:support-v4:27.0.1"
}
Open the dts-generator folder in your terminal
Run the following command:
./gradlew extractAllJars
The command above will get the needed jar files for your dependency and will output them in the dts-generator/jar-files folder (or you can optionaly pass another output folder -PjarsOutput=another-folder
)
You can run the following command to check what are the dependencies between the packages:
./gradlew dependencies --configuration testCompileOnly
Run the dts-generator tool passing as input arguments the path to the output jars folder
To get all the jar files for androidx follow the steps above. You can find the jar files for androidx 1.0.0 in the current repository As androidx needs the base android jar file to create its typings you need to pass the android.jar file as a super parameter to the generator. To avoid having typings for every different API level you can reuse typings built with API level 17 for all API levels until 23. It's quite easy to test this:
/// <reference path="android-17.d.ts"/>
at the top of the generated typings file where android-17.d.ts is the typings file of the android API level 17tsc
passing the generated typings file and there shouldn't be errorstsc
execution completes with no errorBy repeating the steps above we've found that:
The corresponding typings files can be found in the tns-platform-declarations package. The repo's Makefile can be used as a reference for creating these typings files