My plugin does not work in solar2d Android Native

Hi, I’m trying to make my java plugin in solar2d, but I can’t get the standard example working.

I open in Android Studio a standard example from solar2d: Native folder -> Project Template -> App -> android
The main.lua file in the Corona folder:

local library = require "plugin.library"

-- This event is dispatched to the global Runtime object
-- by `didLoadMain:` in MyCoronaDelegate.mm
local function delegateListener( event )
	native.showAlert(
		"Event dispatched from `didLoadMain:`",
		"of type: " .. tostring( event.name ),
		{ "OK" } )
end
Runtime:addEventListener( "delegate", delegateListener )

-- This event is dispatched to the following Lua function
-- by PluginLibrary::show() in PluginLibrary.mm
local function listener( event )
	print( "Received event from Library plugin (" .. event.name .. "): ", event.message )
end

library.init( listener )

local object = display.newRect( 100,100,50,50 )
 
local function onObjectTouch( event )
    if ( event.phase == "ended" ) then
        library.show( "corona" )
        print("TEST",library.show( "corona" ))
    end
    return true
end
object:addEventListener( "touch", onObjectTouch )

When you click on the white square, this code calls the show function from the java plugin and print:

“Hello!”

and

“TEST” “Hello!”

When running and building in Android Studio, the plugin works.

Next, I want to make a plugin for Corona Simulator.
Using the instructions: https://www.qweb.co.uk/blog/creating-a-corona-native-plugin-with-android-studio

I create plugin-release.aar, and extract classes.jar, rename it to plugin.library.jar.
plugin-release.aar
Next, create metadata.lua and move it all to the data folder. In the end, I get the following: Then I compress the resulting folder in gzip and get the data.tgz plugin ready!
plugin_1
plugin_2

build.settings:

settings = 
{
	splashScreen = 
    {
        enable = false
    },
     plugins =
    {
        ["plugin.library"] =
        {
          publisherId = "com.coronalabs",
          supportedPlatforms = { android=true }
        },
    },    

	iphone =
	{
		xcassets = "Images.xcassets",
		plist =
		{
			UILaunchStoryboardName = "LaunchScreen",
		},
	},
}

I copy this to: /Users/user_name/Solar2DPlugins/com.coronalabs/plugin.library/android I make the assembly in solar2d 2020.3594. the assembly passes without errors, when I start Corona Test.apk on the device, I immediately get errors:

2020-05-22 11:37:31.877 2212-2212/? D/Launcher.CellLayout: touch item:ShortcutInfo, id=276, itemType=0, user=UserHandle{0}, mIconType=0, mTitle=Corona Test, mLabel=Corona Test, pkgName=com.coronalabs.Corona, className=com.ansca.corona.CoronaActivity, screenId=7, container=-100, cellX=1, cellY=4, spanX=1, spanY=1, isLandscapePos=false
2020-05-22 11:37:31.958 2212-2212/? I/Timeline: Timeline: Activity_launch_request time:478222031 intent:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.coronalabs.Corona/com.ansca.corona.CoronaActivity bnds=[331,1305][499,1473] (has extras) }
2020-05-22 11:37:31.959 1204-2391/? W/XSpaceManagerService: checkXSpaceControl, from:com.miui.home, to:com.coronalabs.Corona, with act:android.intent.action.MAIN, callingUserId:0, toUserId:0
2020-05-22 11:37:31.961 1204-2391/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.coronalabs.Corona/com.ansca.corona.CoronaActivity bnds=[331,1305][499,1473] (has extras)} from uid 10028
2020-05-22 11:37:31.969 578-578/? E/ANDR-IOP: IOP HAL: Received pkg_name = com.coronalabs.Corona pid = -1
2020-05-22 11:37:31.971 1204-2391/? W/WindowManager: Attempted to set focus to non-existing app token: Token{3618ba0 ActivityRecord{6d36359 u0 com.coronalabs.Corona/com.ansca.corona.CoronaActivity t1110}}
2020-05-22 11:37:31.973 1204-2391/? D/ActivityTrigger: activityStartTrigger: Activity is Triggerred in full screen ApplicationInfo{54202ae com.coronalabs.Corona}
2020-05-22 11:37:31.973 1204-2391/? E/ActivityTrigger: activityStartTrigger: not whiteListedcom.coronalabs.Corona/com.ansca.corona.CoronaActivity/1
2020-05-22 11:37:31.975 1204-2391/? D/ActivityTrigger: activityResumeTrigger: The activity in ApplicationInfo{54202ae com.coronalabs.Corona} is now in focus and seems to be in full-screen mode
2020-05-22 11:37:31.975 1204-2391/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.coronalabs.Corona/com.ansca.corona.CoronaActivity/1
2020-05-22 11:37:31.981 1204-1221/? D/ActivityTrigger: activityResumeTrigger: The activity in ApplicationInfo{54202ae com.coronalabs.Corona} is now in focus and seems to be in full-screen mode
2020-05-22 11:37:31.982 1204-1221/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.coronalabs.Corona/com.ansca.corona.CoronaActivity/1
2020-05-22 11:37:31.999 1204-1221/? D/Boost: hostingType=activity, hostingName=com.coronalabs.Corona/com.ansca.corona.CoronaActivity, callerPackage=com.miui.home, isSystem=true, isBoostNeeded=false.
2020-05-22 11:37:31.999 1204-1221/? I/ActivityManager: Start proc 11507:com.coronalabs.Corona/u0a311 for activity com.coronalabs.Corona/com.ansca.corona.CoronaActivity caller=com.miui.home
2020-05-22 11:37:32.004 4295-4620/? D/PowerKeeper.Event: notifyAMProcStart processName: 10311 reason: com.coronalabs.Corona, pid:0
2020-05-22 11:37:32.144 11507-11507/? I/zygote64: Thread[1,tid=11507,Native,Thread*=0x7ab700dc00,peer=0x72af07b0,"main"] recursive attempt to load library "/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/lib/arm64/libjnlua5.1.so"
2020-05-22 11:37:32.166 11507-11507/? D/AccessibilityManager: AccessibilityManager status: mPackageName = com.coronalabs.Corona, mOptimizeEnabled = true, mIsEnabled = false, mIsUiAutomationEnabled = false, mIsInterestedPackage =false
2020-05-22 11:37:32.331 11507-11507/? W/com.coronalabs.Corona: type=1400 audit(0.0:7316): avc: denied { read } for comm=474C546872656164203133373433 name="u:object_r:opengles_prop:s0" dev="tmpfs" ino=14512 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:opengles_prop:s0 tclass=file permissive=0
2020-05-22 11:37:32.366 11507-11523/? V/Corona: > Class.forName: network.LuaLoader
2020-05-22 11:37:32.367 11507-11523/? V/Corona: < Class.forName: network.LuaLoader
2020-05-22 11:37:32.367 11507-11523/? V/Corona: Loading via reflection: network.LuaLoader
2020-05-22 11:37:32.373 11507-11523/? I/Corona: Platform: MI 5 / ARM64 / 8.0.0 / Adreno (TM) 530 / OpenGL ES 3.2 V@269.0 (GIT@33f3a03, I26dffed9a4) (Date:04/17/18) / 2020.3594 / русский | RU | ru_RU | ru
2020-05-22 11:37:32.382 11507-11523/? V/Corona: > Class.forName: shared.google.play.services.base.LuaLoader
2020-05-22 11:37:32.393 11507-11523/? V/Corona: > Class.forName: CoronaProvider.licensing.google.LuaLoader
2020-05-22 11:37:32.393 11507-11523/? V/Corona: < Class.forName: CoronaProvider.licensing.google.LuaLoader
2020-05-22 11:37:32.393 1204-1337/? I/ActivityManager: Displayed com.coronalabs.Corona/com.ansca.corona.CoronaActivity: +398ms
2020-05-22 11:37:32.394 4295-4620/? D/PowerKeeper.Event: notifyActivityLaunchTime: com.coronalabs.Corona/com.ansca.corona.CoronaActivity totalTime: 398
2020-05-22 11:37:32.395 11507-11523/? V/Corona: Loading via reflection: CoronaProvider.licensing.google.LuaLoader
2020-05-22 11:37:32.396 578-578/? E/ANDR-IOP: IOP HAL: Received pkg_name = com.coronalabs.Corona pid = 11507
2020-05-22 11:37:32.397 1204-1337/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{6d36359 u0 com.coronalabs.Corona/com.ansca.corona.CoronaActivity t1110} time:478222471
2020-05-22 11:37:32.399 11507-11523/? V/Corona: > Class.forName: plugin.library.LuaLoader
2020-05-22 11:37:32.401 11507-11523/? I/Corona: ERROR: Runtime error
    module 'plugin.library' not found:resource (plugin.library.lu) does not exist in archive
    	no field package.preload['plugin.library']
    	no file '(null)/plugin/library.lua'
    	no file '(null)/plugin/library.lua'
    	no file '/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/lib/arm64/libplugin/library.so'
    	no file './plugin/library.so'
    	no file '(null)/plugin/library.so'
    	no file '/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/lib/arm64/libplugin.so'
    	no file './plugin.so'
    	no file '(null)/plugin.so'
    	no file '/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/lib/arm64/libplugin.library.so'
    	no file './plugin.library.so'
    	no file '(null)/plugin.library.so'
    	no Java class 'plugin.library.LuaLoader'
    	no global loaded symbol 'luaopen_plugin_library'
    	no zipped .so symbol at '/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/base.apk!/lib/arm64-v8a/libplugin.library.so'
    stack traceback:
    	[C]: in function 'require'
    	?: in function 'require'
    	/Applications/Corona-3594/Na
2020-05-22 11:37:32.402 11507-11523/? I/Corona: ERROR: Runtime error
2020-05-22 11:37:32.402 11507-11523/? I/Corona: module 'plugin.library' not found:resource (plugin.library.lu) does not exist in archive
    	no field package.preload['plugin.library']
    	no file '(null)/plugin/library.lua'
    	no file '(null)/plugin/library.lua'
    	no file '/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/lib/arm64/libplugin/library.so'
    	no file './plugin/library.so'
    	no file '(null)/plugin/library.so'
    	no file '/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/lib/arm64/libplugin.so'
    	no file './plugin.so'
    	no file '(null)/plugin.so'
    	no file '/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/lib/arm64/libplugin.library.so'
    	no file './plugin.library.so'
    	no file '(null)/plugin.library.so'
    	no Java class 'plugin.library.LuaLoader'
    	no global loaded symbol 'luaopen_plugin_library'
    	no zipped .so symbol at '/data/app/com.coronalabs.Corona-1SQe4c9abv-nOMZWYw9K7w==/base.apk!/lib/arm64-v8a/libplugin.library.so'
    stack traceback:
    	[C]: ?
    	[C]: in function 'require'
    	?: in function 'require'
    	/Applications/Corona-3594/Native/Project Template/App/Corona/main.lua:1: in main chunk
2020-05-22 11:37:32.474 631-631/? D/SurfaceFlinger: duplicate layer name: changing com.coronalabs.Corona/com.ansca.corona.CoronaActivity to com.coronalabs.Corona/com.ansca.corona.CoronaActivity#1

app-debug.apk from Android Studio work: https://drive.google.com/file/d/1rPWoA90S_tEB21ThrRyGh-n5jX5NOEgg/view?usp=sharing
Corona Test.apk from Simulator not work: https://drive.google.com/file/d/1SxzYj4s5GVnCTQ8-RyxQlSl2o3Q16K-U/view?usp=sharing

Links to Corona instructions that I used:
https://docs.coronalabs.com/native/hostedPlugin.html
https://docs.coronalabs.com/native/plugin/index.html
https://docs.coronalabs.com/native/plugin/submission.html

What am I doing wrong? How to make your own plugin for solar2d?

1 Like

You can make the test easier:
LuaLoader code:

package plugin.library;

import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ansca.corona.CoronaActivity;
import com.ansca.corona.CoronaEnvironment;
import com.ansca.corona.CoronaLua;
import com.ansca.corona.CoronaRuntime;
import com.ansca.corona.CoronaRuntimeListener;
import com.ansca.corona.CoronaRuntimeTask;
import com.ansca.corona.CoronaRuntimeTaskDispatcher;
import com.naef.jnlua.JavaFunction;
import com.naef.jnlua.LuaState;
import com.naef.jnlua.NamedJavaFunction;
import android.util.Log;

@SuppressWarnings(“WeakerAccess”)
public class LuaLoader implements JavaFunction{
@Override
public int invoke(LuaState luaState) {
Log.d(“Corona native”, “Lua Loader invoke called”);
return 0;
}
}

main.lua code:

local plugin = require(“plugin.library”)
print(“test:”, plugin)

In build on Android Studio get print - true
In build simulator the same error that I described above :frowning:

How to make my plugin work? Why does it work with standard plugins?

As I suggested in this topic:

Hopefully someone will come up with an indepth how-to starter-guide for android native!

I realized what the problem is! It all worked!
I did not compress the plugin correctly in the archive. Squeezing the plugin through the console as it is written here https://docs.coronalabs.com/native/hostedPlugin.html it worked!

2 Likes

What’s up in recent builds? Native plugins are no longer added to the assembly and do not work :frowning:

Has anyone managed to use a custom native plugin in a recent build?

I understood what the problem is. It says here: https://docs.coronalabs.com/native/android/index.html
libs / - This is where third-party (Corona) .jar plugins that your app relies upon should go.

In recent Solar 2D versions, when compiling .jar files from this folder are not added to classes.dex. Please correct, in 3594 everything works correctly