Gradle sync in Android Studio works fine, but any console build with option

--offline

fails, because of missing cached repository. I can't go online to test an online sync.

I've tried different build options, defined

--gradle-user-home

or

--project-cache-dir

without success.

This is the exception

* What went wrong:
A problem occurred configuring root project 'My-Project'.
> Could not resolve all dependencies for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:2.2.2.
Required by:
project :
> No cached version of com.android.tools.build:gradle:2.2.2 available for offline mode.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Is it not possible to reuse the cached repository of the Android Studio gradle sync? There are so many project folders which seems to contain the cached gradle files, but which one is it? I can see

I needed to get sure the path to the gradle cache has been defined and it is the same for Android Studio and the console build.

The easiest way is to define path to gradle home:

export GRADLE_USER_HOME=/path/to/gradle/local/cache

I created a dummy project on a computer with internet access, executed a gradle online sync. After that, switched into offline mode in Android Studio and executed ./gradlew --offline in the project folder. Build was successful.

Now, it is possible to create a new project still in offline mode (internet access can be switched off) and use the cached gradle build.