Using Component properties in Buildserver's Compiler.java

95 views
Skip to first unread message

Moliata

unread,
Aug 7, 2017, 4:11:27 PM8/7/17
to App Inventor Open Source Development
Hello everyone, I have a question,
Imagine, I have a component MyComponent, containing DesignerProperty used for setting variable value.
How should I reference my component in Compiler.java & how should I get/return variable's value?
Thank you for the help.

Evan Patton

unread,
Aug 8, 2017, 1:49:54 PM8/8/17
to App Inventor Open Source Development
Hi Moliata,

I don't think we there's really a good mechanism for this. For properties that need to be accessed at this level we make them into project properties. As an example, see how the MockWebview sets a project property when the UsesLocation property is changed. This is then referenced in Compiler.java. Unfortunately, this hasn't been generalized (but it would make a good project), so you'll have to implement something similar for your situation. More importantly, you should think about whether the way you are implementing the component really needs to have this level of integration with the compiler as it will save you a lot of time if you can implement it differently.

Evan

Moliata

unread,
Aug 8, 2017, 4:30:43 PM8/8/17
to App Inventor Open Source Development
Well, component has an API Key property, which is used in manifest. Good example, there is a Google Map, which requires to add a tag with api key to manifest. That's why I need such level of integration.
Thank you for the example, I will look into it & reply as soon as possible.
Message has been deleted

Moliata

unread,
Aug 8, 2017, 4:56:59 PM8/8/17
to App Inventor Open Source Development
Evan, if I am right, I have to modify only 5 files - Compiler.java; Project.java; SettingsConstants.java; YoungAndroidProjectService.java; project.properties (Optional/Only if using Companion)?
Thank you for the help.

Moliata

unread,
Aug 8, 2017, 5:08:30 PM8/8/17
to App Inventor Open Source Development
Also, I want to add, that my component is not visible.


2017 m. rugpjūtis 7 d., pirmadienis 23:11:27 UTC+3, Moliata rašė:

Evan Patton

unread,
Aug 9, 2017, 4:53:59 PM8/9/17
to App Inventor Open Source Development
That sounds about right to me. There may be a few other places, such as the unit tests, where the project.properties file is created and tested. The project.properties stores settings related to the project and that are used in the compilation step, so I would not agree that it is optional/companion only.

Evan

Moliata

unread,
Aug 10, 2017, 5:39:07 AM8/10/17
to App Inventor Open Source Development
Ok, so the last question. What if my component doesn't have MockComponent class, as it is not visible. Without MockComponent class, will everything work the same?

Evan Patton

unread,
Aug 10, 2017, 9:13:13 AM8/10/17
to App Inventor Open Source Development
hi Moliata,

So long as nonVisible is set to true in the @DesignerProperty annotation you will be fine. SimpleComponentDescriptor (under the appengine subtree) returns a MockNonvisibleComponent without any further configuration necessary when nonVisible is true.

Evan

Moliata

unread,
Aug 10, 2017, 11:11:04 AM8/10/17
to App Inventor Open Source Development
Ok, thanks for the info.

Moliata

unread,
Aug 14, 2017, 3:19:08 AM8/14/17
to App Inventor Open Source Development
Doesn't work, cannot seem to make anything work. I modified project.properties; YoungAndroidProjectService.java; Project.java; SettingsConstants.java; FileImporterImpl.java; MyComponent.java & Compiler.java


2017 m. rugpjūtis 7 d., pirmadienis 23:11:27 UTC+3, Moliata rašė:

Evan Patton

unread,
Aug 14, 2017, 8:55:55 AM8/14/17
to App Inventor Open Source Development
Hi Moliata,

Your message doesn't give me much to go on, but here's my most likely scenario. If you follow the structure we use for other project level properties, like in MockForm, then you should have some function in MockMoliatasComponent like setAPIKey defined like:

private void setAPIKey(String apikey) {
  editor.getProjectEditor().changeProjectSettingsProperty(SettingsConstants.PROJECT_YOUNG_ANDROID_SETTINGS, <property-name>, apikey).
}

You'll also want to guard against multiple instances of your component, for example, because if I bring in a second instance it will overwrite the project value set in the first instance. We usually address this by making the properties be on the Form and then checking whether the current screen is Screen1 or not.

Evan

Moliata

unread,
Aug 14, 2017, 11:12:03 AM8/14/17
to App Inventor Open Source Development
The thing is that, my component is not visible, so I should I modify MockNonvisibleComponent class. Can you give only *must* to edit files? MyComponent is not going to add new permissions, it won't affect how app works. All I want to do, is to make sure that ApiKey property's value is referencable inside Compiler.java, so I could add required metadata tag. Because, I am not sure, do we actually need to edit YoungAndroidProjectService; YoungAndroidSettings; FileImpoterImpl or other stuff like project.properties.
***
To help you understand, what I am trying to achieve, I will you give a simple example. There is a nonvisible component MyComponent.java, that only has a single designer property ApiKey. When app is being compiled, apikey property's value should be referenced inside Compiler.java, so my component would create a <meta-data> xml tag with a value of apikey.
***
Thank you for the help.

Evan Patton

unread,
Aug 15, 2017, 9:16:42 AM8/15/17
to App Inventor Open Source Development
I would recommend against modifying the MockNonvisibleComponent to add a component-specific behavior. Instead, consider subclassing MockNonvisibleComponent with your own version, similar to how we have MockFirebase component to handle showing the Firebase experimental warning.

Evan

Moliata

unread,
Aug 15, 2017, 9:25:47 AM8/15/17
to App Inventor Open Source Development
Evan, you wanted to add this in different topic, I see :D

Evan Patton

unread,
Aug 16, 2017, 3:48:39 PM8/16/17
to App Inventor Open Source Development
Indeed, although my advice isn't any different for this thread as well. For this specific case, you need to subclass MockNonvisibleComponent so that you can override onPropertyChange() so that after the API Key is changed you can update the project settings. Right now there is no general mechanism to indicate to the system that a component level property is intended for the project.properties file. It's not often that we need to do this and (on average once per year), so it has just been implemented on a per-property basis. In most cases, we only do this for Screen1 because the other challenge that you will face is if someone drags in a second copy of your extension it will overwrite the API key defined by the first component, so keep that in mind as well.

Evan

Moliata

unread,
Aug 16, 2017, 4:02:04 PM8/16/17
to App Inventor Open Source Development
Hey, can actually files that *must* be changed, not can be. SettingsConstants, MockNonvisibleComponent & something more?

Evan Patton

unread,
Aug 16, 2017, 5:30:09 PM8/16/17
to App Inventor Open Source Development
Hi Moliata,

Looking at a recent change I've worked on for Form to add a project level property, I see the following:

appinventor/appengine/src/com/google/appinventor/shared/settings/SettingsConstants.java

1. Add a constant for your new project setting.

appinventor/appengine/src/com/google/appinventor/client/settings/project/YoungAndroidSettings.java

2. Add a default value for your new project setting.

appinventor/appengine/src/com/google/appinventor/server/project/youngandroid/YoungAndroidProjectService.java

3. Update getProjectSettings, getProjectPropertiesFileContents, storeProjectSettings, newProject, and copyProject to reflect the addition of your new project setting.

appinventor/appengine/src/com/google/appinventor/server/FileImporterImpl.java

4. Update importProject to reflect your changes in 3.

appinventor/appengine/tests/com/google/appinventor/server/ProjectServiceTest.java

5. Update testCopyProject to reflect your changes in 3 so that the tests pass.

appinventor/appengine/src/com/google/appinventor/client/editor/simple/components/MockForm.java (or replace with a subclass of MockNonvisibleComponent specific to your component)

6. Override onPropertyChange and provide a check for your new property that calls changeProjectSettingsProperty() to set the new setting defined in 1.

appinventor/buildserver/src/com/google/appinventor/buildserver/Project.java

7. Add a getter for the new setting from the project properties.

appinventor/buildserver/src/com/google/appinventor/buildserver/Compiler.java

8. Add whatever you need to do to consume the setting from Project as part of the compilation process.

appinventor/components/src/com/google/appinventor/components/runtime/Form.java (replace with your actual component)

9. Provide optional behavior for what happens in the companion if the property changes.

Evan
Message has been deleted

Moliata

unread,
Aug 17, 2017, 1:46:42 AM8/17/17
to App Inventor Open Source Development
Together with all these files, I have to edit project.properties too?
Also, thank you for the help, this is really helpful for telling what I have to edit & why I have to do so.

Evan W. Patton

unread,
Aug 17, 2017, 2:17:26 AM8/17/17
to app-inventor-o...@googlegroups.com
Hi Moliata,

By virtue of calling the changeProjectProperty method as part of your mock component the client will trigger a write back to the YoungAndroidProjectEditor, and that updates the project.properties file. If you make the changes at all the points I outlined it should be sufficient. The only other think you might need to consider is adding the entry to the Companion app's project.properties under the aiplayapp directory. 

Evan

On Aug 17, 2017, at 01:42, Moliata <benas...@gmail.com> wrote:

Together with all these files, I have to edit project.properties too?
Also, thank you for the help, this is really helpful for telling what I have to edit & why I have to do so.

--
You received this message because you are subscribed to the Google Groups "App Inventor Open Source Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to app-inventor-open-so...@googlegroups.com.
To post to this group, send email to app-inventor-o...@googlegroups.com.
Visit this group at https://groups.google.com/group/app-inventor-open-source-dev.
For more options, visit https://groups.google.com/d/optout.

Moliata

unread,
Aug 17, 2017, 2:33:44 AM8/17/17
to App Inventor Open Source Development
Hello, yes I was talking about Companion's project.properties, thank you for the help. Is this correct?

appinventor/aiplayapp/youngandroidproject/project.properties
Used for setting default values for Companion.

This was the last question.

Evan Patton

unread,
Aug 17, 2017, 9:16:37 AM8/17/17
to App Inventor Open Source Development
Hi Moliata,

Just a word of caution. It is not advisable to put API keys in a commit that may be published in the future. If you plan to add an API key to project.properties, then you should maintain a separate branch with a commit that makes the appropriate change. Otherwise, if you ever make the information public, others could use your API key and you would get charged for the service.

Evan

Moliata

unread,
Aug 17, 2017, 10:14:03 AM8/17/17
to App Inventor Open Source Development
Ok, thank you for the help. It helped me a lot of. Thumbs up!
Reply all
Reply to author
Forward
0 new messages