Skip to content

Commit 9fa8b1c

Browse files
committed
Add Android support to nativeapi example
Introduces Android platform files for the nativeapi example, including Gradle build scripts, manifests, Kotlin MainActivity, resource files, and launcher icons. Updates cnativeapi to enable ffiPlugin for Android and adjusts CMakeLists.txt for Android-specific sources and linking.
1 parent b99cab4 commit 9fa8b1c

23 files changed

Lines changed: 258 additions & 21 deletions

File tree

packages/cnativeapi/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ dev_dependencies:
2222
flutter:
2323
plugin:
2424
platforms:
25+
android:
26+
ffiPlugin: true
2527
linux:
2628
ffiPlugin: true
2729
macos:

packages/cnativeapi/src/CMakeLists.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,39 @@ set(LIBNATIVEAPI_INCLUDE_DIR "${LIBNATIVEAPI_ROOT}/include")
1818
file(GLOB COMMON_SOURCES "${LIBNATIVEAPI_SRC_DIR}/*.cpp" "${LIBNATIVEAPI_SRC_DIR}/foundation/*.cpp")
1919
list(FILTER COMMON_SOURCES EXCLUDE REGEX "${LIBNATIVEAPI_SRC_DIR}/platform/*")
2020

21-
# Foundation source files
22-
file(GLOB FOUNDATION_SOURCES "${LIBNATIVEAPI_SRC_DIR}/foundation/*.cpp")
23-
2421
# C API source files
2522
file(GLOB CAPI_SOURCES
2623
"${LIBNATIVEAPI_SRC_DIR}/capi/*.cpp"
2724
"${LIBNATIVEAPI_SRC_DIR}/capi/*.c"
2825
)
2926

3027
# Platform-specific source files
31-
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
28+
if(ANDROID)
29+
file(GLOB PLATFORM_SOURCES "${LIBNATIVEAPI_SRC_DIR}/platform/android/*.cpp")
30+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
3231
file(GLOB PLATFORM_SOURCES "${LIBNATIVEAPI_SRC_DIR}/platform/linux/*.cpp")
3332
# Find packages for Linux
3433
find_package(PkgConfig REQUIRED)
3534
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
3635
pkg_check_modules(X11 REQUIRED IMPORTED_TARGET x11)
3736
pkg_check_modules(XI REQUIRED IMPORTED_TARGET xi)
3837
pkg_check_modules(AYATANA_APPINDICATOR REQUIRED IMPORTED_TARGET ayatana-appindicator3-0.1)
39-
elseif (WIN32)
40-
file(GLOB PLATFORM_SOURCES "${LIBNATIVEAPI_SRC_DIR}/platform/windows/*.cpp")
4138
elseif (APPLE)
4239
file(GLOB PLATFORM_SOURCES "${LIBNATIVEAPI_SRC_DIR}/platform/macos/*.mm")
4340
# Find required frameworks for macOS
4441
find_library(COCOA_FRAMEWORK Cocoa)
4542
find_library(CARBON_FRAMEWORK Carbon)
4643
find_library(COREGRAPHICS_FRAMEWORK CoreGraphics)
4744
find_library(APPLICATIONSERVICES_FRAMEWORK ApplicationServices)
45+
elseif (WIN32)
46+
file(GLOB PLATFORM_SOURCES "${LIBNATIVEAPI_SRC_DIR}/platform/windows/*.cpp")
4847
else ()
4948
set(PLATFORM_SOURCES "")
5049
endif ()
5150

5251
# Add library target
5352
add_library(cnativeapi SHARED
5453
${COMMON_SOURCES}
55-
${FOUNDATION_SOURCES}
5654
${PLATFORM_SOURCES}
5755
${CAPI_SOURCES}
5856
)
@@ -73,7 +71,9 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
7371
endif ()
7472

7573
# Link required frameworks and libraries
76-
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
74+
if(ANDROID)
75+
target_link_libraries(cnativeapi PUBLIC log android)
76+
elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux")
7777
target_link_libraries(cnativeapi PUBLIC
7878
PkgConfig::GTK
7979
PkgConfig::X11

packages/nativeapi/example/.metadata

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This file should be version controlled and should not be manually edited.
55

66
version:
7-
revision: "d693b4b9dbac2acd4477aea4555ca6dcbea44ba2"
7+
revision: "9f455d2486bcb28cad87b062475f42edc959f636"
88
channel: "stable"
99

1010
project_type: app
@@ -13,17 +13,11 @@ project_type: app
1313
migration:
1414
platforms:
1515
- platform: root
16-
create_revision: d693b4b9dbac2acd4477aea4555ca6dcbea44ba2
17-
base_revision: d693b4b9dbac2acd4477aea4555ca6dcbea44ba2
18-
- platform: linux
19-
create_revision: d693b4b9dbac2acd4477aea4555ca6dcbea44ba2
20-
base_revision: d693b4b9dbac2acd4477aea4555ca6dcbea44ba2
21-
- platform: macos
22-
create_revision: d693b4b9dbac2acd4477aea4555ca6dcbea44ba2
23-
base_revision: d693b4b9dbac2acd4477aea4555ca6dcbea44ba2
24-
- platform: windows
25-
create_revision: d693b4b9dbac2acd4477aea4555ca6dcbea44ba2
26-
base_revision: d693b4b9dbac2acd4477aea4555ca6dcbea44ba2
16+
create_revision: 9f455d2486bcb28cad87b062475f42edc959f636
17+
base_revision: 9f455d2486bcb28cad87b062475f42edc959f636
18+
- platform: android
19+
create_revision: 9f455d2486bcb28cad87b062475f42edc959f636
20+
base_revision: 9f455d2486bcb28cad87b062475f42edc959f636
2721

2822
# User provided section
2923

@@ -33,3 +27,4 @@ migration:
3327
# Files that are not part of the templates will be ignored by default.
3428
unmanaged_files:
3529
- 'lib/main.dart'
30+
- 'ios/Runner.xcodeproj/project.pbxproj'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
gradle-wrapper.jar
2+
/.gradle
3+
/captures/
4+
/gradlew
5+
/gradlew.bat
6+
/local.properties
7+
GeneratedPluginRegistrant.java
8+
.cxx/
9+
10+
# Remember to never publicly share your keystore.
11+
# See https://flutter.dev/to/reference-keystore
12+
key.properties
13+
**/*.keystore
14+
**/*.jks
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
plugins {
2+
id("com.android.application")
3+
id("kotlin-android")
4+
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
5+
id("dev.flutter.flutter-gradle-plugin")
6+
}
7+
8+
android {
9+
namespace = "com.example.nativeapi_example"
10+
compileSdk = flutter.compileSdkVersion
11+
ndkVersion = flutter.ndkVersion
12+
13+
compileOptions {
14+
sourceCompatibility = JavaVersion.VERSION_11
15+
targetCompatibility = JavaVersion.VERSION_11
16+
}
17+
18+
kotlinOptions {
19+
jvmTarget = JavaVersion.VERSION_11.toString()
20+
}
21+
22+
defaultConfig {
23+
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
24+
applicationId = "com.example.nativeapi_example"
25+
// You can update the following values to match your application needs.
26+
// For more information, see: https://flutter.dev/to/review-gradle-config.
27+
minSdk = flutter.minSdkVersion
28+
targetSdk = flutter.targetSdkVersion
29+
versionCode = flutter.versionCode
30+
versionName = flutter.versionName
31+
}
32+
33+
buildTypes {
34+
release {
35+
// TODO: Add your own signing config for the release build.
36+
// Signing with the debug keys for now, so `flutter run --release` works.
37+
signingConfig = signingConfigs.getByName("debug")
38+
}
39+
}
40+
}
41+
42+
flutter {
43+
source = "../.."
44+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
<!-- The INTERNET permission is required for development. Specifically,
3+
the Flutter tool needs it to communicate with the running application
4+
to allow setting breakpoints, to provide hot reload, etc.
5+
-->
6+
<uses-permission android:name="android.permission.INTERNET"/>
7+
</manifest>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
2+
<application
3+
android:label="nativeapi_example"
4+
android:name="${applicationName}"
5+
android:icon="@mipmap/ic_launcher">
6+
<activity
7+
android:name=".MainActivity"
8+
android:exported="true"
9+
android:launchMode="singleTop"
10+
android:taskAffinity=""
11+
android:theme="@style/LaunchTheme"
12+
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
13+
android:hardwareAccelerated="true"
14+
android:windowSoftInputMode="adjustResize">
15+
<!-- Specifies an Android theme to apply to this Activity as soon as
16+
the Android process has started. This theme is visible to the user
17+
while the Flutter UI initializes. After that, this theme continues
18+
to determine the Window background behind the Flutter UI. -->
19+
<meta-data
20+
android:name="io.flutter.embedding.android.NormalTheme"
21+
android:resource="@style/NormalTheme"
22+
/>
23+
<intent-filter>
24+
<action android:name="android.intent.action.MAIN"/>
25+
<category android:name="android.intent.category.LAUNCHER"/>
26+
</intent-filter>
27+
</activity>
28+
<!-- Don't delete the meta-data below.
29+
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
30+
<meta-data
31+
android:name="flutterEmbedding"
32+
android:value="2" />
33+
</application>
34+
<!-- Required to query activities that can process text, see:
35+
https://developer.android.com/training/package-visibility and
36+
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
37+
38+
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
39+
<queries>
40+
<intent>
41+
<action android:name="android.intent.action.PROCESS_TEXT"/>
42+
<data android:mimeType="text/plain"/>
43+
</intent>
44+
</queries>
45+
</manifest>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.example.nativeapi_example
2+
3+
import io.flutter.embedding.android.FlutterActivity
4+
5+
class MainActivity : FlutterActivity()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Modify this file to customize your launch splash screen -->
3+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
4+
<item android:drawable="?android:colorBackground" />
5+
6+
<!-- You can insert your own image assets here -->
7+
<!-- <item>
8+
<bitmap
9+
android:gravity="center"
10+
android:src="@mipmap/launch_image" />
11+
</item> -->
12+
</layer-list>

0 commit comments

Comments
 (0)