Skip to content

Commit ebbe762

Browse files
committed
feat: Android litert version bump and 16KB page support
1 parent eec86aa commit ebbe762

File tree

5 files changed

+70
-18
lines changed

5 files changed

+70
-18
lines changed

android/build.gradle

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,25 +127,77 @@ dependencies {
127127
implementation "com.facebook.react:react-native:+"
128128

129129
// Tensorflow Lite .aar (includes C API via prefabs)
130-
implementation "com.google.ai.edge.litert:litert:1.0.1"
131-
extractSO("com.google.ai.edge.litert:litert:1.0.1")
132-
extractHeaders("com.google.ai.edge.litert:litert:1.0.1")
130+
implementation "com.google.ai.edge.litert:litert:1.4.0"
131+
extractSO("com.google.ai.edge.litert:litert:1.4.0")
132+
extractHeaders("com.google.ai.edge.litert:litert:1.4.0")
133133

134134
// Tensorflow Lite GPU delegate
135-
implementation "com.google.ai.edge.litert:litert-gpu:1.0.1"
136-
extractSO("com.google.ai.edge.litert:litert-gpu:1.0.1")
137-
extractHeaders("com.google.ai.edge.litert:litert-gpu:1.0.1")
135+
implementation "com.google.ai.edge.litert:litert-gpu:1.4.0"
136+
extractSO("com.google.ai.edge.litert:litert-gpu:1.4.0")
137+
extractHeaders("com.google.ai.edge.litert:litert-gpu:1.4.0")
138138
}
139139

140+
/**
141+
* Custom task to delete the 'src/main/cpp/lib/headers' directory.
142+
*/
143+
task cleanEmptyDirectories(type: Delete) {
144+
// Specify the directory to be deleted.
145+
delete 'src/main/cpp/lib/headers'
146+
delete 'src/main/cpp/lib/res'
147+
}
148+
149+
/**
150+
* Custom task to copy C++ header files (.h) from AAR packages.
151+
* This task iterates through each AAR file in the 'extractHeaders' configuration.
152+
* For each AAR, it extracts and copies header files to specific destinations:
153+
* - Headers found under 'external/org_tensorflow/tensorflow/' inside the AAR
154+
* are copied to 'src/main/cpp/lib/tensorflow/'.
155+
* - All other headers are copied to 'src/main/cpp/lib/{packageName}/', where
156+
* '{packageName}' is derived from the AAR's filename.
157+
*/
140158
task extractAARHeaders {
159+
160+
finalizedBy cleanEmptyDirectories
161+
141162
doLast {
142-
configurations.extractHeaders.files.each {
143-
def file = it.absoluteFile
144-
def packageName = file.name.tokenize('-')[0]
163+
// Iterate over each AAR file defined in the 'extractHeaders' configuration
164+
configurations.extractHeaders.files.each { aarFile ->
165+
// Extract the package name from the AAR filename (e.g., 'my-lib-1.0.aar' -> 'my-lib')
166+
def packageName = aarFile.name.tokenize('-')[0]
167+
168+
// Create a copy operation for the current AAR
145169
copy {
146-
from zipTree(file)
147-
into "src/main/cpp/lib/$packageName/"
148-
include "**/*.h"
170+
// Source: the contents of the AAR treated as a zip archive
171+
from zipTree(aarFile)
172+
// Base destination directory for all copied files from this AAR.
173+
// The 'eachFile' closure will then modify the relative path within this base.
174+
into "src/main/cpp/lib/"
175+
include "**/*.h" // Ensure only header files are included
176+
177+
// Process each file found within the AAR's zipTree
178+
eachFile { fileCopyDetails ->
179+
// Check if the current file is a C++ header file (.h)
180+
if (fileCopyDetails.name.endsWith(".h")) {
181+
// Get the original relative path of the file within the AAR
182+
def originalRelativePath = fileCopyDetails.relativePath.toString()
183+
184+
// Check if the header belongs to the special TensorFlow path
185+
if (originalRelativePath.startsWith("headers/external/org_tensorflow/tensorflow/")) {
186+
// For TensorFlow headers, set the new relative path to start with 'tensorflow/'
187+
// and remove the 'external/org_tensorflow/' prefix.
188+
def newRelativePath = packageName + "/headers/" + originalRelativePath.substring("headers/external/org_tensorflow/".length())
189+
fileCopyDetails.relativePath = new RelativePath(true, newRelativePath.split('/'))
190+
} else {
191+
// For all other headers, set the new relative path to include the 'packageName'
192+
// derived from the AAR filename, followed by its original path.
193+
def newRelativePath = packageName + "/" + originalRelativePath
194+
fileCopyDetails.relativePath = new RelativePath(true, newRelativePath.split('/'))
195+
}
196+
} else {
197+
// If the file is not a .h file, exclude it from the copy operation
198+
fileCopyDetails.exclude()
199+
}
200+
}
149201
}
150202
}
151203
}

cpp/TensorHelpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "TensorHelpers.h"
1010

1111
#ifdef ANDROID
12-
#include <tensorflow/lite/c/c_api.h>
12+
#include <tflite/c/c_api.h>
1313
#else
1414
#include <TensorFlowLiteC/TensorFlowLiteC.h>
1515
#endif

cpp/TensorHelpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <jsi/jsi.h>
1313

1414
#ifdef ANDROID
15-
#include <tensorflow/lite/c/c_api.h>
15+
#include <tflite/c/c_api.h>
1616
#else
1717
#include <TensorFlowLiteC/TensorFlowLiteC.h>
1818
#endif

cpp/TensorflowPlugin.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
#include <thread>
1919

2020
#ifdef ANDROID
21-
#include <tensorflow/lite/c/c_api.h>
22-
#include <tensorflow/lite/delegates/gpu/delegate.h>
23-
#include <tensorflow/lite/delegates/nnapi/nnapi_delegate_c_api.h>
21+
#include <tflite/c/c_api.h>
22+
#include <tflite/delegates/gpu/delegate.h>
23+
#include <tflite/delegates/nnapi/nnapi_delegate_c_api.h>
2424
#else
2525
#include <TensorFlowLiteC/TensorFlowLiteC.h>
2626

cpp/TensorflowPlugin.h

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

1717
#ifdef ANDROID
1818
#include <ReactCommon/CallInvoker.h>
19-
#include <tensorflow/lite/c/c_api.h>
19+
#include <tflite/c/c_api.h>
2020
#else
2121
#include <React-callinvoker/ReactCommon/CallInvoker.h>
2222
#include <TensorFlowLiteC/TensorFlowLiteC.h>

0 commit comments

Comments
 (0)