I'm using PermissionDispatcher with kotlin and kapt. I found strange behavior in PermissionDispatcher.
Creating method annotated with @NeedsPermission and add internal modifier, then created class ***PermissionsDispatcher looks
final class MainActivityPermissionsDispatcher {
private static final int REQUEST_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN = 0;
private static final String[] PERMISSION_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN = new String[] {"android.permission.CAMERA"};
private MainActivityPermissionsDispatcher() {
}
static void showCamera$app_compileDebugKotlinWithCheck(MainActivity target) {
if (PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN)) {
target.showCamera$app_compileDebugKotlin();
} else {
ActivityCompat.requestPermissions(target, PERMISSION_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN, REQUEST_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN);
}
}
static void onRequestPermissionsResult(MainActivity target, int requestCode, int[] grantResults) {
switch (requestCode) {
case REQUEST_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN:
if (PermissionUtils.getTargetSdkVersion(target) < 23 && !PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN)) {
return;
}
if (PermissionUtils.verifyPermissions(grantResults)) {
target.showCamera$app_compileDebugKotlin();
}
break;
default:
break;
}
}
}
Annotated method is below.
@NeedsPermission(Manifest.permission.CAMERA)
internal fun showCamera() {
}
Generated method and variable name may be wrong and it may be bug.
@r-ralph Thx, I think this issue is caused by getting different information from annotation processor API, but I'm not sure how internal method is inlined at compile time actually...
Is it possible to deal with this issue by not adding internal for the time being?
@hotchemi Thank you for the quick response. I deal with this problem in that way.
I guess @jmatsu had been working on the issue...any update?
fun fact: when using android studio's automatic conversion from java to kotlin it adds internal to the annotated methods, that's presumably how people end up with this behaviour
@jmatsu Any update?
@hotchemi
Sorry for delay. 馃檱
Cannot use internal methods w/ PermissionsDispatcher.
// It looks here should be `showCamera`, but compile errors happen at `line A` if it becomes `showCamera`.
static void showCamera$app_compileDebugKotlinWithCheck(MainActivity target) {
if (PermissionUtils.hasSelfPermissions(target, PERMISSION_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN)) {
// here is `line A`
// javac cannot find both of showCamera and showCamera$app_compileDebugKotlin()
target.showCamera$app_compileDebugKotlin();
} else {
ActivityCompat.requestPermissions(target, PERMISSION_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN, REQUEST_SHOWCAMERA$APP_COMPILEDEBUGKOTLIN);
}
}
According to the official document, visibility of internal entities becomes public if Java code is in same module of kotlin files.
But it seems Java code cannot access internal entities from even if all codes are in same module.
Don't use internal modifier.
Replace generated Java code with kotlin code by inserting a Java2Kotlin convert task after kapt.
We can use an official module which can convert Java to Kotlin - JetBrains/j2k.
(I haven't tried this yet, so I don't know if it's possible.)
Generate kotlin file directly by apt
I don't know if it's possible.
FYI, I created a project to verify this on AndroidStudio and IntelliJ with kotlin 1.0.7. https://github.com/jmatsu/internal-entity-access-from-java-sample
@jmatsu Thx for your dedicated investigation!!!
well...currently we nee to follow Don't use internal modifier.. Actually we're able to generate Kotlin file with annotation processing but I suppose it's too much and I'm afraid it will increase the maintenance cost of developers...
And another point is that with Kotlin probably we'll be able to handle such a problem by Delegate property approach. So we're not going to proceed any step to address the problem.
@r-ralph Are you still being in trouble with this issue? Let me clarify 馃檱
@hotchemi I'm using without internal modifier now, and it works correctly. So I am not in a trouble now. Thank you for your kindness.
Added to known issues page so let me close the issue.
https://github.com/hotchemi/PermissionsDispatcher/blob/master/doc/known_issues.md
@r-ralph now you can test inline with 3.0.0-SNAPSHOT!
@hotchemi Sorry for my late reply.
I update to 3.0.1 and it works perfectly! Thanks for the update!
Most helpful comment
@hotchemi Sorry for my late reply.
I update to 3.0.1 and it works perfectly! Thanks for the update!