]> sigrok.org Git - sigrok-androidutils.git/commitdiff
Bundle Java bindings into an AAR for ease of use
authorMarcus Comstedt <redacted>
Wed, 7 Dec 2016 19:25:45 +0000 (20:25 +0100)
committerUwe Hermann <redacted>
Fri, 16 Dec 2016 18:22:52 +0000 (19:22 +0100)
.gitignore
AndroidManifest.xml.in [new file with mode: 0644]
Makefile.am
ant/src/org/sigrok/androidutils/ant/CopyLibsTask.java
build.xml
configure.ac
src/org/sigrok/androidutils/SigrokApplication.java [new file with mode: 0644]

index 0d9b94f078777ef78713a83809b9f367aeeb744d..3b15d219a2f37467bc50832a54adef460b78b928 100644 (file)
@@ -14,11 +14,14 @@ Makefile.in
 /ltmain.sh
 /missing
 /ar-lib
+/jni/
 *.jar
 *.class
 *.o
 *.lo
 *.la
+*.aar
+*.pom
 .deps/
 .libs/
 .dirstamp
diff --git a/AndroidManifest.xml.in b/AndroidManifest.xml.in
new file mode 100644 (file)
index 0000000..1256ec4
--- /dev/null
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+
+   This file is part of the sigrok-androidutils project.
+
+   Copyright (C) 2016 Marcus Comstedt <marcus@mc.pp.se>
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="org.sigrok.sigrok-core-android"
+    android:versionCode="${bindings.versioncode}"
+    android:versionName="${bindings.version}" >
+
+    <uses-sdk
+        android:minSdkVersion="12" />
+
+    <application>
+       <meta-data android:name="org.sigrok.androidutils.jni_libs_resource_id" android:resource="@array/sigrok_androidutils_jni_libs"/>
+    </application>
+
+</manifest>
index 4df180cdd5ef34d4dc2fb78ec0047c1f0fd76008..1926acfcd0b89a1914e8cf3b3e7c682b061dfdb8 100644 (file)
@@ -38,11 +38,20 @@ dist_pkgdata_DATA = device_filter.xml
 jardir = $(prefix)/jar
 jar_DATA = $(UTILS_JAR) $(ANTTASKS_JAR)
 
+mvndir = $(prefix)/.m2/repository/org/sigrok/sigrok-core-android/$(BINDINGS_VERSION)
+mvn_DATA = $(SIGROK_CORE_AAR) $(SIGROK_CORE_POM)
+
 UTILS_JAR = sigrok-androidutils.jar
 
 ANTTASKS_JAR = ant/sigrok-androidutils-anttasks.jar
 
-ANTFLAGS = -Dandroid.sdk=$(ANDROID_SDK) -Dandroid.platform=$(ANDROID_PLATFORM)
+BINDINGS_VERSION = $(LIBSIGROKCXX_VERSION)
+
+SIGROK_CORE_AAR = sigrok-core-android-$(BINDINGS_VERSION).aar
+SIGROK_CORE_POM = sigrok-core-android-$(BINDINGS_VERSION).pom
+
+ANTFLAGS = -Dandroid.sdk=$(ANDROID_SDK) -Dandroid.platform=$(ANDROID_PLATFORM) \
+       -Dprefix=$(prefix) -Dbindings.version=$(BINDINGS_VERSION)
 
 EXTRA_DIST = build.xml src ant/src ant/resources
 
@@ -52,9 +61,16 @@ $(UTILS_JAR): always
 $(ANTTASKS_JAR): always
        $(AM_V_GEN)ant -S -q anttasks
 
+$(SIGROK_CORE_AAR): always $(UTILS_JAR) $(ANTTASKS_JAR) $(lib_LTLIBRARIES)
+       $(AM_V_GEN)ant -S -q $(ANTFLAGS) aar
+
+$(SIGROK_CORE_POM): always
+       $(AM_V_GEN)ant -S -q $(ANTFLAGS) pom
+
 clean-local:
-       -rm -rf build ant/build
+       -rm -rf build ant/build jni
        -rm -f $(UTILS_JAR) $(ANTTASKS_JAR)
+       -rm -f $(SIGROK_CORE_AAR) $(SIGROK_CORE_POM) classes.jar
 
 .PHONY: always
 
index d9b8cef5524a0333b5f47471a4c4359d10fed08b..38e5e0d97f7b3cfeefbf4d3791e12a73829d1f13 100644 (file)
@@ -35,6 +35,7 @@ import java.util.Queue;
 import java.util.TreeSet;
 import java.util.Vector;
 import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.DynamicAttribute;
 import org.apache.tools.ant.Task;
 import org.apache.tools.ant.types.FileSet;
 import org.apache.tools.ant.types.PatternSet;
@@ -43,7 +44,7 @@ import org.apache.tools.ant.types.ResourceCollection;
 import org.apache.tools.ant.types.resources.FileProvider;
 import org.apache.tools.ant.types.selectors.SelectorUtils;
 
-public class CopyLibsTask extends Task
+public class CopyLibsTask extends Task implements DynamicAttribute
 {
        private static final HashMap<String,String> blacklist;
 
@@ -208,7 +209,7 @@ public class CopyLibsTask extends Task
 
                protected File getDestName(File dest)
                {
-                       File d = new File(dest, subdir);
+                       File d = (subdir == null? dest : new File(dest, subdir));
                        File f = new File(d, destname);
                        return f;
                }
@@ -476,12 +477,21 @@ public class CopyLibsTask extends Task
        protected Vector<ResourceCollection> rcs = new Vector<ResourceCollection>();
        protected PatternSet patterns = new PatternSet();
        protected String property = null;
+       protected Vector<String> rpath = new Vector<String>();
 
        public void setTodir(File destDir)
        {
                this.destDir = destDir;
        }
 
+       public void setDynamicAttribute(String name, String value)
+       {
+               if ("rpath-link".equals(name))
+                       this.rpath.add(value);
+               else
+                       throw new BuildException("copylibs doesn't support the \"" + name + "\" attribute");
+       }
+
        public void addFileset(FileSet set)
        {
                add(set);
@@ -528,8 +538,10 @@ public class CopyLibsTask extends Task
                                }
                                Integer m = new Integer(l.elf.header.e_machine);
                                Worker w = workers.get(m);
-                               if (w == null)
+                               if (w == null) {
                                        workers.put(m, (w = new Worker(m.intValue())));
+                                       w.addRpath(rpath);
+                               }
                                w.addWork(l);
                        }
                }
index b76ef35310ef14cb678c52cb01cb1f4efe15b6f3..04496cd7361c19a9594aea37479b6b3e61f09d05 100644 (file)
--- a/build.xml
+++ b/build.xml
 
 <project>
 
+  <property name="android.abi" value="armeabi"/>
+
   <target name="utils" >
     <mkdir dir="build"/>
     <javac srcdir="src" destdir="build" includeantruntime="no"
-       classpath="${android.sdk}/platforms/${android.platform}/android.jar"/>
+      classpath="${android.sdk}/platforms/${android.platform}/android.jar"/>
     <jar destfile="sigrok-androidutils.jar">
       <fileset dir="build" />
     </jar>
     </jar>
   </target>
 
+  <target name="-declare-anttasks">
+    <taskdef resource="anttasks.properties"
+      classpath="ant/sigrok-androidutils-anttasks.jar"/>
+  </target>
+
+  <target name="aar" depends="-declare-anttasks">
+    <copylibs todir="jni/${android.abi}" property="bundled_libs" rpath-link="${prefix}/lib">
+      <fileset file="${prefix}/lib/jni/libsigrok_java_core_classes.so" />
+      <exclude name="libc.so"/>
+      <exclude name="libm.so"/>
+      <exclude name="libdl.so"/>
+      <exclude name="liblog.so"/>
+      <exclude name="libstdc++.so"/>
+      <exclude name="libz.so"/>
+    </copylibs>
+    <copy file=".libs/libsigrokandroidutils.so" todir="jni/${android.abi}" />
+    <mkdir dir="aidl" />
+    <loadresource property="bindings.versioncode">
+      <propertyresource name="bindings.version" />
+      <filterchain>
+        <tokenfilter>
+          <filetokenizer />
+          <replaceregex pattern="[0-9]+" replace="0000\0" flags="g" />
+          <replaceregex pattern="0*([0-9]{3})\.?" replace="\1" flags="g" />
+          <replaceregex pattern="0*([1-9][0-9]*|0)([^0-9].*)?$" replace="\1" />
+        </tokenfilter>
+      </filterchain>
+    </loadresource>
+    <jar destfile="classes.jar">
+      <zipfileset includes="**/*.class" src="sigrok-androidutils.jar" />
+      <zipfileset includes="**/*.class" src="${prefix}/share/java/sigrok-core.jar" />
+    </jar>
+    <zip destfile="sigrok-core-android-${bindings.version}.aar">
+      <zipfileset dir="aidl" prefix="aidl" />
+      <zipfileset dir="jni" prefix="jni" />
+      <zipfileset dir="." includes="classes.jar" />
+      <mappedresources>
+        <concat>
+          <fileset file="AndroidManifest.xml.in" />
+          <filterchain>
+            <expandproperties />
+          </filterchain>
+        </concat>
+        <mergemapper to="AndroidManifest.xml" />
+      </mappedresources>
+      <mappedresources>
+        <concat><![CDATA[<?xml version='1.0' encoding='utf-8'?>
+<resources>
+       <array name="sigrok_androidutils_jni_libs">
+               ${bundled_libs}
+       </array>
+</resources>]]></concat>
+        <mergemapper to="res/values/sigrok_androidutils_jni_libs.xml" />
+      </mappedresources>
+    </zip>
+  </target>
+
+  <target name="pom" >
+    <echo file="sigrok-core-android-${bindings.version}.pom"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.sigrok</groupId>
+  <artifactId>sigrok-core-android</artifactId>
+  <version>${bindings.version}</version>
+  <packaging>aar</packaging>
+</project>
+]]></echo>
+  </target>
+
 </project>
index b95296e271069a623777e35d47414749520617a7..7d1bce8578f8a310c4525b4cc3e8b2b99e7225fc 100644 (file)
@@ -42,7 +42,12 @@ LT_INIT
 AC_PROG_CXX
 AM_PROG_CC_C_O
 
+PKG_CHECK_MODULES([LIBSIGROKCXX], [libsigrokcxx], [
+       LIBSIGROKCXX_VERSION=`$PKG_CONFIG --modversion "libsigrokcxx" 2>&AS_MESSAGE_LOG_FD`
+])
+
 AC_SUBST([ANDROID_SDK])
 AC_SUBST([ANDROID_PLATFORM])
+AC_SUBST([LIBSIGROKCXX_VERSION])
 
 AC_OUTPUT([Makefile lib/libsigrokandroidutils.pc])
diff --git a/src/org/sigrok/androidutils/SigrokApplication.java b/src/org/sigrok/androidutils/SigrokApplication.java
new file mode 100644 (file)
index 0000000..5a68792
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file is part of the sigrok-androidutils project.
+ *
+ * Copyright (C) 2016 Marcus Comstedt <marcus@mc.pp.se>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.sigrok.androidutils;
+
+import android.app.Application;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.util.Log;
+
+public class SigrokApplication extends Application {
+
+       private static final String JNI_LIBS_RESOURCE_ID_META =
+           "org.sigrok.androidutils.jni_libs_resource_id";
+
+       public SigrokApplication()
+       {
+       }
+
+       public static void initSigrok(Context context)
+       {
+               ApplicationInfo appInfo = context.getApplicationInfo();
+               Environment.initEnvironment(appInfo.sourceDir);
+               UsbHelper.setContext(context);
+               try {
+                       appInfo = context.getPackageManager().
+                               getApplicationInfo(context.getPackageName(),
+                               PackageManager.GET_META_DATA);
+               } catch (PackageManager.NameNotFoundException exc) {
+               }
+               if (appInfo.metaData != null &&
+                               appInfo.metaData.containsKey(JNI_LIBS_RESOURCE_ID_META)) {
+                       int resId = appInfo.metaData.getInt(JNI_LIBS_RESOURCE_ID_META);
+                       String[] libs = context.getResources().getStringArray(resId);
+                       int numLibs = libs.length;
+                       for (int i = 0; i < numLibs; i++) {
+                               String libName = libs[i];
+                               System.loadLibrary(libName);
+                       }
+               }
+       }
+
+       @Override
+       public void onCreate()
+       {
+               super.onCreate();
+               initSigrok(getApplicationContext());
+       }
+}