Skip to content

Android ExampleApp cannot be opened in Android Studio if it's moved out from the sdk directory tree #2743

@muzso

Description

@muzso

Describe the bug

The android ExampleApp's readme says:

In case you want to copy the example to a different path of your choice, you need to place a copy of the SDK in the folder <your_path>/ExampleApp/app/src/main/jni/mega (or you can clone the repository, so you can keep the SDK up to date).

If you do this, then you'll run into an issue with how Android Studio resolves invalid pathes.

The build.gradle of ExampleApp contains the following:

android {
(...)
    sourceSets.main {
        java {
            srcDirs += '../../../../bindings/java/nz/mega/sdk'
            srcDirs += '../../../../../../../../main/java/'
            exclude '**/MegaApiSwing.java'
        }
        jni.srcDirs = [] //disable automatic ndk-build
        jniLibs.srcDir 'src/main/libs' // This is not necessary unless you have precompiled libraries in your project.
    }
(...)
}

The first relative path (../../../../bindings/java/nz/mega/sdk) works only as long the ExampleApp remains in the sdk repo's directory structure. It climbs up to the sdk's directory and references the generated java bindings dir.
The second relative path (../../../../../../../../main/java/) doesn't seem to make sense at all, even if you compile ExampleApp inside the sdk directory tree.
If you move the ExampleApp folder somewhere else, things start to behave erratically.

Android Studio doesn't just fail to import the project if it hits a relative path that cannot be resolved properly.
It tries to "make sense" of the path by using the longest prefix of it that still resolves to an existing directory.

Thus if you move ExampleApp e.g. into /home/myuser/src/ExampleApp, then the longest valid prefix of both relative pathes will be '../../../..', which will point to '/', i.e. the root of the given filesystem. And as a result Android Studio starts to index the root folder recursively!

It'll also give the following warning:

External file changes sync may be slow.
Android Studio cannot receive filesystem event notifications for the project. Is it on a network drive?

Meanwhile Android Studio gets seemingly stuck with "Updating indexes ...", but actually it pushes the CPU load to its limits by trying to index the entire filesystem.

The second relative path (i.e. srcDirs += '../../../../../../../../main/java/') should be removed alltogether.
The first one (srcDirs += '../../../../bindings/java/nz/mega/sdk') must be changed to srcDirs += 'src/main/jni/mega/sdk/bindings/java/nz/mega/sdk' if you move ExampleApp and place the sdk into the src/main/jni/mega/sdk folder (as instructed by the readme).

To Reproduce

Steps to reproduce the behavior:

  1. Checkout sdk repo.
  2. Move <sdk>/examples/android/ExampleApp to e.g. $HOME/ExampleApp.
  3. Try to open the ExampleApp folder in Android Studio.

Expected behavior

Project is imported into Android Studio with a couple of warnings/errors.
The reason for the latter is that we didn't build the sdk yet, thus we don't have the necessary java bindings compiled yet.

Screenshots

I don't think this is necessary, since the bug/issue (and a potential fix) is well described. And it's trivially reproducible.

Environment:

  • SDK Version: v9.16.1
  • OS: Ubuntu 22.04.5 LTS
  • Android Studio: Narwhal Feature Drop (2025.1.2 Canary 4)
  • Device: Android

Additional context

No additional context.

Metadata

Metadata

Assignees

Labels

bugUnexpected software behaviour

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions