[view compiler] Add XML to DEX compilation

Test: atest
Bug: 111895153
Change-Id: I91c01ff4474e080c87b902ae963b5d655346f859
This commit is contained in:
Eric Holk
2018-12-17 15:46:18 -08:00
parent aa8f1b7033
commit 4273457c9e
10 changed files with 478 additions and 25 deletions

View File

@@ -34,6 +34,7 @@ cc_library_host_static {
defaults: ["viewcompiler_defaults"],
srcs: [
"dex_builder.cc",
"dex_layout_compiler.cc",
"java_lang_builder.cc",
"tinyxml_layout_parser.cc",
"util.cc",

View File

@@ -14,16 +14,30 @@
// limitations under the License.
//
genrule {
name: "generate_compiled_layout",
tools: [":viewcompiler"],
cmd: "$(location :viewcompiler) $(in) --dex --out $(out) --package android.startop.test",
srcs: ["res/layout/layout1.xml"],
out: [
"layout1.dex",
],
}
android_test {
name: "dex-builder-test",
srcs: ["src/android/startop/test/DexBuilderTest.java"],
srcs: [
"src/android/startop/test/DexBuilderTest.java",
"src/android/startop/test/LayoutCompilerTest.java",
],
sdk_version: "current",
data: [":generate_dex_testcases"],
data: [":generate_dex_testcases", ":generate_compiled_layout"],
static_libs: [
"android-support-test",
"guava",
],
manifest: "AndroidManifest.xml",
resource_dirs: ["res"],
test_config: "AndroidTest.xml",
test_suites: ["general-tests"],
}

View File

@@ -25,6 +25,7 @@
<option name="cleanup" value="true" />
<option name="push" value="trivial.dex->/data/local/tmp/dex-builder-test/trivial.dex" />
<option name="push" value="simple.dex->/data/local/tmp/dex-builder-test/simple.dex" />
<option name="push" value="layout1.dex->/data/local/tmp/dex-builder-test/layout1.dex" />
</target_preparer>
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package android.startop.test;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.view.View;
import com.google.common.io.ByteStreams;
import dalvik.system.InMemoryDexClassLoader;
import dalvik.system.PathClassLoader;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import org.junit.Assert;
import org.junit.Test;
// Adding tests here requires changes in several other places. See README.md in
// the view_compiler directory for more information.
public class LayoutCompilerTest {
static ClassLoader loadDexFile(String filename) throws Exception {
return new PathClassLoader("/data/local/tmp/dex-builder-test/" + filename,
ClassLoader.getSystemClassLoader());
}
@Test
public void loadAndInflaterLayout1() throws Exception {
ClassLoader dex_file = loadDexFile("layout1.dex");
Class compiled_view = dex_file.loadClass("android.startop.test.CompiledView");
Method layout1 = compiled_view.getMethod("layout1", Context.class, int.class);
Context context = InstrumentationRegistry.getTargetContext();
layout1.invoke(null, context, R.layout.layout1);
}
}

View File

@@ -0,0 +1,226 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dex_layout_compiler.h"
#include "layout_validation.h"
#include "android-base/stringprintf.h"
namespace startop {
using android::base::StringPrintf;
void LayoutValidationVisitor::VisitStartTag(const std::u16string& name) {
if (0 == name.compare(u"merge")) {
message_ = "Merge tags are not supported";
can_compile_ = false;
}
if (0 == name.compare(u"include")) {
message_ = "Include tags are not supported";
can_compile_ = false;
}
if (0 == name.compare(u"view")) {
message_ = "View tags are not supported";
can_compile_ = false;
}
if (0 == name.compare(u"fragment")) {
message_ = "Fragment tags are not supported";
can_compile_ = false;
}
}
DexViewBuilder::DexViewBuilder(dex::MethodBuilder* method)
: method_{method},
context_{dex::Value::Parameter(0)},
resid_{dex::Value::Parameter(1)},
inflater_{method->MakeRegister()},
xml_{method->MakeRegister()},
attrs_{method->MakeRegister()},
classname_tmp_{method->MakeRegister()},
xml_next_{method->dex_file()->GetOrDeclareMethod(
dex::TypeDescriptor::FromClassname("android.content.res.XmlResourceParser"), "next",
dex::Prototype{dex::TypeDescriptor::Int()})},
try_create_view_{method->dex_file()->GetOrDeclareMethod(
dex::TypeDescriptor::FromClassname("android.view.LayoutInflater"), "tryCreateView",
dex::Prototype{dex::TypeDescriptor::FromClassname("android.view.View"),
dex::TypeDescriptor::FromClassname("android.view.View"),
dex::TypeDescriptor::FromClassname("java.lang.String"),
dex::TypeDescriptor::FromClassname("android.content.Context"),
dex::TypeDescriptor::FromClassname("android.util.AttributeSet")})},
generate_layout_params_{method->dex_file()->GetOrDeclareMethod(
dex::TypeDescriptor::FromClassname("android.view.ViewGroup"), "generateLayoutParams",
dex::Prototype{dex::TypeDescriptor::FromClassname("android.view.ViewGroup$LayoutParams"),
dex::TypeDescriptor::FromClassname("android.util.AttributeSet")})},
add_view_{method->dex_file()->GetOrDeclareMethod(
dex::TypeDescriptor::FromClassname("android.view.ViewGroup"), "addView",
dex::Prototype{
dex::TypeDescriptor::Void(),
dex::TypeDescriptor::FromClassname("android.view.View"),
dex::TypeDescriptor::FromClassname("android.view.ViewGroup$LayoutParams")})},
// The register stack starts with one register, which will be null for the root view.
register_stack_{{method->MakeRegister()}} {}
void DexViewBuilder::Start() {
dex::DexBuilder* const dex = method_->dex_file();
// LayoutInflater inflater = LayoutInflater.from(context);
auto layout_inflater_from = dex->GetOrDeclareMethod(
dex::TypeDescriptor::FromClassname("android.view.LayoutInflater"),
"from",
dex::Prototype{dex::TypeDescriptor::FromClassname("android.view.LayoutInflater"),
dex::TypeDescriptor::FromClassname("android.content.Context")});
method_->AddInstruction(
dex::Instruction::InvokeStaticObject(layout_inflater_from.id, /*dest=*/inflater_, context_));
// Resources res = context.getResources();
auto context_type = dex::TypeDescriptor::FromClassname("android.content.Context");
auto resources_type = dex::TypeDescriptor::FromClassname("android.content.res.Resources");
auto get_resources =
dex->GetOrDeclareMethod(context_type, "getResources", dex::Prototype{resources_type});
method_->AddInstruction(dex::Instruction::InvokeVirtualObject(get_resources.id, xml_, context_));
// XmlResourceParser xml = res.getLayout(resid);
auto xml_resource_parser_type =
dex::TypeDescriptor::FromClassname("android.content.res.XmlResourceParser");
auto get_layout =
dex->GetOrDeclareMethod(resources_type,
"getLayout",
dex::Prototype{xml_resource_parser_type, dex::TypeDescriptor::Int()});
method_->AddInstruction(dex::Instruction::InvokeVirtualObject(get_layout.id, xml_, xml_, resid_));
// AttributeSet attrs = Xml.asAttributeSet(xml);
auto as_attribute_set = dex->GetOrDeclareMethod(
dex::TypeDescriptor::FromClassname("android.util.Xml"),
"asAttributeSet",
dex::Prototype{dex::TypeDescriptor::FromClassname("android.util.AttributeSet"),
dex::TypeDescriptor::FromClassname("org.xmlpull.v1.XmlPullParser")});
method_->AddInstruction(dex::Instruction::InvokeStaticObject(as_attribute_set.id, attrs_, xml_));
// xml.next(); // start document
method_->AddInstruction(dex::Instruction::InvokeInterface(xml_next_.id, {}, xml_));
}
void DexViewBuilder::Finish() {}
namespace {
std::string ResolveName(const std::string& name) {
if (name == "View") return "android.view.View";
if (name == "ViewGroup") return "android.view.ViewGroup";
if (name.find(".") == std::string::npos) {
return StringPrintf("android.widget.%s", name.c_str());
}
return name;
}
} // namespace
void DexViewBuilder::StartView(const std::string& name, bool is_viewgroup) {
bool const is_root_view = view_stack_.empty();
// xml.next(); // start tag
method_->AddInstruction(dex::Instruction::InvokeInterface(xml_next_.id, {}, xml_));
dex::Value view = AcquireRegister();
// try to create the view using the factories
method_->BuildConstString(classname_tmp_,
name); // TODO: the need to fully qualify the classname
if (is_root_view) {
dex::Value null = AcquireRegister();
method_->BuildConst4(null, 0);
method_->AddInstruction(dex::Instruction::InvokeVirtualObject(
try_create_view_.id, view, inflater_, null, classname_tmp_, context_, attrs_));
ReleaseRegister();
} else {
method_->AddInstruction(dex::Instruction::InvokeVirtualObject(
try_create_view_.id, view, inflater_, GetCurrentView(), classname_tmp_, context_, attrs_));
}
auto label = method_->MakeLabel();
// branch if not null
method_->AddInstruction(
dex::Instruction::OpWithArgs(dex::Instruction::Op::kBranchNEqz, /*dest=*/{}, view, label));
// If null, create the class directly.
method_->BuildNew(view,
dex::TypeDescriptor::FromClassname(ResolveName(name)),
dex::Prototype{dex::TypeDescriptor::Void(),
dex::TypeDescriptor::FromClassname("android.content.Context"),
dex::TypeDescriptor::FromClassname("android.util.AttributeSet")},
context_,
attrs_);
method_->AddInstruction(
dex::Instruction::OpWithArgs(dex::Instruction::Op::kBindLabel, /*dest=*/{}, label));
if (is_viewgroup) {
// Cast to a ViewGroup so we can add children later.
const ir::Type* view_group_def = method_->dex_file()->GetOrAddType(
dex::TypeDescriptor::FromClassname("android.view.ViewGroup").descriptor());
method_->AddInstruction(dex::Instruction::Cast(view, dex::Value::Type(view_group_def->orig_index)));
}
if (!is_root_view) {
// layout_params = parent.generateLayoutParams(attrs);
dex::Value layout_params{AcquireRegister()};
method_->AddInstruction(dex::Instruction::InvokeVirtualObject(
generate_layout_params_.id, layout_params, GetCurrentView(), attrs_));
view_stack_.push_back({view, layout_params});
} else {
view_stack_.push_back({view, {}});
}
}
void DexViewBuilder::FinishView() {
if (view_stack_.size() == 1) {
method_->BuildReturn(GetCurrentView(), /*is_object=*/true);
} else {
// parent.add(view, layout_params)
method_->AddInstruction(dex::Instruction::InvokeVirtual(
add_view_.id, /*dest=*/{}, GetParentView(), GetCurrentView(), GetCurrentLayoutParams()));
// xml.next(); // end tag
method_->AddInstruction(dex::Instruction::InvokeInterface(xml_next_.id, {}, xml_));
}
PopViewStack();
}
dex::Value DexViewBuilder::AcquireRegister() {
top_register_++;
if (register_stack_.size() == top_register_) {
register_stack_.push_back(method_->MakeRegister());
}
return register_stack_[top_register_];
}
void DexViewBuilder::ReleaseRegister() { top_register_--; }
dex::Value DexViewBuilder::GetCurrentView() const { return view_stack_.back().view; }
dex::Value DexViewBuilder::GetCurrentLayoutParams() const {
return view_stack_.back().layout_params.value();
}
dex::Value DexViewBuilder::GetParentView() const {
return view_stack_[view_stack_.size() - 2].view;
}
void DexViewBuilder::PopViewStack() {
const auto& top = view_stack_.back();
// release the layout params if we have them
if (top.layout_params.has_value()) {
ReleaseRegister();
}
// Unconditionally release the view register.
ReleaseRegister();
view_stack_.pop_back();
}
} // namespace startop

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DEX_LAYOUT_COMPILER_H_
#define DEX_LAYOUT_COMPILER_H_
#include "dex_builder.h"
#include <codecvt>
#include <locale>
#include <string>
#include <vector>
namespace startop {
// This visitor does the actual view compilation, using a supplied builder.
template <typename Builder>
class LayoutCompilerVisitor {
public:
explicit LayoutCompilerVisitor(Builder* builder) : builder_{builder} {}
void VisitStartDocument() { builder_->Start(); }
void VisitEndDocument() { builder_->Finish(); }
void VisitStartTag(const std::u16string& name) {
parent_stack_.push_back(ViewEntry{
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(name), {}});
}
void VisitEndTag() {
auto entry = parent_stack_.back();
parent_stack_.pop_back();
if (parent_stack_.empty()) {
GenerateCode(entry);
} else {
parent_stack_.back().children.push_back(entry);
}
}
private:
struct ViewEntry {
std::string name;
std::vector<ViewEntry> children;
};
void GenerateCode(const ViewEntry& view) {
builder_->StartView(view.name, !view.children.empty());
for (const auto& child : view.children) {
GenerateCode(child);
}
builder_->FinishView();
}
Builder* builder_;
std::vector<ViewEntry> parent_stack_;
};
class DexViewBuilder {
public:
DexViewBuilder(dex::MethodBuilder* method);
void Start();
void Finish();
void StartView(const std::string& name, bool is_viewgroup);
void FinishView();
private:
// Accessors for the stack of views that are under construction.
dex::Value AcquireRegister();
void ReleaseRegister();
dex::Value GetCurrentView() const;
dex::Value GetCurrentLayoutParams() const;
dex::Value GetParentView() const;
void PopViewStack();
dex::MethodBuilder* method_;
// Registers used for code generation
dex::Value const context_;
dex::Value const resid_;
const dex::Value inflater_;
const dex::Value xml_;
const dex::Value attrs_;
const dex::Value classname_tmp_;
const dex::MethodDeclData xml_next_;
const dex::MethodDeclData try_create_view_;
const dex::MethodDeclData generate_layout_params_;
const dex::MethodDeclData add_view_;
// used for keeping track of which registers are in use
size_t top_register_{0};
std::vector<dex::Value> register_stack_;
// Keep track of the views currently in progress.
struct ViewEntry {
dex::Value view;
std::optional<dex::Value> layout_params;
};
std::vector<ViewEntry> view_stack_;
};
} // namespace startop
#endif // DEX_LAYOUT_COMPILER_H_

View File

@@ -67,7 +67,7 @@ void JavaLangViewBuilder::Finish() const {
"}\n"; // end CompiledView
}
void JavaLangViewBuilder::StartView(const string& class_name) {
void JavaLangViewBuilder::StartView(const string& class_name, bool /*is_viewgroup*/) {
const string view_var = MakeVar("view");
const string layout_var = MakeVar("layout");
std::string parent = "null";

View File

@@ -35,7 +35,7 @@ class JavaLangViewBuilder {
void Finish() const;
// Begin creating a view (i.e. process the opening tag)
void StartView(const std::string& class_name);
void StartView(const std::string& class_name, bool is_viewgroup);
// Finish a view, after all of its child nodes have been processed.
void FinishView();

View File

@@ -16,8 +16,11 @@
#include "gflags/gflags.h"
#include "android-base/stringprintf.h"
#include "dex_builder.h"
#include "dex_layout_compiler.h"
#include "java_lang_builder.h"
#include "layout_validation.h"
#include "tinyxml_layout_parser.h"
#include "util.h"
@@ -32,6 +35,12 @@
namespace {
using namespace tinyxml2;
using android::base::StringPrintf;
using startop::dex::ClassBuilder;
using startop::dex::DexBuilder;
using startop::dex::MethodBuilder;
using startop::dex::Prototype;
using startop::dex::TypeDescriptor;
using namespace startop::util;
using std::string;
@@ -41,34 +50,44 @@ DEFINE_bool(dex, false, "Generate a DEX file instead of Java");
DEFINE_string(out, kStdoutFilename, "Where to write the generated class");
DEFINE_string(package, "", "The package name for the generated class (required)");
class ViewCompilerXmlVisitor : public XMLVisitor {
template <typename Visitor>
class XmlVisitorAdapter : public XMLVisitor {
public:
explicit ViewCompilerXmlVisitor(JavaLangViewBuilder* builder) : builder_(builder) {}
explicit XmlVisitorAdapter(Visitor* visitor) : visitor_{visitor} {}
bool VisitEnter(const XMLDocument& /*doc*/) override {
builder_->Start();
visitor_->VisitStartDocument();
return true;
}
bool VisitExit(const XMLDocument& /*doc*/) override {
builder_->Finish();
visitor_->VisitEndDocument();
return true;
}
bool VisitEnter(const XMLElement& element, const XMLAttribute* /*firstAttribute*/) override {
builder_->StartView(element.Name());
visitor_->VisitStartTag(
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.from_bytes(
element.Name()));
return true;
}
bool VisitExit(const XMLElement& /*element*/) override {
builder_->FinishView();
visitor_->VisitEndTag();
return true;
}
private:
JavaLangViewBuilder* builder_;
Visitor* visitor_;
};
template <typename Builder>
void CompileLayout(XMLDocument* xml, Builder* builder) {
startop::LayoutCompilerVisitor visitor{builder};
XmlVisitorAdapter<decltype(visitor)> adapter{&visitor};
xml->Accept(&adapter);
}
} // end namespace
int main(int argc, char** argv) {
@@ -88,16 +107,8 @@ int main(int argc, char** argv) {
return 1;
}
if (FLAGS_dex) {
startop::dex::WriteTestDexFile("test.dex");
return 0;
}
const char* const filename = argv[kFileNameParam];
const string layout_name = FindLayoutNameFromFilename(filename);
// We want to generate Java language code to inflate exactly this layout. This means
// generating code to walk the resource XML too.
const string layout_name = startop::util::FindLayoutNameFromFilename(filename);
XMLDocument xml;
xml.LoadFile(filename);
@@ -108,15 +119,34 @@ int main(int argc, char** argv) {
return 1;
}
const bool is_stdout = FLAGS_out == kStdoutFilename;
std::ofstream outfile;
if (FLAGS_out != kStdoutFilename) {
if (!is_stdout) {
outfile.open(FLAGS_out);
}
JavaLangViewBuilder builder{
FLAGS_package, layout_name, FLAGS_out == kStdoutFilename ? std::cout : outfile};
ViewCompilerXmlVisitor visitor{&builder};
xml.Accept(&visitor);
if (FLAGS_dex) {
DexBuilder dex_file;
string class_name = StringPrintf("%s.CompiledView", FLAGS_package.c_str());
ClassBuilder compiled_view{dex_file.MakeClass(class_name)};
MethodBuilder method{compiled_view.CreateMethod(
layout_name,
Prototype{TypeDescriptor::FromClassname("android.view.View"),
TypeDescriptor::FromClassname("android.content.Context"),
TypeDescriptor::Int()})};
startop::DexViewBuilder builder{&method};
CompileLayout(&xml, &builder);
method.Encode();
slicer::MemView image{dex_file.CreateImage()};
(is_stdout ? std::cout : outfile).write(image.ptr<const char>(), image.size());
} else {
// Generate Java language output.
JavaLangViewBuilder builder{FLAGS_package, layout_name, is_stdout ? std::cout : outfile};
CompileLayout(&xml, &builder);
}
return 0;
}