Read assets out of APK on Android (flutter/engine#4742)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
source_set("assets") {
|
||||
sources = [
|
||||
"asset_provider.h",
|
||||
"directory_asset_bundle.cc",
|
||||
"directory_asset_bundle.h",
|
||||
"unzipper_provider.cc",
|
||||
|
||||
25
engine/src/flutter/assets/asset_provider.h
Normal file
25
engine/src/flutter/assets/asset_provider.h
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_ASSETS_ASSET_PROVIDER_H_
|
||||
#define FLUTTER_ASSETS_ASSET_PROVIDER_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "lib/fxl/memory/ref_counted.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class AssetProvider
|
||||
: public fxl::RefCountedThreadSafe<AssetProvider>
|
||||
{
|
||||
public:
|
||||
virtual bool GetAsBuffer(const std::string& asset_name,
|
||||
std::vector<uint8_t>* data) = 0;
|
||||
virtual ~AssetProvider() = default;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
#endif // FLUTTER_ASSETS_ASSET_PROVIDER_H
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "flutter/assets/asset_provider.h"
|
||||
#include "lib/fxl/files/unique_fd.h"
|
||||
#include "lib/fxl/macros.h"
|
||||
#include "lib/fxl/memory/ref_counted.h"
|
||||
@@ -15,14 +16,14 @@
|
||||
namespace blink {
|
||||
|
||||
class DirectoryAssetBundle
|
||||
: public fxl::RefCountedThreadSafe<DirectoryAssetBundle> {
|
||||
: public AssetProvider {
|
||||
public:
|
||||
explicit DirectoryAssetBundle(std::string directory);
|
||||
// Expects fd to be valid, otherwise the file descriptor is ignored.
|
||||
explicit DirectoryAssetBundle(fxl::UniqueFD fd);
|
||||
~DirectoryAssetBundle();
|
||||
virtual ~DirectoryAssetBundle();
|
||||
|
||||
bool GetAsBuffer(const std::string& asset_name, std::vector<uint8_t>* data);
|
||||
virtual bool GetAsBuffer(const std::string& asset_name, std::vector<uint8_t>* data);
|
||||
|
||||
std::string GetPathForAsset(const std::string& asset_name);
|
||||
|
||||
|
||||
@@ -34,15 +34,15 @@ std::shared_ptr<txt::FontCollection> FontCollection::GetFontCollection() const {
|
||||
return collection_;
|
||||
}
|
||||
|
||||
void FontCollection::RegisterFontsFromDirectoryAssetBundle(
|
||||
fxl::RefPtr<blink::DirectoryAssetBundle> directory_asset_bundle) {
|
||||
if (!directory_asset_bundle) {
|
||||
void FontCollection::RegisterFontsFromAssetProvider(
|
||||
fxl::RefPtr<blink::AssetProvider> asset_provider) {
|
||||
|
||||
if (!asset_provider){
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> manifest_data;
|
||||
if (!directory_asset_bundle->GetAsBuffer("FontManifest.json",
|
||||
&manifest_data)) {
|
||||
if (!asset_provider->GetAsBuffer("FontManifest.json", &manifest_data)) {
|
||||
FXL_DLOG(WARNING) << "Could not find the font manifest in the asset store.";
|
||||
return;
|
||||
}
|
||||
@@ -92,8 +92,8 @@ void FontCollection::RegisterFontsFromDirectoryAssetBundle(
|
||||
|
||||
// TODO: Handle weights and styles.
|
||||
std::vector<uint8_t> font_data;
|
||||
if (directory_asset_bundle->GetAsBuffer(font_asset->value.GetString(),
|
||||
&font_data)) {
|
||||
if (asset_provider->GetAsBuffer(font_asset->value.GetString(),
|
||||
&font_data)) {
|
||||
// The data must be copied because it needs to be moved into the
|
||||
// typeface as a stream.
|
||||
auto data =
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "flutter/assets/directory_asset_bundle.h"
|
||||
#include "flutter/assets/asset_provider.h"
|
||||
#include "lib/fxl/macros.h"
|
||||
#include "lib/fxl/memory/ref_ptr.h"
|
||||
#include "txt/asset_data_provider.h"
|
||||
@@ -21,9 +21,7 @@ class FontCollection {
|
||||
|
||||
std::shared_ptr<txt::FontCollection> GetFontCollection() const;
|
||||
|
||||
void RegisterFontsFromDirectoryAssetBundle(
|
||||
fxl::RefPtr<blink::DirectoryAssetBundle> directory_asset_bundle);
|
||||
|
||||
void RegisterFontsFromAssetProvider(fxl::RefPtr<blink::AssetProvider> asset_provider);
|
||||
void RegisterTestFonts();
|
||||
|
||||
private:
|
||||
|
||||
@@ -81,9 +81,9 @@ struct FontMatcher {
|
||||
} // namespace
|
||||
|
||||
void AssetFontSelector::Install(
|
||||
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle) {
|
||||
fxl::RefPtr<AssetProvider> asset_provider) {
|
||||
RefPtr<AssetFontSelector> font_selector =
|
||||
adoptRef(new AssetFontSelector(std::move(directory_asset_bundle)));
|
||||
adoptRef(new AssetFontSelector(std::move(asset_provider)));
|
||||
font_selector->parseFontManifest();
|
||||
UIDartState::Current()->set_font_selector(font_selector);
|
||||
}
|
||||
@@ -96,8 +96,8 @@ void AssetFontSelector::Install(fxl::RefPtr<ZipAssetStore> asset_store) {
|
||||
}
|
||||
|
||||
AssetFontSelector::AssetFontSelector(
|
||||
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle)
|
||||
: directory_asset_bundle_(std::move(directory_asset_bundle)) {}
|
||||
fxl::RefPtr<AssetProvider> asset_provider)
|
||||
: asset_provider_(std::move(asset_provider)) {}
|
||||
|
||||
AssetFontSelector::AssetFontSelector(fxl::RefPtr<ZipAssetStore> asset_store)
|
||||
: asset_store_(std::move(asset_store)) {}
|
||||
@@ -118,9 +118,9 @@ AssetFontSelector::FlutterFontAttributes::~FlutterFontAttributes() {}
|
||||
|
||||
void AssetFontSelector::parseFontManifest() {
|
||||
std::vector<uint8_t> font_manifest_data;
|
||||
if (!directory_asset_bundle_ ||
|
||||
!directory_asset_bundle_->GetAsBuffer(kFontManifestAssetPath,
|
||||
&font_manifest_data)) {
|
||||
if (!asset_provider_ ||
|
||||
!asset_provider_->GetAsBuffer(kFontManifestAssetPath,
|
||||
&font_manifest_data)) {
|
||||
if (!asset_store_ ||
|
||||
!asset_store_->GetAsBuffer(kFontManifestAssetPath, &font_manifest_data))
|
||||
return;
|
||||
@@ -239,8 +239,8 @@ sk_sp<SkTypeface> AssetFontSelector::getTypefaceAsset(
|
||||
}
|
||||
|
||||
std::unique_ptr<TypefaceAsset> typeface_asset(new TypefaceAsset);
|
||||
if (!directory_asset_bundle_ || !directory_asset_bundle_->GetAsBuffer(
|
||||
asset_path, &typeface_asset->data)) {
|
||||
if (!asset_provider_ || !asset_provider_->GetAsBuffer(
|
||||
asset_path, &typeface_asset->data)) {
|
||||
if (!asset_store_ ||
|
||||
!asset_store_->GetAsBuffer(asset_path, &typeface_asset->data)) {
|
||||
typeface_cache_.insert(std::make_pair(asset_path, nullptr));
|
||||
|
||||
@@ -24,7 +24,7 @@ class AssetFontSelector : public FontSelector {
|
||||
|
||||
~AssetFontSelector() override;
|
||||
|
||||
static void Install(fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle);
|
||||
static void Install(fxl::RefPtr<AssetProvider> asset_provider);
|
||||
|
||||
// TODO(zarah): Remove this and related code using asset_store once flx is
|
||||
// removed.
|
||||
@@ -45,7 +45,7 @@ class AssetFontSelector : public FontSelector {
|
||||
struct TypefaceAsset;
|
||||
|
||||
explicit AssetFontSelector(
|
||||
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle);
|
||||
fxl::RefPtr<AssetProvider> asset_provider);
|
||||
|
||||
explicit AssetFontSelector(fxl::RefPtr<ZipAssetStore> asset_store);
|
||||
|
||||
@@ -54,7 +54,7 @@ class AssetFontSelector : public FontSelector {
|
||||
sk_sp<SkTypeface> getTypefaceAsset(const FontDescription& font_description,
|
||||
const AtomicString& family_name);
|
||||
|
||||
fxl::RefPtr<DirectoryAssetBundle> directory_asset_bundle_;
|
||||
fxl::RefPtr<AssetProvider> asset_provider_;
|
||||
|
||||
fxl::RefPtr<ZipAssetStore> asset_store_;
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "flutter/assets/directory_asset_bundle.h"
|
||||
#include "flutter/assets/unzipper_provider.h"
|
||||
#include "flutter/assets/zip_asset_store.h"
|
||||
#include "flutter/assets/asset_provider.h"
|
||||
#include "flutter/common/settings.h"
|
||||
#include "flutter/common/threads.h"
|
||||
#include "flutter/glue/trace_event.h"
|
||||
@@ -288,7 +289,6 @@ void Engine::Init(const std::string& bundle_path) {
|
||||
#else
|
||||
#error Unknown OS
|
||||
#endif
|
||||
|
||||
blink::InitRuntime(vm_snapshot_data, vm_snapshot_instr,
|
||||
default_isolate_snapshot_data,
|
||||
default_isolate_snapshot_instr, bundle_path);
|
||||
@@ -302,7 +302,6 @@ void Engine::RunBundle(const std::string& bundle_path,
|
||||
TRACE_EVENT0("flutter", "Engine::RunBundle");
|
||||
ConfigureAssetBundle(bundle_path);
|
||||
ConfigureRuntime(GetScriptUriFromPath(bundle_path), reuse_runtime_controller);
|
||||
|
||||
if (blink::IsRunningPrecompiledCode()) {
|
||||
runtime_->dart_controller()->RunFromPrecompiledSnapshot(entrypoint);
|
||||
} else {
|
||||
@@ -573,6 +572,12 @@ void Engine::SetSemanticsEnabled(bool enabled) {
|
||||
}
|
||||
|
||||
void Engine::ConfigureAssetBundle(const std::string& path) {
|
||||
auto platform_view = platform_view_.lock();
|
||||
asset_provider_ = platform_view->GetAssetProvider();
|
||||
if (!asset_provider_) {
|
||||
asset_provider_ = fxl::MakeRefCounted<blink::DirectoryAssetBundle>(path);
|
||||
}
|
||||
|
||||
struct stat stat_result = {};
|
||||
|
||||
// TODO(abarth): We should reset directory_asset_bundle_, but that might break
|
||||
@@ -585,8 +590,6 @@ void Engine::ConfigureAssetBundle(const std::string& path) {
|
||||
|
||||
std::string flx_path;
|
||||
if (S_ISDIR(stat_result.st_mode)) {
|
||||
directory_asset_bundle_ =
|
||||
fxl::MakeRefCounted<blink::DirectoryAssetBundle>(path);
|
||||
flx_path = files::GetDirectoryName(path) + "/app.flx";
|
||||
} else if (S_ISREG(stat_result.st_mode)) {
|
||||
flx_path = path;
|
||||
@@ -618,11 +621,11 @@ void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
|
||||
blink::TestFontSelector::Install();
|
||||
if (!blink::Settings::Get().using_blink)
|
||||
blink::FontCollection::ForProcess().RegisterTestFonts();
|
||||
} else if (directory_asset_bundle_) {
|
||||
blink::AssetFontSelector::Install(directory_asset_bundle_);
|
||||
} else if (asset_provider_) {
|
||||
blink::AssetFontSelector::Install(asset_provider_);
|
||||
if (!blink::Settings::Get().using_blink) {
|
||||
blink::FontCollection::ForProcess().RegisterFontsFromDirectoryAssetBundle(
|
||||
directory_asset_bundle_);
|
||||
blink::FontCollection::ForProcess().RegisterFontsFromAssetProvider(
|
||||
asset_provider_);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -693,7 +696,6 @@ void Engine::HandleAssetPlatformMessage(
|
||||
const auto& data = message->data();
|
||||
std::string asset_name(reinterpret_cast<const char*>(data.data()),
|
||||
data.size());
|
||||
|
||||
std::vector<uint8_t> asset_data;
|
||||
if (GetAssetAsBuffer(asset_name, &asset_data)) {
|
||||
response->Complete(std::move(asset_data));
|
||||
@@ -704,9 +706,9 @@ void Engine::HandleAssetPlatformMessage(
|
||||
|
||||
bool Engine::GetAssetAsBuffer(const std::string& name,
|
||||
std::vector<uint8_t>* data) {
|
||||
return (directory_asset_bundle_ &&
|
||||
directory_asset_bundle_->GetAsBuffer(name, data)) ||
|
||||
(asset_store_ && asset_store_->GetAsBuffer(name, data));
|
||||
return ((asset_provider_ &&
|
||||
asset_provider_->GetAsBuffer(name, data)) ||
|
||||
(asset_store_ && asset_store_->GetAsBuffer(name, data)));
|
||||
}
|
||||
|
||||
} // namespace shell
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define SHELL_COMMON_ENGINE_H_
|
||||
|
||||
#include "flutter/assets/zip_asset_store.h"
|
||||
#include "flutter/assets/asset_provider.h"
|
||||
#include "flutter/lib/ui/window/platform_message.h"
|
||||
#include "flutter/lib/ui/window/viewport_metrics.h"
|
||||
#include "flutter/runtime/runtime_controller.h"
|
||||
@@ -109,6 +110,7 @@ class Engine : public blink::RuntimeDelegate {
|
||||
|
||||
static const std::string main_entrypoint_;
|
||||
|
||||
fxl::RefPtr<blink::AssetProvider> asset_provider_;
|
||||
std::weak_ptr<PlatformView> platform_view_;
|
||||
std::unique_ptr<Animator> animator_;
|
||||
std::unique_ptr<blink::RuntimeController> runtime_;
|
||||
|
||||
@@ -126,6 +126,10 @@ VsyncWaiter* PlatformView::GetVsyncWaiter() {
|
||||
return vsync_waiter_.get();
|
||||
}
|
||||
|
||||
fxl::RefPtr<blink::AssetProvider> PlatformView::GetAssetProvider() {
|
||||
return asset_provider_;
|
||||
}
|
||||
|
||||
void PlatformView::UpdateSemantics(blink::SemanticsNodeUpdates update) {}
|
||||
|
||||
void PlatformView::HandlePlatformMessage(
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/assets/asset_provider.h"
|
||||
#include "flutter/flow/texture.h"
|
||||
#include "flutter/lib/ui/semantics/semantics_node.h"
|
||||
#include "flutter/shell/common/engine.h"
|
||||
@@ -64,6 +65,8 @@ class PlatformView : public std::enable_shared_from_this<PlatformView> {
|
||||
virtual void HandlePlatformMessage(
|
||||
fxl::RefPtr<blink::PlatformMessage> message);
|
||||
|
||||
virtual fxl::RefPtr<blink::AssetProvider> GetAssetProvider();
|
||||
|
||||
// Called once per texture, on the platform thread.
|
||||
void RegisterTexture(std::shared_ptr<flow::Texture> texture);
|
||||
|
||||
@@ -97,6 +100,8 @@ class PlatformView : public std::enable_shared_from_this<PlatformView> {
|
||||
flow::TextureRegistry texture_registry_;
|
||||
std::unique_ptr<Engine> engine_;
|
||||
std::unique_ptr<VsyncWaiter> vsync_waiter_;
|
||||
fxl::RefPtr<blink::AssetProvider> asset_provider_;
|
||||
|
||||
SkISize size_;
|
||||
|
||||
private:
|
||||
|
||||
@@ -27,6 +27,8 @@ shared_library("flutter_shell_native") {
|
||||
"android_surface_gl.h",
|
||||
"android_surface_software.h",
|
||||
"android_surface_software.cc",
|
||||
"apk_asset_provider.h",
|
||||
"apk_asset_provider.cc",
|
||||
"flutter_main.cc",
|
||||
"flutter_main.h",
|
||||
"library_loader.cc",
|
||||
@@ -42,6 +44,7 @@ shared_library("flutter_shell_native") {
|
||||
"$flutter_root/common",
|
||||
"$flutter_root/flow",
|
||||
"$flutter_root/fml",
|
||||
"$flutter_root/assets",
|
||||
"$flutter_root/lib/ui",
|
||||
"$flutter_root/runtime",
|
||||
"$flutter_root/shell/common",
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <unistd.h>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
#include "flutter/shell/platform/android/apk_asset_provider.h"
|
||||
#include "lib/fxl/logging.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
bool APKAssetProvider::GetAsBuffer(const std::string& asset_name,
|
||||
std::vector<uint8_t>* data) {
|
||||
std::stringstream ss;
|
||||
ss << directory_.c_str() << "/" << asset_name;
|
||||
AAsset* asset = AAssetManager_open(assetManager_, ss.str().c_str(), AASSET_MODE_BUFFER);
|
||||
if (!asset) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t* buffer = (uint8_t*)AAsset_getBuffer(asset);
|
||||
if (!buffer) {
|
||||
FXL_LOG(ERROR) << "Got null trying to acquire buffer for asset:" << asset;
|
||||
return false;
|
||||
}
|
||||
|
||||
data->resize(AAsset_getLength(asset));
|
||||
std::copy(buffer, buffer + data->size(), data->begin());
|
||||
AAsset_close(asset);
|
||||
return true;
|
||||
}
|
||||
|
||||
APKAssetProvider::~APKAssetProvider() {}
|
||||
|
||||
APKAssetProvider::APKAssetProvider(JNIEnv* env, jobject jassetManager, std::string directory)
|
||||
: directory_(std::move(directory)) {
|
||||
assetManager_ = AAssetManager_fromJava(env, jassetManager);
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_ASSETS_APK_ASSET_PROVIDER_H_
|
||||
#define FLUTTER_ASSETS_APK_ASSET_PROVIDER_H_
|
||||
|
||||
#include <jni.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
|
||||
#include "flutter/assets/asset_provider.h"
|
||||
#include "lib/fxl/memory/ref_counted.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
class APKAssetProvider
|
||||
: public AssetProvider {
|
||||
public:
|
||||
explicit APKAssetProvider(JNIEnv* env, jobject assetManager, std::string directory);
|
||||
virtual ~APKAssetProvider();
|
||||
|
||||
virtual bool GetAsBuffer(const std::string& asset_name,
|
||||
std::vector<uint8_t>* data);
|
||||
|
||||
private:
|
||||
AAssetManager* assetManager_;
|
||||
const std::string directory_;
|
||||
};
|
||||
|
||||
} // namespace blink
|
||||
|
||||
#endif // FLUTTER_ASSETS_APK_ASSET_PROVIDER_H
|
||||
@@ -66,10 +66,16 @@ public class FlutterMain {
|
||||
private static final String DEFAULT_AOT_ISOLATE_SNAPSHOT_INSTR = "isolate_snapshot_instr";
|
||||
private static final String DEFAULT_FLX = "app.flx";
|
||||
private static final String DEFAULT_SNAPSHOT_BLOB = "snapshot_blob.bin";
|
||||
private static final String DEFAULT_KERNEL_BLOB = "kernel_blob.bin";
|
||||
private static final String DEFAULT_PLATFORM_DILL = "platform.dill";
|
||||
private static final String DEFAULT_FLUTTER_ASSETS_DIR = "flutter_assets";
|
||||
|
||||
private static final String MANIFEST = "flutter.yaml";
|
||||
|
||||
private static String fromFlutterAssets(String filePath) {
|
||||
return sFlutterAssetsDir + "/" + filePath;
|
||||
}
|
||||
|
||||
private static final Set<String> SKY_RESOURCES = ImmutableSetBuilder.<String>newInstance()
|
||||
.add("icudtl.dat")
|
||||
.add(MANIFEST)
|
||||
@@ -244,8 +250,10 @@ public class FlutterMain {
|
||||
new ResourceCleaner(context).start();
|
||||
sResourceExtractor = new ResourceExtractor(context)
|
||||
.addResources(SKY_RESOURCES)
|
||||
.addResource(sFlx)
|
||||
.addResource(sFlutterAssetsDir);
|
||||
.addResource(fromFlutterAssets(sFlx))
|
||||
.addResource(fromFlutterAssets(sSnapshotBlob))
|
||||
.addResource(fromFlutterAssets(DEFAULT_KERNEL_BLOB))
|
||||
.addResource(fromFlutterAssets(DEFAULT_PLATFORM_DILL));
|
||||
if (sIsPrecompiledAsSharedLibrary) {
|
||||
sResourceExtractor
|
||||
.addResource(sAotSharedLibraryPath);
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import android.content.res.AssetManager;
|
||||
|
||||
public class FlutterNativeView implements BinaryMessenger {
|
||||
private static final String TAG = "FlutterNativeView";
|
||||
@@ -24,8 +25,10 @@ public class FlutterNativeView implements BinaryMessenger {
|
||||
private final FlutterPluginRegistry mPluginRegistry;
|
||||
private long mNativePlatformView;
|
||||
private FlutterView mFlutterView;
|
||||
private final Context mContext;
|
||||
|
||||
public FlutterNativeView(Context context) {
|
||||
mContext = context;
|
||||
mPluginRegistry = new FlutterPluginRegistry(this, context);
|
||||
attach(this);
|
||||
assertAttached();
|
||||
@@ -68,7 +71,7 @@ public class FlutterNativeView implements BinaryMessenger {
|
||||
|
||||
public void runFromBundle(String bundlePath, String snapshotOverride, String entrypoint, boolean reuseRuntimeController) {
|
||||
assertAttached();
|
||||
nativeRunBundleAndSnapshot(mNativePlatformView, bundlePath, snapshotOverride, entrypoint, reuseRuntimeController);
|
||||
nativeRunBundleAndSnapshot(mNativePlatformView, bundlePath, snapshotOverride, entrypoint, reuseRuntimeController, mContext.getResources().getAssets());
|
||||
}
|
||||
|
||||
public void runFromSource(final String assetsDirectory, final String main, final String packages) {
|
||||
@@ -194,7 +197,8 @@ public class FlutterNativeView implements BinaryMessenger {
|
||||
String bundlePath,
|
||||
String snapshotOverride,
|
||||
String entrypoint,
|
||||
boolean reuseRuntimeController);
|
||||
boolean reuseRuntimeController,
|
||||
AssetManager manager);
|
||||
|
||||
private static native void nativeRunBundleAndSource(long nativePlatformViewAndroid,
|
||||
String bundlePath,
|
||||
|
||||
@@ -13,6 +13,7 @@ import android.util.Log;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -47,48 +48,39 @@ class ResourceExtractor {
|
||||
}
|
||||
|
||||
final AssetManager manager = mContext.getResources().getAssets();
|
||||
try {
|
||||
byte[] buffer = null;
|
||||
final String[] assets = manager.list("");
|
||||
LinkedList<String> assetList = new LinkedList<>(Arrays.asList(assets));
|
||||
while(!assetList.isEmpty()) {
|
||||
String asset = assetList.pop();
|
||||
if (!mResources.contains(asset))
|
||||
continue;
|
||||
|
||||
if (manager.list(asset).length > 0) {
|
||||
// The asset is a directory
|
||||
for (String a: manager.list(asset)) {
|
||||
assetList.add(asset + File.separator + a);
|
||||
mResources.add(asset + File.separator + a);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
byte[] buffer = null;
|
||||
for (String asset : mResources) {
|
||||
try {
|
||||
final File output = new File(dataDir, asset);
|
||||
if (output.exists())
|
||||
|
||||
if (output.exists()) {
|
||||
continue;
|
||||
}
|
||||
if (output.getParentFile() != null) {
|
||||
output.getParentFile().mkdirs();
|
||||
}
|
||||
|
||||
try (InputStream is = manager.open(asset)) {
|
||||
try (OutputStream os = new FileOutputStream(output)) {
|
||||
if (buffer == null) {
|
||||
buffer = new byte[BUFFER_SIZE];
|
||||
}
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
while ((count = is.read(buffer, 0, BUFFER_SIZE)) != -1) {
|
||||
os.write(buffer, 0, count);
|
||||
}
|
||||
os.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
continue;
|
||||
} catch (IOException ioe) {
|
||||
Log.w(TAG, "Exception unpacking resources: " + ioe.getMessage());
|
||||
deleteFiles();
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.w(TAG, "Exception unpacking resources: " + e.getMessage());
|
||||
deleteFiles();
|
||||
return;
|
||||
}
|
||||
|
||||
if (timestamp != null) {
|
||||
@@ -184,18 +176,10 @@ class ResourceExtractor {
|
||||
|
||||
private void deleteFiles() {
|
||||
final File dataDir = new File(PathUtils.getDataDirectory(mContext));
|
||||
LinkedList<String> files = new LinkedList<>(mResources);
|
||||
while (!files.isEmpty()) {
|
||||
String resource = files.pop();
|
||||
for (String resource : mResources) {
|
||||
final File file = new File(dataDir, resource);
|
||||
if (file.exists()) {
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
} else {
|
||||
for (String f : file.list()) {
|
||||
files.add(resource + File.separator + f);
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
for (String timestamp : getExistingTimestamps(dataDir)) {
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "flutter/shell/platform/android/android_external_texture_gl.h"
|
||||
#include "flutter/shell/platform/android/android_surface_gl.h"
|
||||
#include "flutter/shell/platform/android/android_surface_software.h"
|
||||
#include "flutter/shell/platform/android/apk_asset_provider.h"
|
||||
#include "flutter/shell/platform/android/platform_view_android_jni.h"
|
||||
#include "flutter/shell/platform/android/vsync_waiter_android.h"
|
||||
#include "lib/fxl/functional/make_copyable.h"
|
||||
@@ -207,10 +208,16 @@ void PlatformViewAndroid::SurfaceDestroyed() {
|
||||
ReleaseSurface();
|
||||
}
|
||||
|
||||
void PlatformViewAndroid::RunBundleAndSnapshot(std::string bundle_path,
|
||||
void PlatformViewAndroid::RunBundleAndSnapshot(JNIEnv* env, std::string bundle_path,
|
||||
std::string snapshot_override,
|
||||
std::string entrypoint,
|
||||
bool reuse_runtime_controller) {
|
||||
bool reuse_runtime_controller,
|
||||
jobject assetManager) {
|
||||
// The flutter assets directory name is the last directory of the bundle_path and the path into the APK
|
||||
size_t last_slash_idx = bundle_path.rfind("/", bundle_path.size());
|
||||
std::string flutter_assets_dir = bundle_path.substr(last_slash_idx+1, bundle_path.size()-last_slash_idx);
|
||||
|
||||
asset_provider_ = fxl::MakeRefCounted<blink::APKAssetProvider>(env, assetManager, flutter_assets_dir);
|
||||
blink::Threads::UI()->PostTask([
|
||||
engine = engine_->GetWeakPtr(), bundle_path = std::move(bundle_path),
|
||||
snapshot_override = std::move(snapshot_override),
|
||||
|
||||
@@ -38,10 +38,11 @@ class PlatformViewAndroid : public PlatformView {
|
||||
|
||||
void SurfaceDestroyed();
|
||||
|
||||
void RunBundleAndSnapshot(std::string bundle_path,
|
||||
void RunBundleAndSnapshot(JNIEnv* env, std::string bundle_path,
|
||||
std::string snapshot_override,
|
||||
std::string entrypoint,
|
||||
bool reuse_isolate);
|
||||
bool reuse_isolate,
|
||||
jobject assetManager);
|
||||
|
||||
void RunBundleAndSource(std::string bundle_path,
|
||||
std::string main,
|
||||
|
||||
@@ -160,12 +160,15 @@ static void RunBundleAndSnapshot(JNIEnv* env,
|
||||
jstring bundlePath,
|
||||
jstring snapshotOverride,
|
||||
jstring entrypoint,
|
||||
jboolean reuse_runtime_controller) {
|
||||
jboolean reuse_runtime_controller,
|
||||
jobject assetManager) {
|
||||
return PLATFORM_VIEW->RunBundleAndSnapshot(
|
||||
env,
|
||||
fml::jni::JavaStringToString(env, bundlePath), //
|
||||
fml::jni::JavaStringToString(env, snapshotOverride), //
|
||||
fml::jni::JavaStringToString(env, entrypoint), //
|
||||
reuse_runtime_controller //
|
||||
reuse_runtime_controller, //
|
||||
assetManager
|
||||
);
|
||||
}
|
||||
|
||||
@@ -351,7 +354,7 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
|
||||
{
|
||||
.name = "nativeRunBundleAndSnapshot",
|
||||
.signature =
|
||||
"(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V",
|
||||
"(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;ZLandroid/content/res/AssetManager;)V",
|
||||
.fnPtr = reinterpret_cast<void*>(&shell::RunBundleAndSnapshot),
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user