2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef _RUNTIME_ANDROID_RUNTIME_H
20 #define _RUNTIME_ANDROID_RUNTIME_H
22 #include <utils/Errors.h>
23 #include <binder/IBinder.h>
24 #include <utils/String8.h>
25 #include <utils/String16.h>
26 #include <utils/Vector.h>
27 #include <utils/threads.h>
29 #include <nativehelper/jni.h>
37 AndroidRuntime(char* argBlockStart, size_t argBlockSize);
38 virtual ~AndroidRuntime();
47 void setArgv0(const char* argv0);
48 void addOption(const char* optionString, void* extra_info = NULL);
51 * Register a set of methods in the specified class.
53 static int registerNativeMethods(JNIEnv* env,
54 const char* className, const JNINativeMethod* gMethods, int numMethods);
57 * Call a class's static main method with the given arguments,
59 status_t callMain(const String8& className, jclass clazz, const Vector<String8>& args);
62 * Find a class, with the input either of the form
63 * "package/class" or "package.class".
65 static jclass findClass(JNIEnv* env, const char* className);
67 void start(const char *classname, const Vector<String8>& options);
71 void setExitWithoutCleanup(bool exitWithoutCleanup) {
72 mExitWithoutCleanup = exitWithoutCleanup;
75 static AndroidRuntime* getRuntime();
78 * This gets called after the VM has been created, but before we
79 * run any code. Override it to make any FindClass calls that need
82 virtual void onVmCreated(JNIEnv* env);
85 * This gets called after the JavaVM has initialized. Override it
86 * with the system's native entry point.
88 virtual void onStarted() = 0;
91 * This gets called after the JavaVM has initialized after a Zygote
92 * fork. Override it to initialize threads, etc. Upon return, the
93 * correct static main will be invoked.
95 virtual void onZygoteInit() { }
98 * Called when the Java application exits to perform additional cleanup actions
99 * before the process is terminated.
101 virtual void onExit(int code) { }
103 /** create a new thread that is visible from Java */
104 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
107 /** return a pointer to the VM running in this process */
108 static JavaVM* getJavaVM() { return mJavaVM; }
110 /** return a pointer to the JNIEnv pointer for this thread */
111 static JNIEnv* getJNIEnv();
113 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
114 static char* toSlashClassName(const char* className);
116 /** Create a Java string from an ASCII or Latin-1 string */
117 static jstring NewStringLatin1(JNIEnv* env, const char* bytes);
120 static int startReg(JNIEnv* env);
121 bool parseRuntimeOption(const char* property,
123 const char* runtimeArg,
124 const char* defaultArg = "");
125 bool parseCompilerOption(const char* property,
127 const char* compilerArg,
128 const char* quotingArg);
129 bool parseCompilerRuntimeOption(const char* property,
131 const char* runtimeArg,
132 const char* quotingArg);
133 void parseExtraOpts(char* extraOptsBuf, const char* quotingArg);
134 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
136 Vector<JavaVMOption> mOptions;
137 bool mExitWithoutCleanup;
138 char* const mArgBlockStart;
139 const size_t mArgBlockLength;
141 /* JNI JavaVM pointer */
142 static JavaVM* mJavaVM;
145 * Thread creation helpers.
147 static int javaCreateThreadEtc(
148 android_thread_func_t entryFunction,
150 const char* threadName,
151 int32_t threadPriority,
152 size_t threadStackSize,
153 android_thread_id_t* threadId);
154 static int javaThreadShell(void* args);