Get version information of an included library? - android

I have created 2 project 1 is Lib and 2nd in Test.
From test app I pass context to Lib project.
As I have context in Lib, i used below code inside Lib
String version = this.mContext.getPackageManager().getPackageInfo(this.mContext.getPackageName(), 0).versionName;
Log.e("versionName", version);
But this gives me version name of test app and not lib.
How should i get application version name and code for Lib?
Reference link:
How to get version information of an included library?

BuildConfig.VERSION_NAME
Just make sure that you import the BuildConfig for your library (as literally every android library/app has it's own BuildConfig on it's package path). The Android Studio's Gradle Build system indicates that this is always generated as of version 0.7.0 (see changelog).

Related

How do I access an android library from my apk

I've created an android library that lacks any UI. I made it as an android library instead of a regular java library in Android Studio because it needs access to a context. What this means it that if an android application needs to use this library, it must pass in it's context. I could just do this in my android app, but if I separate it out the code will be reusable.
I had this library as a standalone android project. How do I access this library from my android app? If I import it into my app as a module gradle complains:
Warning:Dependency MyAndroidApp:MyLibrary:unspecified on project app resolves to an APK archive which is not supported as a compilation dependency. File: /MyLibrary/build/outputs/apk/mylibrary-release-unsigned.apk

Customizable package name gradle library project

I am currently using a gradle script to compile and generate a .jar library, that is eventually targeted for an android application. I have different customers that would like a specific package name, so I can't give them the library with the package name I am using.
Is there a way a can make this configurable using gradle (preferably)? I basically want to have a way I can specify the package name while (or prior to) building, resulting in a library using the new package name.
I looked online but all I could find was ways I could change package name for an app using gradle or ant. But my project is a library project.

Resources in Android Library

I have problems with accessing resources in my android library. I have created library project with some resources (com.library) and then I imported module into application project (com.app). So I have Android Studio project with library and application.
When I want to access some library resource (com.library.R.string.label) I get error during compilation
package com.library.R does not exist
When I want to run some library method from application which contains/uses R.string.label I get
java.lang.NoClassDefFoundError: com.library.R$string
I added library to application gradle file using
compile project(':Library')
and from IDE perspective looks everything fine and R.java is created with references to resources. I want same usage as I'm using for example android.R.string.cancel or similar libraries in my application project.Where I'm doing mistake? Thank you for help.
PS: In the future I want to have my library project as aar package.
As soon as you add a library to your project all resources will be "copied" to the R-file of your app. So if you want to access a string from your library you don't do something like getString(com.library.R.string.some_string) you simply call getString(R.string.some_string) instead.
In terms of the library method: Would you mind sharing some code with us? Currently I can't imagine what's going wrong.
Problem was in wrong package name in gradle configuration files (after renaming).

Specifying Android library version code/name

Which is the right place to specify android's library project version code/name? Whats the current state on a library's Android Manifest? When you bundle a library jar, can AndroidManifest be ignored while bundling jar and still delivering a usable/unbroken jar?
My question is related with this
You set version code/ name of a project in its AndroidManifest.xml. But if it's a library project, all information in the manifest will be ignored after integrating into the host project.
There is an issue.
BuildConfig.VERSION_NAME
BuildConfig.VERSION_CODE
Just make sure that you import the BuildConfig for your library (as literally every android library/app has it's own BuildConfig on it's package path). The Android Studio's Gradle Build system indicates that this is always generated as of version 0.7.0 (see changelog). This persists as a generated class into the hosting app/library.

Android NoSuchMethodError with APK file (Apache HTTPUtils core)

I use the Apache HTTPUtils core library 4.1.4 in my project to use the
EntityUtils.consume(HTTPEntity);
method.
However, when this method is used in the app in the emulator (SDK v.2.1), it crashes with a NoSuchMethodError.
I guess this is because the app tries to use the Android library which is old and doesn't have this method instead of the newer one I've put in my build path.
How can I use force the app to use the newer library?
Worth to check out Using newer version of HttpClient on Android (like 4.1.x).
If you are right with your guess and you are using Eclipse you can change the order in "Order and Export" tab in Java Build Path options. If you have duplicate classes than you can define the class path order there

Categories

Resources