5

I'm creating an aar library and i need to get aar packagename for having sign of aar file.

i'm trying below code, but getPackageName() method didn't return aar packagename

Signature[] sigs = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures;
for (Signature sig : sigs){
    Log.i("MyApp", "Signature hashcode : " + sig.hashCode());
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Milad Bahmanabadi
  • 946
  • 11
  • 27
  • 1
    if you've created the `aar` library by yourself, then you should know the package name already. Please explain why should you need to read it programmatically? – sourav.bh Jan 01 '19 at 09:09
  • @sourav.bh for getting sign of aar – Milad Bahmanabadi Jan 01 '19 at 11:33
  • 1
    Hi @Milad I was able to get package name of AAR by using `BuildConfig.LIBRARY_PACKAGE_NAME` but getPackageInfo() throws a NameNotFoundException. I am trying to do the same thing that is to verify the signature of my AAR at runtime. Were you able to find a solution to this? Please share if you did. – camelCaseCoder Dec 20 '19 at 09:50

2 Answers2

2

The AAR library is only read during compilation time. When you build an App that uses that library the AAR is "included" and merged with the App code, so the PackageName in runtime is the one of the App and not the one of your library. Signin an AAR file is used to make developers sure about the source of that file, and nothing else. In runtime that signature is lost after the build procedure.

emandt
  • 2,547
  • 2
  • 16
  • 20
1

You can simply get the package name by accessing a class from the .aar library. Suppose you have a class in the library named Sample.java, then you can get the package name like this:

String packageName = Sample.class.getPackage().getName();
sourav.bh
  • 467
  • 1
  • 7
  • 20