public static int[] createBackwardCompatibleStyleable(
      @NonNull int[] localStyleableArray,
      @NonNull Context localContext,
      @NonNull Context remoteContext,
      @NonNull SparseIntArray attributeIdMap) {
    final String remotePackageName = remoteContext.getPackageName();
    if (localContext.getPackageName().equals(remotePackageName)) {
      Logger.d(
          TAG, "This is a local context (" + remotePackageName + "), optimization will be done.");
      // optimization
      for (int attrId : localStyleableArray) {
        attributeIdMap.put(attrId, attrId);
      }
      return localStyleableArray;
    }

    final Resources localRes = localContext.getResources();
    final Resources remoteRes = remoteContext.getResources();
    List<Integer> styleableIdList = new ArrayList<>(localStyleableArray.length);
    for (int attrId : localStyleableArray) {
      final boolean isAndroidAttribute = localRes.getResourcePackageName(attrId).equals("android");
      final int remoteAttrId;

      if (isAndroidAttribute) {
        // android attribute IDs are the same always. So, I can optimize.
        remoteAttrId = attrId;
      } else {
        final String attributeName = localRes.getResourceEntryName(attrId);
        remoteAttrId = remoteRes.getIdentifier(attributeName, "attr", remotePackageName);
        Logger.d(
            TAG, "attr " + attributeName + ", local id " + attrId + ", remote id " + remoteAttrId);
      }
      if (remoteAttrId != 0) {
        attributeIdMap.put(remoteAttrId, attrId);
        styleableIdList.add(remoteAttrId);
      }
    }
    final int[] remoteMappedStyleable = new int[styleableIdList.size()];
    for (int i = 0; i < remoteMappedStyleable.length; i++) {
      remoteMappedStyleable[i] = styleableIdList.get(i);
    }

    return remoteMappedStyleable;
  }
@Test
  public void testDifferentPackageNoValue() {
    // this is a long setup
    Context remoteContext = Mockito.mock(Context.class);
    Mockito.doReturn("com.some.other.package").when(remoteContext).getPackageName();
    Resources remoteRes = Mockito.mock(Resources.class);
    Mockito.doReturn(0)
        .when(remoteRes)
        .getIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
    Mockito.doReturn(remoteRes).when(remoteContext).getResources();

    // starting test
    SparseIntArray sparseIntArray = new SparseIntArray();
    int[] backwardCompatibleStyleable =
        Support.createBackwardCompatibleStyleable(
            R.styleable.KeyboardLayout, getApplicationContext(), remoteContext, sparseIntArray);

    Mockito.verify(remoteRes).getIdentifier("showPreview", "attr", "com.some.other.package");
    Mockito.verify(remoteRes).getIdentifier("autoCap", "attr", "com.some.other.package");
    Mockito.verifyNoMoreInteractions(remoteRes);

    Assert.assertNotSame(backwardCompatibleStyleable, R.styleable.KeyboardLayout);
    Assert.assertEquals(backwardCompatibleStyleable.length, R.styleable.KeyboardLayout.length - 2);
    Assert.assertEquals(backwardCompatibleStyleable.length, sparseIntArray.size());
    for (int attrId : backwardCompatibleStyleable) {
      Assert.assertEquals(attrId, sparseIntArray.get(attrId));
    }
  }