We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
AppGallery Connect GuidesCloud Functions Using the HTTP Trigger to Call a FunctionAndroidCalling a FunctionIntegrating SDKs (for Eclipse)

Integrating SDKs (for Eclipse)

Integrating the AppGallery Connect SDK and Cloud Functions SDK

  1. Generate the Eclipse project package on which AppGallery Connect services depend.
    NOTE

    Before generating an Eclipse project package, ensure that the JDK and Android SDK have been installed on your PC.

    1. Download the aar2eclipse.zip tool package for generating an Eclipse project package to your PC and decompress it.
    2. Open the build.gradle file in the aar2eclipse/aar directory and check whether the Maven repository address is correct. If not, correct it.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. allprojects {
      2. repositories {
      3. maven { url 'https://developer.huawei.com/repo' }
      4. google()
      5. jcenter()
      6. }
      7. }
    1. Configure the SDK dependency address of Cloud Functions in dependencies.
      Java
      Kotlin
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. dependencies {
      2. transform "com.huawei.agconnect:agconnect-function:1.9.6.300"
      3. }
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. dependencies {
      2. transform "com.huawei.agconnect:agconnect-function-ktx:1.9.1.300"
      3. }
    2. Double-click the build.bat file in the aar2eclipse\aar directory. When the file execution is successful, the path (eclipse under aar2eclipse/aar) of the Eclipse project package is recorded in logs.

  2. Open your Eclipse project, go to File > Import, and import the generated Eclipse project package to your workspace.
    1. Go to Android > Existing Android Code Into Workspace and click Next.

    2. Click Browse and select the aar2eclipse\aar\eclipse directory where the project package is located. Select the project to be imported under Projects and click Finish. By default, the project name is BridgeActivity.
      NOTE

      The project to be imported must be located in the same drive as the workspace, for example, D:\. If they are not in the same drive, select Copy projects into workspace to copy the project files to the workspace. Do not place the project to be imported in the directory whose level is the same as that of your Eclipse project. Otherwise, the import may fail.

  3. Add the project dependency.
    1. Right-click your project and choose Properties from the shortcut menu.
    2. Choose Android from the navigation tree on the left, click Add, and select the BridgeActivity project that you have imported to the workspace.

    3. Click Apply and Close.
  4. Copy resource files in the assets directory of the imported BridgeActivity project to the same directory of your Eclipse project.

    This step is required because Eclipse cannot automatically merge these assets directories. The following figure shows the resource files to be copied.

  5. Modify the project configuration.
    1. Configure the project.properties file.

      By default, Eclipse does not support AndroidManifest.xml file merging. You need to find the project.properties file in your project directory, open it, and add the following configuration to merge the Manifest files in subprojects:

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. manifestmerger.enabled=true
    2. Set the app package name.

      Replace all ${applicationId} variables in the AndroidManifest.xml file of the BridgeActivity project with your app package name. Example:

      Before:

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. <application>
      2. <provider
      3. android:name="com.huawei.agconnect.core.provider.AGConnectInitializeProvider"
      4. android:authorities="${applicationId}.AGCInitializeProvider"
      5. android:exported="false" />

      After:

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. <application>
      2. <provider
      3. android:name="com.huawei.agconnect.core.provider.AGConnectInitializeProvider"
      4. android:authorities="com.huawei.functioneclipse.AGCInitializeProvider"
      5. android:exported="false" />
  6. Add the AppGallery Connect configuration file.
    1. Place the agconnect-services.json file in the assets directory of your project, and add the following code to the Application class of the app. This is because the Eclipse IDE does not support the AppGallery Connect plugin. If your app does not have the Application class, create one, for example, MyApplication.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. public class MyApplication extends Application{
      2. // TODO: Add the following code:
      3. @Override
      4. public void onCreate() {
      5. super.onCreate();
      6. try {
      7. AGConnectOptionsBuilder builder = new AGConnectOptionsBuilder();
      8. builder.setInputStream(getAssets().open("agconnect-services.json"));
      9. AGConnectInstance.initialize(this, builder);
      10. } catch (IOException e) {
      11. e.printStackTrace();
      12. }
      13. }
      14. // Code adding ends.
      15. }
    2. Open the AndroidManifest.xml file of your project and declare the MyApplication class in the application block. If the class has been declared, skip this step.
      Collapse
      Word wrap
      Dark theme
      Copy code
      1. <application
      2. <!-- Declaration starts. -->
      3. android:name=".MyApplication">
      4. <!-- Declaration ends. -->
      5. </application>
    3. Click Project on the Eclipse menu bar and then run the clean command.

(Optional) Passing the Key Information to AppGallery Connect

Through Parameters in the Configuration File

The AppGallery Connect SDK provides the AGConnectOptionsBuilder class to set parameters in the agconnect-services.json file. If you have selected Do not include key when downloading the file, the file does not include the client_id, client_secret, and api_key parameters. You must set these parameters in the AppGallery Connect SDK through the AGConnectOptionsBuilder class during app launch.

You are advised to set the parameters such as client_id, client_secret, and api_key in the onCreate method of Application or the first launched Activity.
Collapse
Word wrap
Dark theme
Copy code
  1. // Add the following code:
  2. try {
  3. AGConnectOptionsBuilder builder = new AGConnectOptionsBuilder();
  4. InputStream in = getAssets().open("agconnect-services.json"); // If you are using the AppGallery Connect plugin, delete this line.
  5. builder.setInputStream(in); // If you are using the AppGallery Connect plugin, delete this line.
  6. builder.setClientId("client_id");
  7. builder.setClientSecret("client_secret");
  8. builder.setApiKey("api_key");
  9. builder.setCPId("cp_id");
  10. builder.setProductId("product_id");
  11. builder.setAppId("app_id");
  12. AGConnectInstance.initialize(this, builder);
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. // TODO: Code adding ends.

You can go to Project settings > General information and check the value of each parameter, or click next to a parameter to copy the parameter value.

Replace the parameters in the code with the settings on the General information tab page as follows:

  • Replace client_id with the value of Client ID in the Project information area.
  • Replace client_secret with the value of Client secret in the Project information area.
  • Replace api_key with the value of API key in the Project information area.
  • Replace cp_id with the value of Developer ID in the Developer information area.
  • Replace product_id with the value of Project ID in the Project information area.
  • Replace app_id with the value of App ID in the App information area.

Through the Token

If you think it is insecure to store client_id and client_secret in the JSON file, you are advised to store them on your own server. You can call the API https://connect-drcn.dbankcloud.cn/agc/apigw/oauth2/v1/token to obtain a token, after which you can call setCustomCredentialsProvider to pass the token to AppGallery Connect during the AppGallery Connect SDK initialization.

Sample code for calling the API to obtain a token:

Collapse
Word wrap
Dark theme
Copy code
  1. POST /agc/apigw/oauth2/v1/token
  2. Host: connect-drcn.dbankcloud.cn
  3. Content-Type: application/json
  4. {
  5. "grant_type":"client_credentials",
  6. "client_id":client ID displayed in the App information area on the Project settings page of AppGallery Connect,
  7. "client_secret":client secret displayed in the App information area on the Project settings page of AppGallery Connect,
  8. "useJwt":1
  9. }

Response example of the API for obtaining a token:

Collapse
Word wrap
Dark theme
Copy code
  1. HTTP/1.1 200 OK
  2. Content-Type: application/json; charset=utf-8
  3. {
  4. "access_token": "eyJhbGciOiJIUzU****************",
  5. "expires_in": 0
  6. }
Sample code for calling setCustomCredentialsProvider:
Java
Kotlin
Collapse
Word wrap
Dark theme
Copy code
  1. String tokenStr = "
    access_token
    ";
  2. long expiration =
    expires_in
    ;
  3. CustomCredentialsProvider developerClientToken = new CustomCredentialsProvider() {
  4. @Override
  5. public Task<Token> getTokens(boolean forceRefresh) {
  6. TaskCompletionSource<Token> source = new TaskCompletionSource<>();
  7. Token token;
  8. token = new Token() {
  9. @Override
  10. public long getExpiration() {
  11. return expiration;
  12. }
  13. @Override
  14. public long getIssuedAt() {
  15. return 0;
  16. }
  17. @Override
  18. public long getNotBefore() {
  19. return 0;
  20. }
  21. @Override
  22. public String getTokenString() {
  23. return tokenStr;
  24. }
  25. };
  26. source.setResult(token);
  27. return source.getTask();
  28. }
  29. };
  30. AGConnectOptionsBuilder builder = new AGConnectOptionsBuilder();
  31. builder.setCustomCredentialsProvider(developerClientToken);
  32. AGConnectInstance.initialize(getApplicationContext(),builder);
Collapse
Word wrap
Dark theme
Copy code
  1. val tokenStr = "
    access_token
    "
  2. val expiration: Long =
    expires_in
  3. val developerClientToken = CustomCredentialsProvider {
  4. val source = TaskCompletionSource<Token>()
  5. val token: Token
  6. token = object : Token {
  7. override fun getExpiration(): Long {
  8. return expiration
  9. }
  10. override fun getIssuedAt(): Long {
  11. return 0
  12. }
  13. override fun getNotBefore(): Long {
  14. return 0
  15. }
  16. override fun getTokenString(): String {
  17. return tokenStr
  18. }
  19. }
  20. source.setResult(token)
  21. source.task
  22. }
  23. val builder = AGConnectOptionsBuilder()
  24. builder.setCustomCredentialsProvider(developerClientToken)
  25. AGConnectInstance.initialize(getApplicationContext(), builder)

Configuring Obfuscation Scripts

Before building the APK, configure obfuscation scripts to prevent the AppGallery Connect SDK from being obfuscated. If obfuscation arises, the AppGallery Connect SDK may not function properly.

  1. Open the obfuscation configuration file.
  2. Add configurations to exclude the AppGallery Connect SDK from obfuscation.

    Add configurations to the proguard-project.txt file to exclude the AppGallery Connect SDK from obfuscation.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. -keep class com.huawei.agconnect.remoteconfig.*{*;}
    2. -keepclassmembers class **{
    3. public <init>(android.content.Context,com.huawei.agconnect.AGConnectInstance);
    4. }
    5. -keepclassmembers class com.huawei.agconnect.remoteconfig.internal.server.**{*;}
    6. -keep class * implements android.os.Parcelable {
    7. public static final android.os.Parcelable$Creator *;
    8. }

(Optional) Initializing the AppGallery Connect SDK

If multiple processes are running concurrently, you need to call an initialization method in each subprocess to initialize the AppGallery Connect SDK after it is integrated into your app.

Add the initialization code to the onCreate method of Application:

Collapse
Word wrap
Dark theme
Copy code
  1. if (AGConnectInstance.getInstance() == null) {
  2. AGConnectInstance.initialize(getApplicationContext());
  3. }
Search in Guides
Enter a keyword.