blob: a5e09a2c9d57a3827a86a9aa484469147e33fc52 [file] [log] [blame]
Paulo Casanova25596082016-10-07 12:00:26 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include "com_android_tools_aapt2_Aapt2.h"
18
Paulo Casanova25596082016-10-07 12:00:26 +010019#include <algorithm>
Paulo Casanova25596082016-10-07 12:00:26 +010020#include <memory>
21#include <utility>
22#include <vector>
23
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "android-base/logging.h"
25#include "nativehelper/ScopedUtfChars.h"
Paulo Casanova25596082016-10-07 12:00:26 +010026
27#include "util/Util.h"
28
Paulo Casanova25596082016-10-07 12:00:26 +010029namespace aapt {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030extern int Compile(const std::vector<StringPiece> &args);
31extern int Link(const std::vector<StringPiece> &args);
Paulo Casanova25596082016-10-07 12:00:26 +010032}
33
34/*
35 * Converts a java List<String> into C++ vector<ScopedUtfChars>.
36 */
37static std::vector<ScopedUtfChars> list_to_utfchars(JNIEnv *env, jobject obj) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 std::vector<ScopedUtfChars> converted;
Paulo Casanova25596082016-10-07 12:00:26 +010039
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 // Call size() method on the list to know how many elements there are.
41 jclass list_cls = env->GetObjectClass(obj);
42 jmethodID size_method_id = env->GetMethodID(list_cls, "size", "()I");
43 CHECK(size_method_id != 0);
44 jint size = env->CallIntMethod(obj, size_method_id);
45 CHECK(size >= 0);
Paulo Casanova25596082016-10-07 12:00:26 +010046
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 // Now, iterate all strings in the list
48 // (note: generic erasure means get() return an Object)
Paulo Casanovab555b592016-11-01 14:50:46 +000049 jmethodID get_method_id = env->GetMethodID(list_cls, "get", "(I)Ljava/lang/Object;");
50 CHECK(get_method_id != 0);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 for (jint i = 0; i < size; i++) {
52 // Call get(i) to get the string in the ith position.
53 jobject string_obj_uncast = env->CallObjectMethod(obj, get_method_id, i);
54 CHECK(string_obj_uncast != nullptr);
55 jstring string_obj = static_cast<jstring>(string_obj_uncast);
56 converted.push_back(ScopedUtfChars(env, string_obj));
57 }
Paulo Casanova25596082016-10-07 12:00:26 +010058
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 return converted;
Paulo Casanova25596082016-10-07 12:00:26 +010060}
61
62/*
63 * Extracts all StringPiece from the ScopedUtfChars instances.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 *
Paulo Casanova25596082016-10-07 12:00:26 +010065 * The returned pieces can only be used while the original ones have not been
66 * destroyed.
67 */
68static std::vector<aapt::StringPiece> extract_pieces(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 const std::vector<ScopedUtfChars> &strings) {
70 std::vector<aapt::StringPiece> pieces;
71
72 std::for_each(
73 strings.begin(), strings.end(),
74 [&pieces](const ScopedUtfChars &p) { pieces.push_back(p.c_str()); });
75
76 return pieces;
Paulo Casanova25596082016-10-07 12:00:26 +010077}
78
79JNIEXPORT void JNICALL Java_com_android_tools_aapt2_Aapt2_nativeCompile(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 JNIEnv *env, jclass aapt_obj, jobject arguments_obj) {
81 std::vector<ScopedUtfChars> compile_args_jni =
82 list_to_utfchars(env, arguments_obj);
83 std::vector<aapt::StringPiece> compile_args =
84 extract_pieces(compile_args_jni);
85 aapt::Compile(compile_args);
Paulo Casanova25596082016-10-07 12:00:26 +010086}
87
88JNIEXPORT void JNICALL Java_com_android_tools_aapt2_Aapt2_nativeLink(
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 JNIEnv *env, jclass aapt_obj, jobject arguments_obj) {
90 std::vector<ScopedUtfChars> link_args_jni =
91 list_to_utfchars(env, arguments_obj);
92 std::vector<aapt::StringPiece> link_args = extract_pieces(link_args_jni);
93 aapt::Link(link_args);
Paulo Casanova25596082016-10-07 12:00:26 +010094}
Paulo Casanovab555b592016-11-01 14:50:46 +000095
96JNIEXPORT void JNICALL Java_com_android_tools_aapt2_Aapt2_ping(
97 JNIEnv *env, jclass aapt_obj) {
98 // This is just a dummy method to see if the library has been loaded.
99}