@@ -2,6 +2,7 @@ use flutter_winit::FlutterWindow;
2
2
use glutin:: window:: WindowBuilder ;
3
3
use std:: path:: Path ;
4
4
5
+ #[ cfg( not( target_os = "android" ) ) ]
5
6
fn main ( ) {
6
7
env_logger:: init ( ) ;
7
8
@@ -23,3 +24,69 @@ fn main() {
23
24
24
25
flutter. run ( ) ;
25
26
}
27
+
28
+ #[ cfg( target_os = "android" ) ]
29
+ fn main ( ) {
30
+ use android_logger:: Config ;
31
+ use android_ndk:: android_app:: AndroidApp ;
32
+ use jni_android_sys:: android:: content:: Context ;
33
+ use log:: Level ;
34
+ use std:: ffi:: OsStr ;
35
+ use std:: os:: unix:: ffi:: OsStrExt ;
36
+ use std:: path:: PathBuf ;
37
+
38
+ android_logger:: init_once (
39
+ Config :: default ( )
40
+ . with_min_level ( Level :: Debug )
41
+ . with_tag ( "flutter_app_template" ) ,
42
+ ) ;
43
+
44
+ let android_app = unsafe { AndroidApp :: from_ptr ( android_glue:: get_android_app ( ) ) } ;
45
+
46
+ let assets_dir = PathBuf :: from ( OsStr :: from_bytes (
47
+ android_app. activity ( ) . internal_data_path ( ) . to_bytes ( ) ,
48
+ ) ) ;
49
+
50
+ let mut args = Vec :: with_capacity ( 1 ) ;
51
+
52
+ let vm = unsafe { jni_glue:: VM :: from_jni_local ( & * android_app. activity ( ) . vm ( ) ) } ;
53
+
54
+ let snapshot = vm. with_env ( |env| {
55
+ let context = Context :: new ( env) . unwrap ( ) ;
56
+ let info = context. getApplicationInfo ( ) . unwrap ( ) . unwrap ( ) ;
57
+ let lib_dir = info. nativeLibraryDir ( ) . unwrap ( ) . to_string ( ) . unwrap ( ) ;
58
+ Path :: new ( & lib_dir) . join ( "app.so" )
59
+ } ) ;
60
+
61
+ if snapshot. exists ( ) {
62
+ args. push ( format ! ( "--aot-shared-library-name={}" , snapshot. display( ) ) ) ;
63
+ } else {
64
+ std:: fs:: create_dir_all ( & assets_dir) . unwrap ( ) ;
65
+ copy_asset (
66
+ & android_app. activity ( ) . asset_manager ( ) ,
67
+ b"kernel_blob.bin\0 " ,
68
+ & assets_dir. join ( "kernel_blob.bin" ) ,
69
+ ) ;
70
+ }
71
+
72
+ let window = WindowBuilder :: new ( ) . with_title ( "Flutter App Demo" ) ;
73
+ let flutter = FlutterWindow :: new ( window) . unwrap ( ) ;
74
+
75
+ flutter. start_engine ( Path :: new ( & assets_dir) , & args) . unwrap ( ) ;
76
+
77
+ flutter. run ( ) ;
78
+ }
79
+
80
+ #[ cfg( target_os = "android" ) ]
81
+ fn copy_asset ( asset_manager : & android_ndk:: asset:: AssetManager , asset : & [ u8 ] , path : & Path ) {
82
+ use std:: ffi:: CStr ;
83
+ use std:: io:: Read ;
84
+
85
+ let bytes: Vec < u8 > = asset_manager
86
+ . open ( CStr :: from_bytes_with_nul ( asset) . unwrap ( ) )
87
+ . unwrap ( )
88
+ . bytes ( )
89
+ . collect :: < Result < _ , _ > > ( )
90
+ . unwrap ( ) ;
91
+ std:: fs:: write ( path, bytes) . unwrap ( ) ;
92
+ }
0 commit comments