How to build a JAR for an android project - android

I am working on a small project to allow cookies to persist. I want to make the project as a JAR so other developers can use it in there project. At the moment I am able to build the project as an APKLIB however this seems unnecessary as the resources are not needed. If I change the packaging to JAR I get the follow error when building
Results :
Tests in error:
shouldAddCookie(com.doward.persistantcookietest.PersistantCookieStoreTest):
java.lang.ClassNotFoundException: caught an exception while obtaining
a class file for com.doward.persistantcookies.R
shouldClearAllCookies(com.doward.persistantcookietest.PersistantCookieStoreTest):
java.lang.ClassNotFoundException: caught an exception while obtaining
a class file for com.doward.persistantcookies.R
shouldClearExpired(com.doward.persistantcookietest.PersistantCookieStoreTest):
java.lang.ClassNotFoundException: caught an exception while obtaining
a class file for com.doward.persistantcookies.R
The POM looks like this
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.doward</groupId>
<artifactId>persistantcookiestore</artifactId>
<version>1</version>
<packaging>jar</packaging>
<name>persistantcookies</name>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.pivotallabs</groupId>
<artifactId>robolectric</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>android</groupId>
<artifactId>android</artifactId>
<version>4.2.2_r2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
<sdk>
<platform>17</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

If you want to just build a JAR file from .class files and no resources e.g. no XML files releating to layouts, values, colours etc then you can do so from the command line:
jar -cvf <filenameorjar.jar> <files to be included>
Example:
jar -cvf cookiepersist.jar com/*
This will create a jar file in the present working directory and include all the .class files from the directory com in the present working directory.

If you have a andorid project and you wish to use the same in another project you can make the project as a library project and refer the same in another Android project. This can include your resources also.
http://developer.android.com/tools/projects/projects-eclipse.html
You export your utility classes that do not refer to resources in android ie pure java classes. If you use eclipse you can have a look at the tutorial below
http://androiddevelopement.blogspot.in/2011/01/creating-jar-file-in-eclipse.html

Related

Intellij Idea stuck on reading pom.xml while importing android project

I have problem with Intellij-Idea. IDE stuck on "Looking for avalible profiles - reading pom.xml" while importing as a maven project, Intelij stuck on reading pom also on orginal project.
I have tried to build project via console mvn clean install and it... works perfectly, building project without errors but in Intellij dependency classes still are not avalible to import and shows up as red in code. I don't if that helps but I'm using mbp with OSx yosemite.
project can be found on github:
wykop offline on github
Restarting Ide/computer does not help
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mierzejewski.wykopoffline</groupId>
<artifactId>wykop-client</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>apk</packaging>
<properties>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<sdk.path>/Users/dom/Desktop/adt-bundle-mac-x86_64-20140624/sdk</sdk.path>
</properties>
<!--<repositories>-->
<!--<repository>-->
<!--<id>jitpack.io</id>-->
<!--<url>https://jitpack.io</url>-->
<!--</repository>-->
<!--</repositories>-->
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mcxiaoke.volley</groupId>
<artifactId>library</artifactId>
<version>1.0.16</version>
</dependency>
<dependency>
<groupId>com.mikepenz.materialdrawer</groupId>
<artifactId>library</artifactId>
<version>3.0.9</version>
<scope>system</scope>
<type>aar</type>
<systemPath>${project.basedir}/libs/materialdrawer-3.0.9.aar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.9.0-rc.1</version>
<configuration>
<sdk>
<path>${sdk.path}</path>
<platform>19</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
you can try to config this into your host file.
127.0.0.1 localhost
This maybe the maven in IDEA force to read localhost first. Then you stuck in reading pom.xml forever.
Is it that you can't go past the import window because clicking the "next" button doesn't produce any result?
if that is the issue, then it is a memory problem:
The default value assigned to Intellij importer is not enough to
handle the operation!
I have documented the solution to this issue in a similar question:
Memory problem as the answer to "Can't import the maven project in IntelliJ Idea"
P.s.: Do not ask me why as i was surprised as well that the selling company could really make such a mistake.

Android ActionBarSherlock Maven - No resources found

I have been trying to add ActionBarSherlock Maven dependecy on my project and i'm getting some problems. When i use ABS themes like:
<style name="AppThemeCustomizada" parent="#style/Sherlock.__Theme.DarkActionBar">
I get the following error:
No resource found that matches the given name '#style/Sherlock.__Theme.DarkActionBar'.
Here is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RunasProject</groupId>
<artifactId>RunasProject</artifactId>
<version>1.0.1-SNAPSHOT</version>
<name>Runas</name>
<dependencies>
<dependency>
<groupId>com.actionbarsherlock</groupId>
<artifactId>actionbarsherlock</artifactId>
<version>4.4.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.actionbarsherlock</groupId>
<artifactId>actionbarsherlock</artifactId>
<version>4.4.0</version>
<type>apklib</type>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<extensions>true</extensions>
<configuration>
<source>1.6</source>
<target>1.6</target>
<sdk>
<platform>8</platform>
</sdk>
</configuration>
</plugin>
</plugins>
</build>
I have tried many suggestion on net, but i could not solve the problem. ABS classes work fine, but styles don't.
You have to change the packaging of your project to apk and use the Android Maven Plugin. Your configuration is mixing setup from the Android Maven Plugin into the Compiler plugin. That wont work. Check out the sample applications (including an ABS demo) of the Android Maven Plugin.

Maven + RoboGuice + ActionBarSherlock + RoboGuice-Sherlock

I am trying to create an Android base project using android-quickstart archetype, and adding RoboGuice, ActionBarSherlock dependencies, plus RoboGuice-Sherlock to combine the two.
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>BaseApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>apk</packaging>
<name>BaseApp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<platform.version> 4.1.1.4
</platform.version>
<android.plugin.version>3.6.0</android.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${platform.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.roboguice</groupId>
<artifactId>roboguice</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>1.3.9</version>
</dependency>
<dependency>
<groupId>com.actionbarsherlock</groupId>
<artifactId>actionbarsherlock</artifactId>
<version>4.4.0</version>
<type>apklib</type>
</dependency>
<dependency>
<groupId>com.github.rtyley</groupId>
<artifactId>roboguice-sherlock</artifactId>
<version>1.5</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>${android.plugin.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<sdk>
<platform>16</platform>
</sdk>
</configuration>
</plugin>
</plugins>
</build>
</project>
But m2eclipse wont work with this Pom.xml, as it is unable to find the apklib dependency.
Speficically I am getting the following message under "Problems" tab in Eclipse:
dependency=[com.actionbarsherlock:actionbarsherlock:apklib:4.4.0:compile] not found in workspace pom.xml /BaseApp line 1 me.gladwell.eclipse.m2e.android.markers.dependency.apklib
I have tried removing the <type>apklib</type> line but then my ABS resources (namely the required Themes) are not imported into the project.
I've read in several questions that the apklib type only works on command line, and that if I want to stick to the IDE I need to import ABS manually as a library. I did, but then RoboGuice crashes because the project becomes a library project and, apparently, resource ids are not final anymore in library projects. (The compile-time error I get is "The value for annotation attribute InjectView.value must be a constant expression"). For reference, this is my only Activity:
public class HelloAndroidActivity extends RoboSherlockActivity {
#InjectView(R.id.helloworld) TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView.setText("Roboguice says Hello World");
}
}
IS there any proper way to create a base project with these three elements using maven and Eclipse?
Turns out I accidentally clicked the "Is Library" checkbox on Project Properties. Now it works with ABS manually imported as a library.
It is a pity though not being able to use Maven directly :(

R.java will not be generated (android-maven-plugin)

I tried different goals. mvn package results in package R does not exist, mvn android:generate-sources results in BUILD SUCCESS but no R.java will be created.
I would like to pack my classes into a jar container (not apk) because I would like to use it as a library in other projects.
AndroidManifest.xml: (minimal setup, because there are no Activities)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
</manifest>
default.properties:
android.library=true
target=android-8
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>mylib</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.4.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
The only resource is ./res/layout/example.xml.
android-maven-plugin has no capability to generate a fully functional jar (contains all compiled class file, resources, manifest and etc) from Android library project.
The solution given by android-maven-plugin is apklib, which is simply a zip of the library project (has same directory structure contains raw java file, resource, manifest and etc). I have explained a bit more about how apklib works in this answer, for some sample projects, check out maven-android-plugin-samples/libraryprojects.
The furthest android-maven-plugin can go is a jar that contains only compiled class file, no resource, this is covered by the default <packaging>apklib<packaging> build life cycle and generated along with the .apklib file.

Maven does not fail from introduced compilation errors

Just moving my Android project over to Maven / Jenkins and am exploring the build / compile / testing procedures.
Issue:
I introduced a simple compilation error in the java code, but when i run mvn clean install package I get a build success. It's only when I deploy the App to my device that it crashes and fails.
Is there something wrong with my POM?
ps. I have XXXXX'd out some info. Don't worry about it.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xxxxx</groupId>
<artifactId>xxxxxx</artifactId>
<version>1.0.0</version>
</parent>
<groupId>xxxxx</groupId>
<artifactId>xxxx</artifactId>
<version>1.0.0</version>
<packaging>apk</packaging>
<name>Android App</name>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.0.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.actionbarsherlock</groupId>
<artifactId>library</artifactId>
<version>4.0.0</version>
<type>apklib</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
<assetsDirectory>${project.basedir}/assets</assetsDirectory>
<resourceDirectory>${project.basedir}/res</resourceDirectory>
<nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
<sdk>
<platform>15</platform>
<path>/Users/aidenfry/android-sdks</path>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I suspect, that the reason you're not seeing an error is most likely because maven didn't even bother compiling your classes in the first place.
Maven has various conventions, and one of them is that it expects your sourcecode to be located under src/main/java.
I'm betting that you've just dropped the above pom.xml into your project root area and run mvn clean install. Maven will happily run that, but since you've probably left your source files in the default IDE locations, maven just skips over it. Have a look in your target directory and you'll see it hasn't compiled any classes in there for you
You have 2 options:
Add this somewhere inside the <build> tags in the above pom :
${basedir}/src
Re-arrange your source files so they follow the src/main/java structure.
Personally, I'd opt for number 2.
P.S, there is also src/main/test too, test classes should go there :)
You can read more about the configuration of the pom file here

Categories

Resources