简体   繁体   English

从我的图书馆隐藏课程(aar)

[英]Hide classes from my library (aar)

im trying to create my little library. 我试图创建我的小图书馆。 But i have little problem. 但我没有什么问题。 I cant right understand how to hide some classes/methods from public usage. 我无法理解如何隐藏一些类/方法以免公开使用。 Say i have structure : 说我有结构:

-myPackage.com
  -classA  (public class example : public classA {...})
  -classB  (just a class example : class classB {...}

in this way when i add my aar file to android project. 这样当我将我的aar文件添加到android项目时。 I can use from library only classA. 我只能从库中使用classA。 classB hided(invisible for developer) and i cant call him (its ok). classB隐藏(对于开发者来说是隐形的)而我无法打电话给他(好吧)。 But classA can use functions from classB . 但是classA可以使用classB中的函数。 Because its in same package. 因为它在同一个包装中。 And its ok. 好吧。 So how correctly create classes in another packages ? 那么如何在另一个包中正确创建类?

-myPackage.com
 -myHelpPackage
  -classC (public class example: public class classC {...}
  -classD (just a class example: class classD {...}
 -classA (public class example : public classA {...})
 -classB (just a class example : class classB {...}

in this way i have two public classes which developer can call 通过这种方式,我有两个开发人员可以调用的公共类

classA
classC

classD visible only for classA. classD仅对classA可见。 So i cant call classD(functions) from classA. 所以我不能从classA调用classD(函数)。 Main question : how to achive it ? 主要问题:如何实现它? I want to have only one classA to call. 我想只有一个classA来打电话。 classC is ok. classC没问题。 classD and classB should be invisible for developer. classD和classB应该对开发人员不可见。 But visible only for classA. 但仅适用于classA。 Anyone can help me with this ? 有人可以帮我这个吗?

If you don't specify any access level modifier, eg: 如果您未指定任何访问级别修饰符,例如:

package myPackage.com

class classA {
}

then the class will be package-private by default. 那么这个类默认是package-private This means it is only visible to classes inside your package. 这意味着它只对包内的类可见。 See also the documentation by Oracle here 另见甲骨文的文档在这里

This question has been answered in the comments by JakobJeremais , but in case it's not clear to anyone, or they skip over it because they only read top-level answers (understandable). JakobJeremais的评论已经回答了这个问题,但是如果对任何人都不清楚,或者他们跳过它,因为他们只阅读顶级答案(可以理解)。 If you have a package-private class in your aar, it will still be publicly accessible, so I would recommend nesting the class in whatever calls it, an you will be safe. 如果您的aar中有一个包私有类,它仍然可以公开访问,所以我建议将该类嵌套在任何调用它,您将是安全的。

For instance, I had to extend EditText so that I could override just one method, and nothing else. 例如,我不得不扩展EditText,以便我可以只覆盖一个方法,而不是其他方法。 Then I had to reference that from only one other class, but didn't want it accessible from whatever projects I added my library to. 然后我不得不从其他一个类引用它,但不希望它从我添加我的库的任何项目中访问。 So I nested it, and now it's fine. 所以我把它嵌套了,现在没关系。

public class VisibleClass extends View {
    public static HiddenClass extends EditTextCompat { 
        @Override
        public void annoyingFunction() {
            //Disable this function
        }
    }
}

I should note, though: The compiler isn't happy about accessing it from layout, so I suppose it hasn't really helped, so I wouldn't recommend it, for now. 我应该注意到:编译器不喜欢从布局中访问它,所以我认为它没有真正帮助,所以我暂时不推荐它。 If you have time to tinker, go ahead, though. 如果你有时间修补,请继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM