How to use resource in Android library dependency on ADT? - android

My development environment:
adt:22.3.0
android-maven-plugin:3.8.2
maven:3.1.1
I am developing a google map Android app with maven structure and dependencies as below:
<dependency>
<groupId>com.google.android.gms</groupId>
<artifactId>google-play-services</artifactId>
<version>13.0.0</version>
<type>apklib</type>
</dependency>
<dependency>
<groupId>com.google.android.gms</groupId>
<artifactId>google-play-services</artifactId>
<version>13.0.0</version>
</dependency>
or
<dependency>
<groupId>com.google.android.gms</groupId>
<artifactId>play-services</artifactId>
<version>4.0.30</version>
<type>aar</type>
</dependency>
I tried apk and aar type but I still can't use resource drawable, string etc (R.drawable.poi, R.style.button_style1..) in android lib dependency. How can I handle this?

To use the resource drawables of Library, you also have to import the project into eclipse workspace and reference it from your mavenized project.
I faced this problem, solved it by importing Google-play-services project to workspace and referencing it from my maven project.
For referencing. right click on your project->Properties->Android->(libraryProjects->Add->[browse google-play-library project]), click on apply and save it.
you can find google-play-library project in sdk/extras/google/

A similar problem appeared in our project after updating android-maven-plugin from version 3.8.0 to 3.8.2, android-maven-plugin looks bugy.
We had to fallback to version 3.8.0. I recommend you to do the same using apklib as aar is only available since version 3.8.2 and in case you want to use aarto wait for later version of android-maven-plugin.
In details, the problem was in the linking of the apklib, all ressources (layouts, drawables...) referenced seemed to be disaligned. For example a layout or a drawable was displayed instead of another. Application was crashing when accessing to a missing reference/id in a layout (the reference was not present because the wrong layout was loaded).
See issue http://code.google.com/p/maven-android-plugin/issues/detail?id=441

Related

How to include App Compatibility v7 In Maven Build

I want to use android.support.v7.widget.SwitchCompat
This is more of a problem than I ever imagined. I need to know the exact steps to include v7 compatibility library in maven build.
So far I either run into class not found error on android.support.v7.widget.SwitchCompat or when I include that jar,or I get exception related to not finding R.attr at runtime when it compiles ok.
So I need to know exactly what needs to be done to compile v7 so that everything I need is in the APK.
I don't see that classes.jar inside apklib even has the R.attr etc.
Could someone just tell me exactly what I need to do include v7 compatibility in my maven build.
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v7</artifactId>
<version>23.0.1</version>
<type>apklib</type>
<scope>system</scope>
<systemPath>${basedir}/libs/appcompat-v7-23.0.1.jar</systemPath>
</dependency>
<dependency>
<groupId>com.android.support</groupId>
<artifactId>appcompat-v7</artifactId>
<version>23.0.1</version>
<type>jar</type>
<scope>system</scope>
<systemPath>${basedir}/libs/android-support-v7-appcompat.jar</systemPath>
</dependency>
I have seen it said before to use compile scope, but this is not available if the libs are on system. It must use system scope.
Now in the past I always use eclipse, and when something was missing at runtime it was always just a matter of 'exporting' certain jars in ide, so now with pure maven pom build, I don't know what to do. It needs to be built on server where eclipse is not part of the build process. What I am not getting is what is telling the build to include the classes like R.attr at runtime related to this UI library? I need to resources at runtime. In the past, I always just depended on eclipse exporting of jars for this and link to the library. Now it is not so clear ...
Can someone tell me how to tell maven to check my local repo for items? Right now if it does not find an item on server it ends in error even if I installed items locally in on files system \username.m2

Android project using Maven with AAR for not having Eclipse errors

I have an android project that uses several libraries:
play-services for analytics,
appcompat-v7 for the actionar,
and others... (for ads, in-app...)
I used to use APKLIB packaging and I was able to generate the APK of my project, but having hundreds of errors in Eclipse. So my intention is to fix this for not having any error at all }:-)
I discovered the AAR format and all this gradle staff, but I'd like to keep using my maven structure as it is.
So I got the AAR package of play_services (thanks to https://github.com/mosabua/maven-android-sdk-deployer) and generated the AAR package of appcompat-v7 and the others libs.
And I almost made this happen, excepting some errors with styles and other missing resources from those libs that are still not found:
Error: No resource found that matches the given name (at 'value' with value '#integer/google_play_services_version').
Error: No resource found that matches the given name (at 'theme' with value '#style/Theme.AppCompat.Light').
This is an example of some of my dependencies:
<dependency>
<groupId>com.google.android.gms</groupId>
<artifactId>play-services</artifactId>
<version>5.0.77</version>
<type>aar</type>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v7-appcompat</artifactId>
<version>20.0.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v7-appcompat</artifactId>
<version>20.0.0</version>
<type>aar</type>
</dependency>
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v4</artifactId>
<version>20.0.0</version>
</dependency>
My questions are:
Does it worth to forget Maven and move to Gradle for using Android apps? (Please say no :)
Is it not possible to get the dependencies resources using AAR packaging? I don't want to import all my projects in Eclipse for having access to their resources.
Move to Gradle, it might pay off the effort you have to do to fix it. These new google libraries are more and more made for Gradle. Android-Maven-Plugin isn't mature enough to work well with AAR files the way APKLIB used to work, so it might require a lot of little adaptations. So if that is the case, I would recommend to spend this precious time with Gradle instead.
Note: by "moving to Gradle" it doesn't necessarily mean that you have to switch to Android Studio. You can still use this great IDE that Eclipse is ;-)

Does Android Maven apklib include its own SDK in the packaging?

When I configured my apklib I included:
<dependency>
<groupId>android</groupId>
<artifactId>android</artifactId>
<version>4.0_r3</version>
<scope>provided</scope>
</dependency>
in the dependencies list. If I don't do that it won't recognize the source code in the editor
and won't compile. However, my question is, does the Android SDK then get included into the
project that uses the apklib? It seems so. However, why would it be so? I thought which SDK to use should be only decided by the project that uses the apklib rather then the apklib itself(?). I don't want to use an apklib that for example use api-level 19 when I use api-level 14 in my main project.
In short: Does an SDK dependency in the apklib pom get included into the main project (using Maven)?
I got it.
One should use scope=provided for the dependency.
Then, Maven assumes that the container project supplies the classes, and so the SDK classes don't get imported together with the apklib.

How do I add a library (android-support-v7-appcompat) in IntelliJ IDEA

I created a project, copied the resource files in the project, library, added it to the project structure, prescribed style Theme.AppCompat.
Compiled without errors, but when you start the relegation Exception:
08-03 00:50:00.406: ERROR/AndroidRuntime(4055): FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:98)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at com.example.SampleMetrRost.CentralActivity.onCreate(CentralActivity.java:12)
at android.app.Activity.performCreate(Activity.java:4636)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
Help me, please. Spent all day today.
Using Gradle
If you are using Gradle, you can add it as a compile dependency.
Instructions
Make sure you have the Android Support Repository SDK package installed. Android Studio automatically recognizes this repository during the build process (not sure about plain IntelliJ).
Add the dependency to {project}/build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:+'
}
Click the Sync Project with Gradle Files button.
EDIT: Looks like these same instructions are on the documentation under Adding libraries with resources -> Using Android Studio.
Without Gradle (Click here for the Gradle solution)
Create a support library project.
Import your library project to Intellij from Eclipse project (this step only applies if you created your library in Eclipse).
Right click on module and choose Open Module Settings.
Setup libraries of v7 jar file
Setup library module of v7
Setup app module dependency of v7 library module
Using Maven
First of all you should install android libraries to your local maven repository using Maven Android SDK Deployer
Then you can add dependency to your pom like this:
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v7-appcompat</artifactId>
<version>${compatibility.version}</version>
<type>apklib</type>
</dependency>
<dependency>
<groupId>android.support</groupId>
<artifactId>compatibility-v7-appcompat</artifactId>
<version>${compatibility.version}</version>
<type>jar</type>
</dependency>
This is my solution:
Copy&paste $ANDROID_SDK/extras/android/support/v7/appcompat to your project ROOT
Open "Project Structure" on Intellij, click "Modules" on "Project Settings", then click "appcompat"->"android', make sure "Library Module" checkbox is checked.
click "YOUR-PROJECT_NAME" under "appcompat", remove "android-support-v4" and "android-support-v7-compat"; ensure the checkbox before "appcompat" is checked. And, click "ok" to close "Project Structure" dialogue.
back to the mainwindow, click "appcompat"->"libs" on the top-left project area. Right-click on "android-support-v4", select menuitem "Add as library", change "Add to Module" to "Your-project". Same with "android-support-v7-compat".
After doing above, intellij should be able to correctly find the android-support-XXXX modules.
Good Luck!
Another yet simple solution is to paste these line into the build.gradle file
dependencies {
//import of gridlayout
compile 'com.android.support:gridlayout-v7:19.0.0'
compile 'com.android.support:appcompat-v7:+'
}
Another solution for maven (and a better solution, for me at least) is to use the maven repository included in the local android SDK. To do this, just add a new repository into your pom pointing at the local android SDK:
<repository>
<id>android-support</id>
<url>file://${env.ANDROID_HOME}/extras/android/m2repository</url>
</repository>
After adding this repository you can add the standard Google dependency like this:
<dependency>
<groupId>com.android.support</groupId>
<artifactId>support-v13</artifactId>
<version>${support-v13.version}</version>
</dependency>
<dependency>
<groupId>com.android.support</groupId>
<artifactId>appcompat-v7</artifactId>
<version>${appcompat-v7.version}</version>
<type>aar</type>
</dependency>
As a Library Project
You should add the resources in a library project as per http://developer.android.com/tools/support-library/setup.html
Section > Adding libraries with resources
You then add the android-support-v7-appcompat library in your workspace and then add it as a reference to your app project.
Defining all the resources in your app project will also work (but there are a lot of definitions to add and you have missed some of them), and it is not the recommended approach.
This is my solution, it is very similar to the previous one:
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v7</artifactId>
<scope>system</scope>
<systemPath>${android.home}/support/v7/appcompat/libs/android-support-v7-appcompat.jar</systemPath>
<version>19.0.1</version>
</dependency>
Where {android.home} is the root directory of the Android SDK and it uses systemPath instead of repository.
As an update to Austyn Mahoney's answer, configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed at the end of 2018. For more information see here.

Android Eclipse project compilation errors with maven provided dependency

I'm converting an android application to be maven-based
what I've done is:
Insert pom.xml
List item
copy java classes into main/java directory and make this directory
the source path
insert the maven nature into .project file
I add the google android library dependency into pom.xml:
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.1.2</version>
<scope>provided</scope>
</dependency>
I then realised that I don't need the android library to be included in the class path as I'm actually refering it from the pom.xml
So I removed the library, and what happend is the the project has compilation errors, though It exports the apk successfully when I right click > Run As> Maven Install, and this means that this project is a correct project from a maven perspective, so what's wrong with eclipse ??
What I found is that the Maven Dependency Library is abscent when I set the library scope as provided,
<scope>provided</scope>
but is back when I set the dependency to it's default(Compile),
<scope>compile</scope>
Though this scope makes maven fail to package as this library is a core library that must not be included, and anyway I find it a terrible solution even if it's packaging well
What am I missing here ??
NB: I'm using MotoDev IDE
It turned out that there is an issue with the android-maven-plugin with apk packaging ... will post the solution when solved

Categories

Resources