How to add Jgit to your Android project? - android

I have been trying to add Jgit to my android project for to execute remote push commands. i have tried ajgit and many others and none of them support "remote push".
I have tried including Jgit from source as a module. to encounter "Lambda expressions not supported in Android". gave that up and downloaded the latest jar library from official jgit site and added it as an external library and came across with the error below. which i suppose is a external dependency issue.
Please advice how i can include Jgit or any other library that would allow me to do "remote pushes" in to my android studio project so i can move on to the better part of my project. (using Maven or else.. what i need is a fast solution because my work is kind of halted due to this setback)
thank you in advance.
the code i would like to execute:
try{
String httpUrl = "https://github.com/repo/branch";
String localPath = "/sdcard/Folder1/folder2";
Log.d("GIT","1");
Repository localRepo = new FileRepository(localPath);
Log.d("GIT","2");
Git git = new Git(localRepo);
Log.d("GIT","3");
// add remote repo:
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName("origin");
remoteAddCommand.setUri(new URIish(httpUrl));
// you can add more settings here if needed
remoteAddCommand.call();
// push to remote:
PushCommand pushCommand = git.push();
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider("username", "password"));
// you can add more settings here if needed
pushCommand.call();
}catch (Exception ex){
imessage += "/n"+ex.getMessage();
}
which returns error at line ,
Repository localRepo = new FileRepository(localPath);
E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.crimson.studio.Ruby.Ui, PID: 3847
java.lang.BootstrapMethodError: Exception from call site #145 bootstrap method
at org.eclipse.jgit.util.SystemReader.getOsName(SystemReader.java:372)
at org.eclipse.jgit.util.SystemReader.isWindows(SystemReader.java:350)
at org.eclipse.jgit.util.SystemReader.setPlatformChecker(SystemReader.java:197)
at org.eclipse.jgit.util.SystemReader.init(SystemReader.java:187)
at org.eclipse.jgit.util.SystemReader.<clinit>(SystemReader.java:83)
at org.eclipse.jgit.util.SystemReader.getInstance(SystemReader.java:160)
at org.eclipse.jgit.util.FS$FSFactory.detect(FS.java:131)
at org.eclipse.jgit.util.FS.detect(FS.java:306)
at org.eclipse.jgit.util.FS.detect(FS.java:279)
at org.eclipse.jgit.util.FS.<clinit>(FS.java:269)
at org.eclipse.jgit.lib.BaseRepositoryBuilder.setupWorkTree(BaseRepositoryBuilder.java:657)
at org.eclipse.jgit.lib.BaseRepositoryBuilder.setup(BaseRepositoryBuilder.java:589)
at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:151)
at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:165)
at com.crimson.studio.Ruby.VulkanActivity.showAlert(VulkanActivity.java:58)
Caused by: java.lang.ClassCastException: Bootstrap method returned null
at org.eclipse.jgit.util.SystemReader.getOsName(SystemReader.java:372) 
at org.eclipse.jgit.util.SystemReader.isWindows(SystemReader.java:350) 
at org.eclipse.jgit.util.SystemReader.setPlatformChecker(SystemReader.java:197) 
at org.eclipse.jgit.util.SystemReader.init(SystemReader.java:187) 
at org.eclipse.jgit.util.SystemReader.<clinit>(SystemReader.java:83) 
at org.eclipse.jgit.util.SystemReader.getInstance(SystemReader.java:160) 
at org.eclipse.jgit.util.FS$FSFactory.detect(FS.java:131) 
at org.eclipse.jgit.util.FS.detect(FS.java:306) 
at org.eclipse.jgit.util.FS.detect(FS.java:279) 
at org.eclipse.jgit.util.FS.<clinit>(FS.java:269) 
at org.eclipse.jgit.lib.BaseRepositoryBuilder.setupWorkTree(BaseRepositoryBuilder.java:657) 
at org.eclipse.jgit.lib.BaseRepositoryBuilder.setup(BaseRepositoryBuilder.java:589) 
at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:151) 
at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:165) 
at com.crimson.studio.Ruby.VulkanActivity.showAlert(VulkanActivity.java:58)

It seems JGit is not built for being used on Android and thus causes some deeper problem during startup.
There are some existing Git Client Apps for Android, see e.g. https://www.hongkiat.com/blog/guide-to-using-git-on-android/ and https://livablesoftware.com/mobile-apps-git-github-android-iphone/, but these provide a separate application for using Git on Android and not a library to include in your project.
I did only find https://github.com/rtyley/agit which provides source-code for using Git on Android, although it is unmaintained for some time, but you still might be able to extract a way of using Git in your application from it.

Related

TypeError: undefined is not an object (evaluating 'argv.indexOf')

I'm running a React Native app which has a local Node project as a dependency, managed through yalc. I'm using rn-nodeify to deal with the Node libraries.
Whenever I try using one of the methods/constants from my Node project, I get the above error during react-native start after the app is built (I'm using Intellij's default "Android" configuration to run RN and have an emulator in Android Studio).
I looked up the error message and people say it's related to supports-color, but neither of my projects use that library and installing it didn't fix it either.
Edit:
I'm not sure if this could be related, but I also have to add the lines
import { Buffer } from 'buffer';
import 'reflect-metadata';
global.Buffer = global.Buffer || Buffer
When I use this file from my Node project or I get errors regarding Reflect.hasOwnMetadata and Buffer

AmazonSQSClientBuilder and AWSStaticCredentialsProvider with the Android SDK

I am attempting to implement message sending from my Android app using AWS SQS. I have included aws-android-sdk-core-2.6.15 and aws-android-sdk-sqs-2.6.15 jar files. With just these, I am unable to resolve AmazonSQSClientBuilder (import com.amazonaws.services.sqs.AmazonSQSClientBuilder) and AWSStaticCredentialsProvider (import com.amazonaws.auth.AWSStaticCredentialsProvider).
These work if I include the aws-java-sdk-1.11.278 jar file. However, this causes the 'DuplicateFileException' when I try to build. If I include only this jar, then I get the 'GC overhead limit exceeded' error.
Is there a smaller package that will allow the import of these necessary classes?
Thanks!
The AWS SDK for Android does not follow the same pattern as AWS SDK for Java.
The applicable constructors can be found in these files depending on whether you want an async client or not:
https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-sqs/src/main/java/com/amazonaws/services/sqs/AmazonSQSAsyncClient.java
https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-sqs/src/main/java/com/amazonaws/services/sqs/AmazonSQSClient.java
One example:
AWSCredentialsProvider awsCredentialsProvider = // Choose one of many classes that implement this for instance, CognitoCachingCredentialsProvider
AmazonSQSClient client = new AmazonSQSClient(awsCredentialsProvider);

Runtime error using Xamarin Android with F# and ReactiveUI

I have created a Xamarin Android project that is using F# and ReactiveUI.
When loading my Dashboard, I encounter a runtime exception (of type MissingMethodException) on the inherit line of this code snippet:
type DashboardViewModel(?host: IScreen) =
inherit ReactiveViewModel()
let host = LocateIfNone host
member __.Title with get() = "Dashboard"
interface IRoutableViewModel with
member __.HostScreen = host
member __.UrlPathSegment = "Dashboard"
The error message reads
Method 'Microsoft.FSharp.Quotations.FSharpExpr.Deserialize40' not found.
The ReactiveViewModel type is a thin wrapper around ReactiveObject:
type ReactiveViewModel() as this =
inherit ReactiveObject()
let mutable message = noMessage
let uiContext = SynchronizationContext.Current
member __.SyncContext with get() = uiContext
member this.Message
with get() = message
and set(value) =
this.RaiseAndSetIfChanged(&message, value, "Message") |> ignore
if message <> noMessage then this.RaiseAndSetIfChanged(&message, noMessage, "Message") |> ignore
member val MessageSent =
this.WhenAnyValue(toLinq <# fun vm -> vm.Message #>).ObserveOn(RxApp.MainThreadScheduler).Where(fun m -> m <> noMessage) with get
The project is open source: at the moment, it contains very little content. It can be found at https://github.com/SpiegelSoft/Astrid.
I have submitted a bug on the Xamarin bug tracker: https://bugzilla.xamarin.com/show_bug.cgi?id=51000
Are there any known fixes I can implement myself, so that I can close the bug of my own accord?
UPDATE 1
I've been investigating this issue this weekend.
The FSharp.Core version that is loaded is stuck on the obsolete version 2.3.98.1. This corresponds to the FSharp.Core.dll file in
C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework\MonoAndroid\v1.0
I have tried to remove this version and load the NuGet package FSharp.Core; however, when I build the Android project, the path always reverts to the obsolete file in the Reference Assemblies path.
Is there a way to override this behaviour?
UPDATE 2
Replacing the FSharp.Core.dll file in the Reference Assemblies path fixes the issue, but this is a very unsatisfactory sticking plaster, which I can't ask my users to apply. Ideally, I would like to find a way to prevent the .Droid project from loading FSharp.Core from the GAC rather than the NuGet package.
I just ran into a very similar issue the other day. My android app had a reference to a Profile7 F# PCL library, which made use of List.unfold, which I believe was introduced in F# 4. When I used the library in my app, I saw a MissingMethodException similar to what you are seeing. The version of FSharp.Core that Xamarin references by default when creating a new Android app didn't have this newer method. I got around it by editing the .fsproj file for the app to remove the original reference to FSharp.Core, and replaced it with a reference to the newer version ( I copy/pasted the tag from the PCL .fsproj file). It looks something like this:
<Reference Include="FSharp.Core">
<Name>FSharp.Core</Name>
<Private>True</Private>
<AssemblyName>FSharp.Core.dll</AssemblyName>
<HintPath>$(MSBuildExtensionsPath32)\..\Reference Assemblies\Microsoft\FSharp\.NETCore\$(TargetFSharpCoreVersion)\FSharp.Core.dll</HintPath>
</Reference>
I was suprised to find that this seems to have fixed the problem for me. I'm not sure if I'll run into other issues down the line, but it may be worth trying this if you havent already.
UPDATE If this doesn't work immediately, follow the sequence of steps in Rob Lyndon's answer.
It appears that this has been fixed by the GitHub commit
https://github.com/xamarin/xamarin-android/commit/df41af046000556ed82f638e8041b7f718966a92
which removes FSharp.Core from the list of framework assemblies, and allows the project to be built without the NuGet FSharp.Core assembly being replaced.
Until this fix is released into the SDK, there is a workaround. The answer submitted by user3850711 works, but you need to apply the changes in a specific sequence, because otherwise the reference will be overwritten during the build.
Delete the existing reference to FSharp.Core.
Install or reinstall the FSharp.Core NuGet package.
Unload the project and add <HintPath>packages\FSharp.Core.4.0.0.1\lib\portable-net45+monoandroid10+monotouch10+xamarinios10\FSharp.Core.dll</HintPath> to the FSharp.Core project reference.

Setting up JNA in Android Studio

I try to import jna.jar into my project since JNA is a very useful tool to call Native library which is base on JNI.
OS: Windows 10
IDE: Android Studio 1.5.1
JDK: 1.8.0_73
NDK: r10e
What I have done
(AS = Android Studio)
Create a new project by AS with API18.
Download jna.jar from their GitHub.
https://github.com/java-native-access/jna
copy jna.jar into project folder.
JNATest\app\libs\jna.jar
In AS, right-click on the icon of jna.jar, choose Add as Library
Wait for few seconds, check the File->Project Structure->app->Dependencies. We do have the jna.jar. (Same as app\build.gradle)
build gradle
Implement JAVA code about JNA in MainActivity.java
Run app on real device Sony Z3 (arm)
Crash by CLibrary.Instance.printf("Hello, JNA");
Error Message on Android Monitor
E/AndroidRuntime: FATAL EXCEPTION: main
Process: i3d.jnatest, PID: 1068
java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/android-arm/libjnidispatch.so) not found in resource path (.)
at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:866)
at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:826)
at com.sun.jna.Native.<clinit>(Native.java:140)
..
... so on
Java code
package i3d.jnatest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CLibrary.Instance.printf("Hello, JNA");
}
public interface CLibrary extends Library
{
CLibrary Instance = (CLibrary) Native.loadLibrary("msvcrt", CLibrary.class);
void printf(String format, Object... args);
}
}
Question
According to error message, I miss /android-arm/libjnidispatch.so in runtime.
Did I put the wrong place for jna.jar?
How should I get and use /android-arm/libjnidispatch.so?
I am a newbie about Android Studio, so maybe misunderstanding something key-point.
For Android, reference the JNA library adding #aar at the end of the string instead of downloading the JNA jar:
https://github.com/java-native-access/jna/blob/master/www/FrequentlyAskedQuestions.md#jna-on-android
Put the .so file in the following directory (when using android studio): yourproject\app\src\main\jniLibs\armeabi-v7a\libjnidispatch.so
Update I: (up to version <= 4.3.0) Since some of you asked where to find the *.so file:
On the official JNA site you will find all the supported architectures (30+) for download:
https://github.com/java-native-access/jna/tree/master/lib/native
Download the jar of the architecture you'd like and open it with some zip tool. In there you'll find the libjnidispatch.so file (of course only for unix architectures. For windows its a dll)
Update II: (starting from version >= 4.4.0)
Use the jna-X.X.0.aar file, supplied by the JNA project
As mentioned in a comment - starting from version 4.4.0 JNA publishes an AAR to maven central with all the libjnidispatch.so's in it. Users have had better luck using gradle than straight maven here, which doesn't always select or properly handle aars.
I found this comment in a file in the library github repo - "If you're using Google's Eclipse plugin then you must manually remove libjnidispatch.so from jna.jar/lib/armeabi and add it into your project's libs/armeabi directory."
Since this file was created in 2012 and Android Studio was still in very early phase and not super popular by that time, I assume it might be a valid note for Eclipse and also for Android Studio. I suggest you try it.

Generate Cloud Endpoint Client Library in Android Studio using Entity Class Design Pattern

While following the steps outlined here :
https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial/
for creating a cloud endpoint, but using Android Studio instead of Eclipse, I am stuck at Step 9 of the Entity Class Design Pattern as described here :
https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial/#ecdp
In Eclipse, there is a right-click-menu-option for "Generate Cloud Endpoint Client library" when you right-click on the app engine project. However, there is no equivalent option in Android Studio (v1.0.0)
Is this an omission on Google's part or am I missing something.
What is the best workaround for generating the cloud endpoint client library from within Android Studio.
Is there a way to do it from the command-line?
I did find the steps for gradle here :
https://cloud.google.com/appengine/docs/java/endpoints/endpoints_tool
and here :
https://cloud.google.com/appengine/docs/java/endpoints/consume_android
but these are much more time-consuming than the single-step process described in the original link for eclipse.
As stated above the libraries are auto-compiled for you, the other point to note that had me confused is where to get the Builder from.
Now as of Android Studio 1.0.1 the original Eclipse instructions are a little out of date for this as well, the "Builder" is no longer buried into the Endpoint class you make. Instead it is rolled into a separate API class to describe the Builder and associated code.
See: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/HelloEndpoints
Endpoint Usage from Android would now look like this:
/* OLD
MyEndpoint.Builder builder = ... */
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against local devappserver
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
#Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
We're working on updating that shopping kart sample to use Android Studio.
In the meantime the documentation for generating endpoints in AS can be found here https://cloud.google.com/tools/android-studio/
There is no 'Generate Cloud Endpoint Client Library' task anymore since it's not needed in the Android Studio workflow. Simply building the project will ensure that the client libraries are available to your android app.
Check out the docs for the appengine gradle plugin https://github.com/GoogleCloudPlatform/gradle-appengine-plugin if you want to be able to manually perform some of the endpoint client library steps from the command line using Gradle.
As Lucien Murray-Pitts explained, the Builder is not in the Endpoint class but in a auto-generated XXXXApi class.
Imagine your java bean is a class called Portfolio under package com.example.backend
You have to add the following import in the AsyncTask class:
import com.example.backend.portfolioApi.PortfolioApi;
and then you can do
PortfolioApi.Builder builder = new PortfolioApi.Builder(....

Categories

Resources