Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include "com_android_tools_aapt2_Aapt2.h" |
| 18 | |
Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 19 | #include <algorithm> |
Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | #include "android-base/logging.h" |
| 25 | #include "nativehelper/ScopedUtfChars.h" |
Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 26 | |
| 27 | #include "util/Util.h" |
| 28 | |
Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 29 | namespace aapt { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | extern int Compile(const std::vector<StringPiece> &args); |
| 31 | extern int Link(const std::vector<StringPiece> &args); |
Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* |
| 35 | * Converts a java List<String> into C++ vector<ScopedUtfChars>. |
| 36 | */ |
| 37 | static std::vector<ScopedUtfChars> list_to_utfchars(JNIEnv *env, jobject obj) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | std::vector<ScopedUtfChars> converted; |
Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 39 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 40 | // 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 Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 46 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | // Now, iterate all strings in the list |
| 48 | // (note: generic erasure means get() return an Object) |
Paulo Casanova | b555b59 | 2016-11-01 14:50:46 +0000 | [diff] [blame^] | 49 | jmethodID get_method_id = env->GetMethodID(list_cls, "get", "(I)Ljava/lang/Object;"); |
| 50 | CHECK(get_method_id != 0); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 51 | 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 Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 58 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | return converted; |
Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
| 63 | * Extracts all StringPiece from the ScopedUtfChars instances. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | * |
Paulo Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 65 | * The returned pieces can only be used while the original ones have not been |
| 66 | * destroyed. |
| 67 | */ |
| 68 | static std::vector<aapt::StringPiece> extract_pieces( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 69 | 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 Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | JNIEXPORT void JNICALL Java_com_android_tools_aapt2_Aapt2_nativeCompile( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 80 | 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 Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | JNIEXPORT void JNICALL Java_com_android_tools_aapt2_Aapt2_nativeLink( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 89 | 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 Casanova | 2559608 | 2016-10-07 12:00:26 +0100 | [diff] [blame] | 94 | } |
Paulo Casanova | b555b59 | 2016-11-01 14:50:46 +0000 | [diff] [blame^] | 95 | |
| 96 | JNIEXPORT 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 | } |