Reputation: 49
I want to build android library so that all its classes except of one will be not accessible for an application that will use this library. How I can do it?
Upvotes: 3
Views: 2644
Reputation: 13928
If your SDK is bundled as JAR file (instead of AAR), you can mimic internal APIs hiding in the same way as it is done in Android SDK by creating two JAR files (one will be regular library, and second stripped - which will represent the API). Let's see an example.
Full library (I'll call it sdk-runtime.jar) will consist of a single class: Api.java
:
package info.osom.sdk;
public class Api {
public static void init() {
System.out.println("Api.init()");
anotherPublicMethod();
}
public static void anotherPublicMethod() {
System.out.println("Api.anotherPublicMethod()");
}
}
Stripped library (I'll call it sdk-api.jar) will hide the anotherPublicMethod()
, and expose only the init()
method:
package info.osom.sdk;
public class Api {
public static void init() {
throw new RuntimeException("stub!");
}
}
Instruct your SDK consumers to configure their application dependencies as following:
dependencies {
provided files('libs/sdk-api.jar')
apk files('libs/sdk-runtime.jar')
}
According to documentation:
provided
configuration adds the dependency to the compilation classpath only (it is not added to the APK).
Note: If you're creating an Android app module, you cannot useprovided
for AAR dependencies, only for JARs. In an Android library module, you can use it for both JARs and AARs.
apk
configuration adds the dependency to the APK only (it is not added to the compilation classpath).
Note: You can use apk only for JAR binary dependencies. It does not support library modules or AAR binary dependencies.
Since I've stripped the anotherPublicMethod()
from sdk-api.jar
, it won't be available for SDK consumer:
I've created a simple application project that consumes the above SDK.
Upvotes: 1
Reputation: 1006614
Mark them as private
, the same as you would in a traditional Java JAR.
Upvotes: 0