2016-04-22 62 views
0

我想在Android設備上執行jGit庫。我想創建本地存儲庫。Android設備上的jGit使用情況

該代碼正在PC:

File fRepositoryDir = new File("D:/My/jgittestrpo"); 
if (!fRepositoryDir.exists()) { 
    fRepositoryDir.mkdirs(); 
} 

Repository localRepo; 
Git git; 
try { 
    localRepo = new FileRepository(fRepositoryDir.getAbsolutePath() + "/.git"); 
    localRepo.create(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

這在Android設備上相同的代碼不工作:

File fRepositoryDir = new File(getFilesDir().getAbsolutePath() + File.separator + "repository"); 
//File fRepositoryDir = new File(Environment.getExternalStorageDirectory() + File.separator + "repository"); // Trying on sd card, not working too 
if (!fRepositoryDir.exists()) { 
    fRepositoryDir.mkdirs(); 
} 

Repository localRepository; 
try { 
    localRepository = new FileRepository(fRepositoryDir.getAbsolutePath() + File.separator + ".git"); 
    //localRepository = new FileRepository("/data/data/com.examplecompany.gitexample/files/repository/.git");   // Trying to set repo folder directly 
    localRepository.create(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

異常Android設備上:

FATAL EXCEPTION: main 
Process: com.examplecompany.gitexample, PID: 2036 
java.lang.IllegalStateException: Could not execute method of the activity 
    at android.view.View$1.onClick(View.java:3823) 
    at android.view.View.performClick(View.java:4438) 
    at android.view.View$PerformClick.run(View.java:18422) 
    at android.os.Handler.handleCallback(Handler.java:733) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5001) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.reflect.InvocationTargetException 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at android.view.View$1.onClick(View.java:3818) 
    ... 11 more 
Caused by: java.lang.VerifyError: org/eclipse/jgit/internal/storage/file/ObjectDirectory 
    at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:221) 
    at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:145) 
    at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:159) 
    at com.examplecompany.gitexample.MainActivity.onLoginClick(MainActivity.java:173) 
    ... 14 more 

在哪裏的問題?可能是我使用不正確的庫?

+0

你在我們的android設備上安裝了git嗎?我不這樣做。 – CodeWizard

+0

不,我沒有在Android設備上安裝git ...必須採取什麼措施在Android設備上創建本地存儲庫? – Orgatres

回答

1

Git有一些Android客戶端。

下面是對這些工具的部分列表:

從市場上的另一種應用:

+0

這是悲傷的... – Orgatres

+0

很抱歉,但沒有什麼可以做的。 Git只在win/unix/mac等版本上發佈,不適用於移動平臺。 – CodeWizard

+1

感謝您的解釋) – Orgatres

1

它可以做到!查看爲Android提供git-client的agit application的源代碼。

我沒有仔細檢查它,但似乎使用補丁版本的JGit和Repository類的不同實現來避免Android上不可能實現的直接文件訪問。

它可能有一些限制,即現在沒有提交/寫入支持,但至少它可以顯示基本訪問是如何可能的,並且如果您需要更多功能,您可以從那裏構建它。

相關問題