Skip to content

Commit dea1bf3

Browse files
[jni] Do not fail flutter build on desktop if jdk is unavailable (#2794)
1 parent 5501355 commit dea1bf3

File tree

7 files changed

+129
-48
lines changed

7 files changed

+129
-48
lines changed

.github/workflows/jnigen.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,62 @@ jobs:
263263
- run: dart run jni:setup
264264
- run: dart test --test-randomize-ordering-seed random
265265

266+
# Not having Java installed on a Desktop will not fail Flutter build.
267+
test_no_jdk_fallback:
268+
runs-on: ${{ matrix.os }}
269+
defaults:
270+
run:
271+
working-directory: ./pkgs/jni
272+
strategy:
273+
fail-fast: false
274+
matrix:
275+
os: [ubuntu-latest, windows-latest]
276+
include:
277+
- os: ubuntu-latest
278+
target: linux
279+
- os: windows-latest
280+
target: windows
281+
282+
steps:
283+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
284+
- uses: subosito/flutter-action@fd55f4c5af5b953cc57a2be44cb082c8f6635e8e
285+
with:
286+
channel: 'stable'
287+
cache: true
288+
cache-key: 'flutter-:os:-:channel:-:version:-:arch:-:hash:'
289+
290+
- name: Create Test App Directory
291+
run: mkdir test_app
292+
293+
- name: Create New Flutter App
294+
run: flutter create .
295+
working-directory: ./pkgs/jni/test_app
296+
297+
- name: Add JNI Plugin (using relative path to link local source)
298+
run: flutter pub add jni --path ../
299+
working-directory: ./pkgs/jni/test_app
300+
301+
- name: Clear JAVA_HOME to Simulate Missing JDK
302+
run: echo "JAVA_HOME=" >> $GITHUB_ENV
303+
shell: bash
304+
305+
- name: Install Linux Build Dependencies and Enable Desktop (Ubuntu)
306+
if: matrix.os == 'ubuntu-latest'
307+
run: |
308+
sudo apt-get update -y
309+
sudo apt-get install -y ninja-build libgtk-3-dev
310+
flutter config --enable-linux-desktop
311+
working-directory: ./pkgs/jni/test_app
312+
313+
- name: Enable Windows Desktop (Windows)
314+
if: matrix.os == 'windows-latest'
315+
run: flutter config --enable-windows-desktop
316+
working-directory: ./pkgs/jni/test_app
317+
318+
- name: Run Build (Must succeed without JDK installed)
319+
run: flutter build ${{ matrix.target }}
320+
working-directory: ./pkgs/jni/test_app
321+
266322
test_jnigen_windows_minimal:
267323
needs: [analyze_jnigen]
268324
runs-on: windows-latest

pkgs/jni/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.15.2
2+
3+
- Do not fail `flutter build` if JDK is not found for desktop.
4+
15
## 0.15.1
26

37
- **Breaking Change**: Removed the `engineId` argument from

pkgs/jni/bin/setup.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ void main(List<String> arguments) async {
270270
final tempDir = await jniDir.createTemp('jni_native_build_');
271271
final cmakeArgs = <String>[];
272272
cmakeArgs.addAll(options.cmakeArgs);
273+
274+
// Force JNI to be required for standalone builds
275+
cmakeArgs.add('-DREQUIRE_JNI=ON');
276+
273277
// Pass absolute path of srcDir because cmake command is run in temp dir
274278
cmakeArgs.add(srcDir.absolute.path);
275279
await runCommand('cmake', cmakeArgs, tempDir.path);

pkgs/jni/linux/CMakeLists.txt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
# the plugin to fail to compile for some customers of the plugin.
44
cmake_minimum_required(VERSION 3.10)
55

6-
# Project-level configuration.
76
set(PROJECT_NAME "jni")
87
project(${PROJECT_NAME} LANGUAGES CXX)
98

10-
# Invoke the build for native code shared with the other target platforms.
11-
# This can be changed to accomodate different builds.
129
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../src" "${CMAKE_CURRENT_BINARY_DIR}/shared")
1310

14-
# List of absolute paths to libraries that should be bundled with the plugin.
15-
# This list could contain prebuilt libraries, or libraries created by an
16-
# external build triggered from this build file.
17-
set(jni_bundled_libraries
18-
# Defined in ../src/CMakeLists.txt.
19-
# This can be changed to accomodate different builds.
20-
$<TARGET_FILE:jni>
21-
PARENT_SCOPE
22-
)
11+
if (TARGET jni)
12+
set(jni_bundled_libraries
13+
$<TARGET_FILE:jni>
14+
PARENT_SCOPE
15+
)
16+
else()
17+
set(jni_bundled_libraries
18+
""
19+
PARENT_SCOPE
20+
)
21+
endif()

pkgs/jni/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
name: jni
66
description: A library to access JNI from Dart and Flutter that acts as a support library for package:jnigen.
7-
version: 0.15.1
7+
version: 0.15.2
88
repository: https://github.com/dart-lang/native/tree/main/pkgs/jni
99
issue_tracker: https://github.com/dart-lang/native/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Ajni
1010

pkgs/jni/src/CMakeLists.txt

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,48 @@ cmake_minimum_required(VERSION 3.10)
77

88
project(jni_library VERSION 0.0.1 LANGUAGES C)
99

10-
add_library(jni SHARED
11-
"dartjni.c"
12-
"third_party/global_jni_env.c"
13-
"include/dart_api_dl.c"
14-
)
15-
16-
set_target_properties(jni PROPERTIES
17-
PUBLIC_HEADER dartjni.h
18-
OUTPUT_NAME "dartjni"
19-
)
20-
21-
target_compile_definitions(jni PUBLIC DART_SHARED_LIB)
22-
23-
if(WIN32)
24-
set_target_properties(${TARGET_NAME} PROPERTIES
25-
LINK_FLAGS "/DELAYLOAD:jvm.dll")
26-
endif()
27-
10+
set(JNI_AVAILABLE FALSE)
2811
if (ANDROID)
29-
target_link_libraries(jni log)
30-
target_link_options(jni PRIVATE "-Wl,-z,max-page-size=16384")
12+
set(JNI_AVAILABLE TRUE)
3113
else()
32-
find_package(JNI REQUIRED COMPONENTS JVM)
33-
include_directories(${JNI_INCLUDE_DIRS})
34-
target_link_libraries(jni ${JNI_LIBRARIES})
14+
if (REQUIRE_JNI)
15+
# Standalone Dart Build: Fail strictly if JNI is missing
16+
find_package(JNI REQUIRED COMPONENTS JVM)
17+
set(JNI_AVAILABLE TRUE)
18+
else()
19+
# Flutter Plugin Build: Try to find JNI, but don't fail if missing
20+
find_package(JNI COMPONENTS JVM)
21+
if (JNI_FOUND)
22+
set(JNI_AVAILABLE TRUE)
23+
endif()
24+
endif()
25+
endif()
26+
27+
if (JNI_AVAILABLE)
28+
add_library(jni SHARED
29+
"dartjni.c"
30+
"third_party/global_jni_env.c"
31+
"include/dart_api_dl.c"
32+
)
33+
34+
set_target_properties(jni PROPERTIES
35+
PUBLIC_HEADER dartjni.h
36+
OUTPUT_NAME "dartjni"
37+
)
38+
39+
target_compile_definitions(jni PUBLIC DART_SHARED_LIB)
40+
41+
if(WIN32)
42+
set_target_properties(${TARGET_NAME} PROPERTIES
43+
LINK_FLAGS "/DELAYLOAD:jvm.dll")
44+
endif()
45+
46+
if (ANDROID)
47+
target_link_libraries(jni log)
48+
target_link_options(jni PRIVATE "-Wl,-z,max-page-size=16384")
49+
else()
50+
find_package(JNI REQUIRED COMPONENTS JVM)
51+
include_directories(${JNI_INCLUDE_DIRS})
52+
target_link_libraries(jni ${JNI_LIBRARIES})
53+
endif()
3554
endif()

pkgs/jni/windows/CMakeLists.txt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@
44
# customers of the plugin.
55
cmake_minimum_required(VERSION 3.14)
66

7-
# Project-level configuration.
87
set(PROJECT_NAME "jni")
98
project(${PROJECT_NAME} LANGUAGES CXX)
109

11-
# Invoke the build for native code shared with the other target platforms.
12-
# This can be changed to accomodate different builds.
1310
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../src" "${CMAKE_CURRENT_BINARY_DIR}/shared")
1411

15-
# List of absolute paths to libraries that should be bundled with the plugin.
16-
# This list could contain prebuilt libraries, or libraries created by an
17-
# external build triggered from this build file.
18-
set(jni_bundled_libraries
19-
# Defined in ../src/CMakeLists.txt.
20-
# This can be changed to accomodate different builds.
21-
$<TARGET_FILE:jni>
22-
PARENT_SCOPE
23-
)
12+
if (TARGET jni)
13+
set(jni_bundled_libraries
14+
$<TARGET_FILE:jni>
15+
PARENT_SCOPE
16+
)
17+
else()
18+
set(jni_bundled_libraries
19+
""
20+
PARENT_SCOPE
21+
)
22+
endif()

0 commit comments

Comments
 (0)