Skip to content

Commit a350273

Browse files
committed
Support Android build (library) in gyp
1 parent 984d61c commit a350273

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+6450
-7
lines changed

base.gyp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,45 @@
211211
],
212212
} ],
213213
],
214+
['OS=="android"', {
215+
'defines': [
216+
'OS_ANDROID',
217+
],
218+
'sources': [
219+
'src/base/string16.cc',
220+
'src/base/time_posix.cc',
221+
'src/base/base_paths_android.cc',
222+
'src/base/debug/debugger_posix.cc',
223+
'src/base/file_util_android.cc',
224+
'src/base/file_util_posix.cc',
225+
'src/base/nix/xdg_util.cc',
226+
'src/base/platform_file_posix.cc',
227+
'src/base/rand_util_posix.cc',
228+
'src/base/safe_strerror_posix.cc',
229+
'src/base/synchronization/condition_variable_posix.cc',
230+
'src/base/synchronization/lock_impl_posix.cc',
231+
'src/base/synchronization/waitable_event_posix.cc',
232+
'src/base/sys_info_posix.cc',
233+
'src/base/sys_string_conversions_posix.cc',
234+
'src/base/threading/platform_thread_posix.cc',
235+
'src/base/threading/thread_local_posix.cc',
236+
'src/base/threading/thread_local_storage_posix.cc',
237+
'src/base/threading/worker_pool_posix.cc',
238+
'src/net/base/file_stream_metrics_posix.cc',
239+
'src/net/base/file_stream_posix.cc',
240+
'src/net/base/net_errors_posix.cc',
241+
'src/base/sys_info_linux.cc',
242+
'src/base/message_pump_android.cc',
243+
'src/base/os_compat_android.cc',
244+
'src/base/android/scoped_java_ref.cc',
245+
'src/base/android/path_utils.cc',
246+
'src/base/android/path_service_android.cc',
247+
'src/base/android/jni_string.cc',
248+
'src/base/android/jni_helper.cc',
249+
'src/base/android/jni_array.cc',
250+
'src/base/android/jni_android.cc',
251+
],
252+
} ],
214253
} , {
215254
'target_name': 'chromium_base_shared',
216255
'type': 'shared_library',

build_android.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
4+
arch=$1
5+
6+
if [ -z $arch ];
7+
then
8+
arch="arm"
9+
else
10+
arch="x86"
11+
fi
12+
13+
export ANDROID_NDK_ROOT=/opt/android/android-ndk-r8e
14+
export ANDROID_SDK_ROOT=opt/android/adt-bundle-linux-x86-20130522/sdk
15+
16+
if [ $arch = "x86" ];
17+
then
18+
export ANDROID_ARCH=arch-x86
19+
export ANDROID_LIB_ARCH=x86
20+
export ANDROID_TOOLCHAIN=x86-4.7
21+
export ANDROID_TOOL_PREFIX=i686-linux-android
22+
elif [ $arch = "arm" ];
23+
then
24+
export ANDROID_ARCH=arch-arm
25+
export ANDROID_LIB_ARCH=armeabi-v7a
26+
export ANDROID_TOOLCHAIN=arm-linux-androideabi-4.7
27+
export ANDROID_TOOL_PREFIX=arm-linux-androideabi
28+
else
29+
echo "We don't support platform " $arch
30+
exit 1
31+
fi
32+
33+
export PREBUILD=$ANDROID_NDK_ROOT/toolchains/$ANDROID_TOOLCHAIN/prebuilt/linux-x86
34+
export BIN=$PREBUILD/bin
35+
36+
export CXX=$BIN/$ANDROID_TOOL_PREFIX-g++
37+
export CC=$BIN/$ANDROID_TOOL_PREFIX-gcc
38+
export LINK=$BIN/$ANDROID_TOOL_PREFIX-g++
39+
export AR=$BIN/$ANDROID_TOOL_PREFIX-ar
40+
41+
42+
export GYP_DEFINES="OS=android"
43+
44+
platform="android"
45+
46+
./build.sh `pwd` $platform $mode

inc/base/base_paths_android.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef BASE_BASE_PATHS_ANDROID_H_
6+
#define BASE_BASE_PATHS_ANDROID_H_
7+
8+
// This file declares Android-specific path keys for the base module.
9+
// These can be used with the PathService to access various special
10+
// directories and files.
11+
12+
namespace base {
13+
14+
enum {
15+
PATH_ANDROID_START = 300,
16+
17+
DIR_ANDROID_APP_DATA, // Directory where to put Android app's data.
18+
DIR_ANDROID_EXTERNAL_STORAGE, // Android external storage directory.
19+
20+
PATH_ANDROID_END
21+
};
22+
23+
} // namespace base
24+
25+
#endif // BASE_BASE_PATHS_ANDROID_H_

inc/base/hash_tables.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@
3232
#define BASE_HASH_NAMESPACE stdext
3333

3434
#elif defined(COMPILER_GCC)
35-
#if defined(OS_ANDROID)
36-
#define BASE_HASH_NAMESPACE std
37-
#else
3835
#define BASE_HASH_NAMESPACE __gnu_cxx
39-
#endif
36+
4037

4138
// This is a hack to disable the gcc 4.4 warning about hash_map and hash_set
4239
// being deprecated. We can get rid of this when we upgrade to VS2008 and we
@@ -47,8 +44,8 @@
4744
#endif
4845

4946
#if defined(OS_ANDROID)
50-
#include <hash_map>
51-
#include <hash_set>
47+
#include <ext/hash_map>
48+
#include <ext/hash_set>
5249
#else
5350
#include <ext/hash_map>
5451
#include <ext/hash_set>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "base/android/base_jni_registrar.h"
6+
7+
#include "base/basictypes.h"
8+
#include "base/message_pump_android.h"
9+
#include "base/android/build_info.h"
10+
#include "base/android/jni_android.h"
11+
#include "base/android/jni_registrar.h"
12+
#include "base/android/locale_utils.h"
13+
#include "base/android/path_service_android.h"
14+
#include "base/android/path_utils.h"
15+
16+
namespace base {
17+
namespace android {
18+
19+
static RegistrationMethod kBaseRegisteredMethods[] = {
20+
{ "BuildInfo", base::android::BuildInfo::RegisterBindings },
21+
{ "LocaleUtils", base::android::RegisterLocaleUtils },
22+
{ "PathService", base::android::RegisterPathService },
23+
{ "PathUtils", base::android::RegisterPathUtils },
24+
{ "SystemMessageHandler", base::MessagePumpForUI::RegisterBindings },
25+
};
26+
27+
bool RegisterJni(JNIEnv* env) {
28+
return RegisterNativeMethods(env, kBaseRegisteredMethods,
29+
arraysize(kBaseRegisteredMethods));
30+
}
31+
32+
} // namespace android
33+
} // namespace base
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef BASE_ANDROID_BASE_JNI_REGISTRAR_H_
6+
#define BASE_ANDROID_BASE_JNI_REGISTRAR_H_
7+
8+
#include <jni.h>
9+
10+
namespace base {
11+
namespace android {
12+
13+
// Register all JNI bindings necessary for base.
14+
bool RegisterJni(JNIEnv* env);
15+
16+
} // namespace android
17+
} // namespace base
18+
19+
#endif // BASE_ANDROID_BASE_JNI_REGISTRAR_H_

src/base/android/build_info.cc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "base/android/build_info.h"
6+
7+
#include <string>
8+
9+
#include "base/android/jni_android.h"
10+
#include "base/android/jni_string.h"
11+
#include "base/android/scoped_java_ref.h"
12+
#include "base/logging.h"
13+
#include "base/memory/singleton.h"
14+
#include "jni/BuildInfo_jni.h"
15+
16+
namespace {
17+
18+
// The caller takes ownership of the returned const char*.
19+
const char* StrDupJString(const base::android::JavaRef<jstring>& java_string) {
20+
std::string str = ConvertJavaStringToUTF8(java_string);
21+
return strdup(str.c_str());
22+
}
23+
24+
} // namespace
25+
26+
namespace base {
27+
namespace android {
28+
29+
struct BuildInfoSingletonTraits {
30+
static BuildInfo* New() {
31+
return new BuildInfo(AttachCurrentThread());
32+
}
33+
34+
static void Delete(BuildInfo* x) {
35+
// We're leaking this type, see kRegisterAtExit.
36+
NOTREACHED();
37+
}
38+
39+
static const bool kRegisterAtExit = false;
40+
static const bool kAllowedToAccessOnNonjoinableThread = true;
41+
};
42+
43+
BuildInfo::BuildInfo(JNIEnv* env)
44+
: device_(StrDupJString(Java_BuildInfo_getDevice(env))),
45+
model_(StrDupJString(Java_BuildInfo_getDeviceModel(env))),
46+
brand_(StrDupJString(Java_BuildInfo_getBrand(env))),
47+
android_build_id_(StrDupJString(Java_BuildInfo_getAndroidBuildId(env))),
48+
android_build_fp_(StrDupJString(
49+
Java_BuildInfo_getAndroidBuildFingerprint(env))),
50+
package_version_code_(StrDupJString(Java_BuildInfo_getPackageVersionCode(
51+
env, GetApplicationContext()))),
52+
package_version_name_(StrDupJString(Java_BuildInfo_getPackageVersionName(
53+
env, GetApplicationContext()))),
54+
package_label_(StrDupJString(Java_BuildInfo_getPackageLabel(
55+
env, GetApplicationContext()))),
56+
java_exception_info_(NULL) {
57+
}
58+
59+
// static
60+
BuildInfo* BuildInfo::GetInstance() {
61+
return Singleton<BuildInfo, BuildInfoSingletonTraits >::get();
62+
}
63+
64+
void BuildInfo::set_java_exception_info(const std::string& info) {
65+
DCHECK(!java_exception_info_) << "info should be set only once.";
66+
java_exception_info_ = strndup(info.c_str(), 1024);
67+
}
68+
69+
// static
70+
bool BuildInfo::RegisterBindings(JNIEnv* env) {
71+
return RegisterNativesImpl(env);
72+
}
73+
74+
} // namespace android
75+
} // namespace base

src/base/android/build_info.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef BASE_ANDROID_BUILD_INFO_H_
6+
#define BASE_ANDROID_BUILD_INFO_H_
7+
8+
#include <jni.h>
9+
10+
#include <string>
11+
12+
#include "base/memory/singleton.h"
13+
14+
namespace base {
15+
namespace android {
16+
17+
// BuildInfo is a singleton class that stores android build and device
18+
// information. It will be called from Android specific code and gets used
19+
// primarily in crash reporting.
20+
21+
// It is also used to store the last java exception seen during JNI.
22+
// TODO(nileshagrawal): Find a better place to store this info.
23+
class BuildInfo {
24+
public:
25+
26+
~BuildInfo() {}
27+
28+
// Static factory method for getting the singleton BuildInfo instance.
29+
// Note that ownership is not conferred on the caller and the BuildInfo in
30+
// question isn't actually freed until shutdown. This is ok because there
31+
// should only be one instance of BuildInfo ever created.
32+
static BuildInfo* GetInstance();
33+
34+
// Const char* is used instead of std::strings because these values must be
35+
// available even if the process is in a crash state. Sadly
36+
// std::string.c_str() doesn't guarantee that memory won't be allocated when
37+
// it is called.
38+
const char* device() const {
39+
return device_;
40+
}
41+
42+
const char* model() const {
43+
return model_;
44+
}
45+
46+
const char* brand() const {
47+
return brand_;
48+
}
49+
50+
const char* android_build_id() const {
51+
return android_build_id_;
52+
}
53+
54+
const char* android_build_fp() const {
55+
return android_build_fp_;
56+
}
57+
58+
const char* package_version_code() const {
59+
return package_version_code_;
60+
}
61+
62+
const char* package_version_name() const {
63+
return package_version_name_;
64+
}
65+
66+
const char* package_label() const {
67+
return package_label_;
68+
}
69+
70+
const char* java_exception_info() const {
71+
return java_exception_info_;
72+
}
73+
74+
void set_java_exception_info(const std::string& info);
75+
76+
static bool RegisterBindings(JNIEnv* env);
77+
78+
private:
79+
friend struct BuildInfoSingletonTraits;
80+
81+
explicit BuildInfo(JNIEnv* env);
82+
83+
// Const char* is used instead of std::strings because these values must be
84+
// available even if the process is in a crash state. Sadly
85+
// std::string.c_str() doesn't guarantee that memory won't be allocated when
86+
// it is called.
87+
const char* const device_;
88+
const char* const model_;
89+
const char* const brand_;
90+
const char* const android_build_id_;
91+
const char* const android_build_fp_;
92+
const char* const package_version_code_;
93+
const char* const package_version_name_;
94+
const char* const package_label_;
95+
// This is set via set_java_exception_info, not at constructor time.
96+
const char* java_exception_info_;
97+
98+
DISALLOW_COPY_AND_ASSIGN(BuildInfo);
99+
};
100+
101+
} // namespace android
102+
} // namespace base
103+
104+
#endif // BASE_ANDROID_BUILD_INFO_H_
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
package org.chromium.base;
6+
7+
import java.lang.annotation.ElementType;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.RetentionPolicy;
10+
import java.lang.annotation.Target;
11+
12+
/**
13+
* @AccessedByNative is used to ensure proguard will keep this field, since it's
14+
* only accessed by native.
15+
*/
16+
@Target(ElementType.FIELD)
17+
@Retention(RetentionPolicy.RUNTIME)
18+
public @interface AccessedByNative {
19+
public String value() default "";
20+
}

0 commit comments

Comments
 (0)