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);
50 * Register a set of methods in the specified class.
52 static int registerNativeMethods(JNIEnv* env,
53 const char* className, const JNINativeMethod* gMethods, int numMethods);
56 * Call a class's static main method with the given arguments,
58 status_t callMain(const String8& className, jclass clazz, const Vector<String8>& args);
61 * Find a class, with the input either of the form
62 * "package/class" or "package.class".
64 static jclass findClass(JNIEnv* env, const char* className);
66 int addVmArguments(int argc, const char* const argv[]);
68 void start(const char *classname, const Vector<String8>& options);
72 void setExitWithoutCleanup(bool exitWithoutCleanup) {
73 mExitWithoutCleanup = exitWithoutCleanup;
76 static AndroidRuntime* getRuntime();
79 * This gets called after the VM has been created, but before we
80 * run any code. Override it to make any FindClass calls that need
83 virtual void onVmCreated(JNIEnv* env);
86 * This gets called after the JavaVM has initialized. Override it
87 * with the system's native entry point.
89 virtual void onStarted() = 0;
92 * This gets called after the JavaVM has initialized after a Zygote
93 * fork. Override it to initialize threads, etc. Upon return, the
94 * correct static main will be invoked.
96 virtual void onZygoteInit() { }
99 * Called when the Java application exits to perform additional cleanup actions
100 * before the process is terminated.
102 virtual void onExit(int code) { }
104 /** create a new thread that is visible from Java */
105 static android_thread_id_t createJavaThread(const char* name, void (*start)(void *),
108 /** return a pointer to the VM running in this process */
109 static JavaVM* getJavaVM() { return mJavaVM; }
111 /** return a pointer to the JNIEnv pointer for this thread */
112 static JNIEnv* getJNIEnv();
114 /** return a new string corresponding to 'className' with all '.'s replaced by '/'s. */
115 static char* toSlashClassName(const char* className);
118 static int startReg(JNIEnv* env);
119 bool parseRuntimeOption(const char* property,
121 const char* runtimeArg,
122 const char* defaultArg = "");
123 bool parseCompilerRuntimeOption(const char* property,
125 const char* runtimeArg,
126 const char* quotingArg);
127 void parseExtraOpts(char* extraOptsBuf, const char* quotingArg);
128 int startVm(JavaVM** pJavaVM, JNIEnv** pEnv);
130 Vector<JavaVMOption> mOptions;
131 bool mExitWithoutCleanup;
132 char* const mArgBlockStart;
133 const size_t mArgBlockLength;
135 /* JNI JavaVM pointer */
136 static JavaVM* mJavaVM;
139 * Thread creation helpers.
141 static int javaCreateThreadEtc(
142 android_thread_func_t entryFunction,
144 const char* threadName,
145 int32_t threadPriority,
146 size_t threadStackSize,
147 android_thread_id_t* threadId);
148 static int javaThreadShell(void* args);