From 064277a81053356cda3c276aaf22034e7daf785e Mon Sep 17 00:00:00 2001 From: Spencer Brown <spencer@privategrity.com> Date: Tue, 13 Feb 2018 11:26:39 -0800 Subject: [PATCH] Get sample Android app talking to go library working --- .gitignore | 1 + README.md | 21 ++++++++++++---- android/.idea/gradle.xml | 1 + android/.idea/misc.xml | 24 +++++++++++++++++++ android/.idea/modules.xml | 1 + android/app/build.gradle | 3 ++- .../messagingclient/MainActivity.java | 9 +++++++ .../app/src/main/res/layout/activity_main.xml | 9 ++++--- android/client/build.gradle | 2 ++ android/settings.gradle | 2 +- hello.go | 7 ++++++ 11 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 android/client/build.gradle create mode 100644 hello.go diff --git a/.gitignore b/.gitignore index d0b369a49..30f29f0e7 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ localdev_* *.dex *.class *.aar +*.jar diff --git a/README.md b/README.md index a2b2c7ba5..9b1d0a48d 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,22 @@ In either case you will need to run $ gomobile init. Building == -To build the .aar for the client, run +To build the .aar for the client, cd to privategrity/client/android/client and +run this command: -$ gomobile bind +$ gomobile bind -target=android gitlab.com/privategrity/client + +Adding the .aar to the Android Studio project +== + +In case you need to add another .aar to the Android Studio project, follow +these steps: + +1. Go to File-\>New-\>New Module. +1. Scroll to and click on Import .JAR/.AAR Package. +1. Pick the .aar in the file chooser. +1. Click through the rest of the wizard. + +In any case, this isn't a recommended course of action because there might be +some weirdness about gomobile generating more than one .aar. -Then import the .aar in Android Studio. For future builds, you'll need to copy -the new bindings .aar over the .aar that's been imported to Android Studio. diff --git a/android/.idea/gradle.xml b/android/.idea/gradle.xml index 7ac24c777..875fe877d 100644 --- a/android/.idea/gradle.xml +++ b/android/.idea/gradle.xml @@ -9,6 +9,7 @@ <set> <option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$/app" /> + <option value="$PROJECT_DIR$/client" /> </set> </option> <option name="resolveModulePerSourceSet" value="false" /> diff --git a/android/.idea/misc.xml b/android/.idea/misc.xml index 37a750962..396387992 100644 --- a/android/.idea/misc.xml +++ b/android/.idea/misc.xml @@ -1,5 +1,29 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> + <component name="NullableNotNullManager"> + <option name="myDefaultNullable" value="android.support.annotation.Nullable" /> + <option name="myDefaultNotNull" value="android.support.annotation.NonNull" /> + <option name="myNullables"> + <value> + <list size="4"> + <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" /> + <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" /> + <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" /> + <item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" /> + </list> + </value> + </option> + <option name="myNotNulls"> + <value> + <list size="4"> + <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" /> + <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" /> + <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" /> + <item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" /> + </list> + </value> + </option> + </component> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/build/classes" /> </component> diff --git a/android/.idea/modules.xml b/android/.idea/modules.xml index abc31fc67..4987fb25c 100644 --- a/android/.idea/modules.xml +++ b/android/.idea/modules.xml @@ -4,6 +4,7 @@ <modules> <module fileurl="file://$PROJECT_DIR$/android.iml" filepath="$PROJECT_DIR$/android.iml" /> <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> + <module fileurl="file://$PROJECT_DIR$/client/client.iml" filepath="$PROJECT_DIR$/client/client.iml" /> </modules> </component> </project> \ No newline at end of file diff --git a/android/app/build.gradle b/android/app/build.gradle index bb62dc6c2..00c8d3e3c 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -19,10 +19,11 @@ android { } dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' + implementation project(':client') } diff --git a/android/app/src/main/java/com/privategrity/messagingclient/MainActivity.java b/android/app/src/main/java/com/privategrity/messagingclient/MainActivity.java index 144abb2f3..d278844e9 100644 --- a/android/app/src/main/java/com/privategrity/messagingclient/MainActivity.java +++ b/android/app/src/main/java/com/privategrity/messagingclient/MainActivity.java @@ -2,12 +2,21 @@ package com.privategrity.messagingclient; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.widget.TextView; + +import client.Client; public class MainActivity extends AppCompatActivity { + private TextView textView; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + textView = (TextView) findViewById(R.id.mytextview); + + String greetings = Client.greetings("Android and Gopher"); + textView.setText(greetings); } } diff --git a/android/app/src/main/res/layout/activity_main.xml b/android/app/src/main/res/layout/activity_main.xml index b27ecbcb0..b4175c8ee 100644 --- a/android/app/src/main/res/layout/activity_main.xml +++ b/android/app/src/main/res/layout/activity_main.xml @@ -7,12 +7,11 @@ tools:context="com.privategrity.messagingclient.MainActivity"> <TextView + android:id="@+id/mytextview" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Hello World!" - app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintLeft_toLeftOf="parent" - app:layout_constraintRight_toRightOf="parent" - app:layout_constraintTop_toTopOf="parent" /> + android:text="TextView" + tools:layout_editor_absoluteX="16dp" + tools:layout_editor_absoluteY="16dp" /> </android.support.constraint.ConstraintLayout> diff --git a/android/client/build.gradle b/android/client/build.gradle new file mode 100644 index 000000000..2d28884b3 --- /dev/null +++ b/android/client/build.gradle @@ -0,0 +1,2 @@ +configurations.maybeCreate("default") +artifacts.add("default", file('client.aar')) \ No newline at end of file diff --git a/android/settings.gradle b/android/settings.gradle index e7b4def49..48a8eb7a4 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -1 +1 @@ -include ':app' +include ':app', ':client' diff --git a/hello.go b/hello.go new file mode 100644 index 000000000..65ca75a1a --- /dev/null +++ b/hello.go @@ -0,0 +1,7 @@ +package client + +import "fmt" + +func Greetings(name string) string { + return fmt.Sprintf("Hello, %s!", name) +} -- GitLab