4

I am trying clone a repository on Android Java but I get an error when I call the method toPath(). Apparently the File object has no toPath() method.

public void gitClone() throws GitAPIException {

    // File dir = new File(_ctx.getExternalFilesDir(null) + File.separator + "openshift_files");
    Bundle b2 = getIntent().getExtras();
    //File localPath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
    final File localPath = new File(getExternalFilesDir(null) + File.separator + getCurrentApp().getName());

    Git.cloneRepository()
            .setURI(getUrlGit())
            .setDirectory(localPath)
            .setCredentialsProvider(new UsernamePasswordCredentialsProvider(b2.getString("OPEN_SHIFT_USER"), b2.getString("OPEN_SHIFT_PASSWORD")))
            .call();
    System.out.println("Success!");
}

This is the stack trace

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #5
E/AndroidRuntime: Process: com.javier.openshift, PID: 22919
E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:304)
E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
E/AndroidRuntime:  Caused by: java.lang.NoSuchMethodError: No virtual method toPath()Ljava/nio/file/Path; in class Ljava/io/File; or its super classes (declaration of 'java.io.File' appears in /system/framework/core-libart.jar)
E/AndroidRuntime:     at org.eclipse.jgit.util.FileUtil.exists(FileUtil.java:149)
E/AndroidRuntime:     at org.eclipse.jgit.util.FS_POSIX.exists(FS_POSIX.java:275)
E/AndroidRuntime:     at org.eclipse.jgit.internal.storage.file.ObjectDirectory.exists(ObjectDirectory.java:193)
E/AndroidRuntime:     at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:207)
E/AndroidRuntime:     at org.eclipse.jgit.lib.BaseRepositoryBuilder.build(BaseRepositoryBuilder.java:579)
E/AndroidRuntime:     at org.eclipse.jgit.api.InitCommand.call(InitCommand.java:113)
E/AndroidRuntime:     at org.eclipse.jgit.api.CloneCommand.init(CloneCommand.java:161)
E/AndroidRuntime:     at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:132)
E/AndroidRuntime:     at com.javier.apps.AppsActivity.gitClone(AppsActivity.java:981)
E/AndroidRuntime:     at com.javier.apps.AppsActivity$AsyncTaskCloneRepository.doInBackground(AppsActivity.java:1002)
E/AndroidRuntime:     at com.javier.apps.AppsActivity$AsyncTaskCloneRepository.doInBackground(AppsActivity.java:985)
E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 
Tunaki
  • 132,869
  • 46
  • 340
  • 423
user3154785
  • 121
  • 1
  • 6

2 Answers2

4

You are using a too recent version of JGit. Starting from JGit 4.0.0, the minimum Java version was bumped from Java 5 to Java 7 (see Eclipse documentation here). JGit is using the Java 7 Path API and it is not available for the JDK you are working with.

You should downgrade to JGit 3.7.1 (which is the lastest of the 3.x line).

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • I am working with JGit 4.1.0 in NetBeans with the same JDK and works fine! – user3154785 Sep 30 '15 at 19:05
  • @user3154785 Something to do with Android then (I don't know Android that well). But downgrading will most probably solve your problem. – Tunaki Sep 30 '15 at 19:12
1

Even when using Java 7, java.nio.file.Path is not available in Android along with other nio classes that JGit depends on. More details here:

Android import java.nio.file.Files; cannot be resolved

So @Tunaki's answer is still correct, but for a different reason. Unfortunately on Android we are locked to JGit 3.7.1 until those nio classes become available, even when using Java 7.

yuval
  • 6,369
  • 3
  • 32
  • 44