60 lines of FAIL

Post date: Jan 07, 2013 8:26:6 AM

From Android's NativeActivity:

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        String libname = "main";

        String funcname = "ANativeActivity_onCreate";

        ActivityInfo ai;

        

        mIMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

        mInputMethodCallback = new InputMethodCallback(this);

        getWindow().takeSurface(this);

        getWindow().takeInputQueue(this);

        getWindow().setFormat(PixelFormat.RGB_565);

        getWindow().setSoftInputMode(

                WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED

                | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        mNativeContentView = new NativeContentView(this);

        mNativeContentView.mActivity = this;

        setContentView(mNativeContentView);

        mNativeContentView.requestFocus();

        mNativeContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);

        

        try {

            ai = getPackageManager().getActivityInfo(

                    getIntent().getComponent(), PackageManager.GET_META_DATA);

            if (ai.metaData != null) {

                String ln = ai.metaData.getString(META_DATA_LIB_NAME);

                if (ln != null) libname = ln;

                ln = ai.metaData.getString(META_DATA_FUNC_NAME);

                if (ln != null) funcname = ln;

            }

        } catch (PackageManager.NameNotFoundException e) {

            throw new RuntimeException("Error getting activity info", e);

        }

        

        String path = null;

        

        File libraryFile = new File(ai.applicationInfo.nativeLibraryDir,

                System.mapLibraryName(libname));

        if (libraryFile.exists()) {

            path = libraryFile.getPath();

        }

        

        if (path == null) {

            throw new IllegalArgumentException("Unable to find native library: " + libname);

        }

        

        byte[] nativeSavedState = savedInstanceState != null

                ? savedInstanceState.getByteArray(KEY_NATIVE_SAVED_STATE) : null;

        mNativeHandle = loadNativeCode(path, funcname, Looper.myQueue(),

                 getFilesDir().toString(), getObbDir().toString(),

                 Environment.getExternalStorageAppFilesDirectory(ai.packageName).toString(),

                 Build.VERSION.SDK_INT, getAssets(), nativeSavedState);

        

        if (mNativeHandle == 0) {

            throw new IllegalArgumentException("Unable to load native library: " + path);

        }

        super.onCreate(savedInstanceState);

    }

Thanks, Google. I wanted to dynamically download a library to use with NativeActivity. Of course, the nativeLibraryDir is read only, and the code effectively prevents me from putting the file anywhere else. I can't even bypass the code, as it overrides the onCreate call, which needs to be called when initializing an activity.

tl;dr: Android is stupid sometimes.