-
Notifications
You must be signed in to change notification settings - Fork 81
[jnigen] migrate in_app_java example to Dart API #2671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+89
−78
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,31 @@ | ||
# In-App Java Example | ||
|
||
This example shows how to write custom java code in `android/app/src` and call | ||
it using `jnigen` generated bindings. | ||
it using bindings generated by `jnigen`. | ||
|
||
#### How to run this example: | ||
### How to run this example: | ||
|
||
- Run `flutter run` to run the app. | ||
|
||
- To regenerate bindings after changing Java code, run | ||
`flutter pub run jnigen --config jnigen.yaml`. This requires at least one APK | ||
build to have been run before, so that it's possible for `jnigen` to obtain | ||
classpaths of Android Gradle libraries. Therefore, once run | ||
`flutter build apk` before generating bindings for the first time, or after a | ||
`flutter clean`. | ||
`dart run tool/jnigen.dart`. This requires at least one APK build to have | ||
been run before, so that `jnigen` can obtain classpaths of Android Gradle | ||
libraries. Therefore, run `flutter build apk` once before generating bindings | ||
for the first time, or after a `flutter clean`. | ||
|
||
#### General steps | ||
### General steps | ||
|
||
These are general steps to integrate Java code into a flutter project using | ||
These are general steps to integrate Java code into a Flutter project using | ||
`jnigen`. | ||
|
||
- Write Java code in suitable package folder, under `android/` subproject of the | ||
flutter app. | ||
- Create A jnigen config like `jnigen.yaml` in this example. | ||
- Generate bindings using jnigen config. | ||
- Add proguard rules to exclude your custom classes from tree shaking, since | ||
they are always accessed reflectively in JNI. | ||
- Place Java code in a suitable package folder under the `android/` subproject | ||
of the flutter app. | ||
- Create a `jnigen` configuration script like `tool/jnigen.dart` in this | ||
example. | ||
- Generate bindings by running that script with `dart run`. | ||
- To prevent tree shaking of your custom classes (which are always accessed | ||
reflectively in JNI) use either the `@Keep` annotation | ||
(`androidx.annotation.Keep`) for code written in the application itself, or | ||
a [proguard-rules file](https://github.com/dart-lang/native/blob/main/pkgs/jnigen/example/in_app_java/android/app/proguard-rules.pro) | ||
for external libraries. | ||
- Build and run the app. |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:jnigen/jnigen.dart'; | ||
|
||
void main(List<String> args) { | ||
final packageRoot = Platform.script.resolve('../'); | ||
generateJniBindings( | ||
Config( | ||
outputConfig: OutputConfig( | ||
dartConfig: DartCodeOutputConfig( | ||
path: packageRoot.resolve('lib/android_utils.g.dart'), | ||
structure: OutputStructure.singleFile, | ||
), | ||
), | ||
androidSdkConfig: AndroidSdkConfig(addGradleDeps: true), | ||
sourcePath: [packageRoot.resolve('android/app/src/main/java')], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: sourcePath is singular, but the type is a list. |
||
classes: [ | ||
'com.example.in_app_java', // Generate the entire package | ||
'androidx.emoji2.text.EmojiCompat', // From gradle's compile classpath | ||
'androidx.emoji2.text.DefaultEmojiCompatConfig', // From gradle's compile classpath | ||
'android.os.Build', // from gradle's compile classpath | ||
'java.util.HashMap', // from gradle's compile classpath | ||
], | ||
), | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How often do we pass this or not? Are we passing this to 90% of our use cases? If yes, should this be the default? And if it's the default, does that require there always is a Gradle project configured? How is this Gradle project configured, always via
flutter create
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You either can download and pass the jars or use this option, I don't have data to say 90% or 80%.
Always via
flutter create
, yes.