Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

onuralpszr
Copy link
Contributor

@onuralpszr onuralpszr commented Apr 1, 2023

Hello,

Android Gradle Plugin version 8.0 is asking for namespace. This is become mandatory and after I update my AGP to 8.0, I got this error

Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

android {
    namespace 'com.example.namespace'
}

If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.

This change fix this future releases. However I am not sure how opencv wants to user namespace I used "org.opencv" if there is a different namespace please let me know so I can changed that too. Also should I add namepsace into "opencv/modules/java/android_sdk/android_gradle_lib/build.gradle" here ?

Sources

Android developer link: https://developer.android.com/studio/preview/features#namespace-dsl
Issue Tracker Google: https://issuetracker.google.com/issues/191813691?pli=1#comment19

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@asmorkalov
Copy link
Contributor

OpenCV still uses very old versions of NDK, SDK and Gradle for pre-commit testing and releases. Gradle update at least to 7.3.3 and Android plugin update at least to 7.2 is required. Thanks a lot for the contribution! We will return back to your patch after build infra update.

@onuralpszr
Copy link
Contributor Author

onuralpszr commented Apr 11, 2023

Using AGP 7.x basically fix the problem. With AGP 8.0 it become mandatory, it does fix that too

endforeach()

# set build.gradle namespace
set(OPENCV_NAMESPACE "org.opencv" CACHE STRING "OpenCV Namespace string")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use version check to avoid breaking of existed build configurations:

if(... GRADLE VERSION CHECK ...)
    ocv_update(OPENCV_NAMESPACE_DECLARATION "namespace 'org.opencv'")
else()
    ocv_update(OPENCV_NAMESPACE_DECLARATION "")
endif()

and below:

 android {
+    @OPENCV_NAMESPACE_DECLARATION@

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made those changes accordingly so please check it out.

@onuralpszr
Copy link
Contributor Author

onuralpszr commented Apr 15, 2023

I don't wanna open up a new pull request and I can If needs to be but we also need to add

    buildFeatures {
        aidl true
    }

https://developer.android.com/build/releases/gradle-plugin#default-changes

Inside of the gradle file because new default become false, gradle 7 and 5 was "true" so it won't effect them and It should be fine for people who start brand new project so they can avoid aidl error for people who start with new gradle 8.0

@onuralpszr
Copy link
Contributor Author

@asmorkalov I was also looking for JDK version but could not found it.

https://developer.android.com/build/releases/gradle-plugin#compatibility-8-0-0 also set JDK minimum to 17. As far as I know opencv uses jdk 11 but user needs to set them to 17 for newer gradle 8.0+ for old versions they can leave as it is. I already tested JDK 17 and it works fine.

For gradle 8.0

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    

For older version I just need to VERSION_11 (based on opencv minimum) I would like to add this so It can build without error. So can we add at least something similar to gradle or add hard coded VERSION_11 and advise users to change on of top gradle readme like similar to set SDK versions

@onuralpszr onuralpszr changed the title gradle 8.0 build.gradle namespace requirement added gradle 8.0 build.gradle namespace and aidl buildFeature requirement added Apr 16, 2023
@asmorkalov
Copy link
Contributor

CI reports issue:

cmake/android/android_gradle_projects.cmake:85: trailing whitespace.
+# set build.gradle namespace 

@onuralpszr onuralpszr force-pushed the gradle80_namespace branch 2 times, most recently from 2baf144 to 673667c Compare April 19, 2023 02:10
Comment on lines 85 to 90
# set build.gradle namespace
if(GRADLE_VERSION STREQUAL "5.6.4")
ocv_update(OPENCV_NAMESPACE_DECLARATION "")
else()
ocv_update(OPENCV_NAMESPACE_DECLARATION "namespace 'org.opencv'")
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if GRADLE_VERSION is 5.6.3?

Copy link
Contributor Author

@onuralpszr onuralpszr Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Namescape came with AGP 7.3 (https://developer.android.com/build/releases/gradle-plugin) so is it okay to set minimum 7.3.0 and any higher should be fine. Any lower version basically won't use it. That include 5.6.3 or any X version lower then 7.3.0

# set build.gradle namespace 
if(GRADLE_VERSION VERSION_GREATER_EQUAL "7.3.0")
  ocv_update(OPENCV_NAMESPACE_DECLARATION "namespace 'org.opencv'")
else()
  ocv_update(OPENCV_NAMESPACE_DECLARATION "")
endif()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, condition should use GE version check instead of strong equality.

Copy link
Contributor Author

@onuralpszr onuralpszr Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean this ?

if(GRADLE_VERSION VERSION_GREATER "7.2.9").

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both cases should do the similar thing:

if(GRADLE_VERSION VERSION_GREATER_EQUAL "7.3.0")
if(GRADLE_VERSION VERSION_GREATER "7.2.9")

The first option is clear however.


BTW, "gradle plugin version" is not the same as "gradle version".
Plugin version is specified through ANDROID_GRADLE_PLUGIN_VERSION

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed with AGP and push it.

@opencv-alalek opencv-alalek added this to the 4.8.0 milestone Apr 19, 2023
@onuralpszr onuralpszr force-pushed the gradle80_namespace branch 2 times, most recently from 4cab819 to 44a8822 Compare April 19, 2023 12:09
@onuralpszr onuralpszr changed the title gradle 8.0 build.gradle namespace and aidl buildFeature requirement added AGP 8.0 build.gradle namespace and aidl buildFeature requirement added Apr 19, 2023
@onuralpszr
Copy link
Contributor Author

onuralpszr commented Apr 19, 2023

@opencv-alalek can AGP version set to at least 4.1 ? AGP 8.0 gonna is failing in "aidl" and current AGP is too old to understand aidl feature. If it is gonna stay for now, How can I add multi line with "\n" "\t" etc. ? Because I need to add into either same condition or need to set different condition and set to >= 4.1 and as well.

Action : https://github.com/opencv/opencv/actions/runs/4738797811/jobs/8421666401#step:12:500

@onuralpszr
Copy link
Contributor Author

@asmorkalov I was also looking for JDK version but could not found it.

https://developer.android.com/build/releases/gradle-plugin#compatibility-8-0-0 also set JDK minimum to 17. As far as I know opencv uses jdk 11 but user needs to set them to 17 for newer gradle 8.0+ for old versions they can leave as it is. I already tested JDK 17 and it works fine.

For gradle 8.0

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    

For older version I just need to VERSION_11 (based on opencv minimum) I would like to add this so It can build without error. So can we add at least something similar to gradle or add hard coded VERSION_11 and advise users to change on of top gradle readme like similar to set SDK versions

@opencv-alalek I also need suggestion for this one too because of AGP(8.0) again.

@opencv-alalek
Copy link
Contributor

Perhaps we need to introduce some variable for Java version (similar to ANDROID_GRADLE_PLUGIN_VERSION).
E.g., ANDROID_GRADLE_JAVA_VERSION (prefer digits only to use version comparison).

add "ANDROID" into namespace variable avoid future name conflict

Signed-off-by: Onuralp SEZER <[email protected]>
@onuralpszr
Copy link
Contributor Author

@opencv-alalek both changed added.

@opencv-alalek
Copy link
Contributor

@onuralpszr Thanks! Triggered CI with latest changes.

Please take a look on the comment above about ANDROID_GRADLE_JAVA_VERSION proposal for JavaVersion description (if required).

@onuralpszr
Copy link
Contributor Author

onuralpszr commented Apr 26, 2023

Perhaps we need to introduce some variable for Java version (similar to ANDROID_GRADLE_PLUGIN_VERSION). E.g., ANDROID_GRADLE_JAVA_VERSION (prefer digits only to use version comparison).

Sure, based on suggestion here is how is it look like

Cmake file changes

set(ANDROID_GRADLE_JAVA_VERSION 1.8 CACHE STRING "Android Gradle Java version")
message(STATUS "Android Gradle Java version: ${ANDROID_GRADLE_JAVA_VERSION}")
# set android gradle java version in build.gradle
if(NOT (ANDROID_GRADLE_PLUGIN_VERSION VERSION_LESS "8.0.0"))
  # AGP-8.0 requires a minimum JDK version of JDK17
  ocv_update(ANDROID_GRADLE_JAVA_VERSION "17")
else()
  ocv_update(ANDROID_GRADLE_JAVA_VERSION "${ANDROID_GRADLE_JAVA_VERSION}")
endif()

Build.gradle file changes

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_@ANDROID_GRADLE_JAVA_VERSION@
        targetCompatibility JavaVersion.VERSION_@ANDROID_GRADLE_JAVA_VERSION@
    }

@opencv-alalek
Copy link
Contributor

how is it look like

Looks good.
Please apply changes and we will check them on existed build configurations.

@onuralpszr
Copy link
Contributor Author

onuralpszr commented Apr 26, 2023

how is it look like

Looks good. Please apply changes and we will check them on existed build configurations.

I added JDK sections please trigger CI and one more thing,

I removed "aidl" part temporally because it needs to be added via condition because current AGP in CI not support "buildFeatures" which failed on last CI trigger.

    buildFeatures {
        aidl true
    }

@onuralpszr
Copy link
Contributor Author

@opencv-alalek

I set to JDK 1.8 based on CI and I also fix the typo "AGP8.0 JDK version 17 not 18 sorry about that. I fix both and please trigger the CI

@opencv-alalek
Copy link
Contributor

BTW, there are "config" files for Android packages here: https://github.com/opencv/opencv/tree/4.x/platforms/android

It makes sense to create new one configuration file which describes your tested configuration (where to pass required CMake parameters (like ANDROID_GRADLE_JAVA_VERSION) through cmake_vars dictionary).

set(ANDROID_GRADLE_PLUGIN_VERSION "3.2.1" CACHE STRING "Android Gradle Plugin version")
message(STATUS "Android Gradle Plugin version: ${ANDROID_GRADLE_PLUGIN_VERSION}")

set(ANDROID_GRADLE_JAVA_VERSION "1_8" CACHE STRING "Android Gradle Java version")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ocv_update(ANDROID_GRADLE_JAVA_VERSION "17") below would not work after that (there is "no op" logic if value exists for the variable)

We could use it in this form (condition check should go first) - see _INIT suffix:

# set android gradle java version in build.gradle
if(NOT (ANDROID_GRADLE_PLUGIN_VERSION VERSION_LESS "8.0.0"))
  # AGP-8.0 requires a minimum JDK version of JDK17
  ocv_update(ANDROID_GRADLE_JAVA_VERSION_INIT "17")
else()
  ocv_update(ANDROID_GRADLE_JAVA_VERSION_INIT "1_8")
endif()
set(ANDROID_GRADLE_JAVA_VERSION "${ANDROID_GRADLE_JAVA_VERSION_INIT}" CACHE STRING "Android Gradle Java version")
message(STATUS "Android Gradle Java version: ${ANDROID_GRADLE_JAVA_VERSION}")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed the changes

and set minimum version to JDK 1_8 for Android builds,
use JDK17 for AGP version 8.0 or above.

Signed-off-by: Onuralp SEZER <[email protected]>
@onuralpszr
Copy link
Contributor Author

BTW, there are "config" files for Android packages here: https://github.com/opencv/opencv/tree/4.x/platforms/android

It makes sense to create new one configuration file which describes your tested configuration (where to pass required CMake parameters (like ANDROID_GRADLE_JAVA_VERSION) through cmake_vars dictionary).

I am not sure I understand this, because problem is based on AGP/Gradle/Java but not "cmake" ? I didn't understand that adding another config file based on "which ndk" version ?

Copy link
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@onuralpszr
Copy link
Contributor Author

onuralpszr commented Apr 27, 2023

how is it look like

Looks good. Please apply changes and we will check them on existed build configurations.

I added JDK sections please trigger CI and one more thing,

I removed "aidl" part temporally because it needs to be added via condition because current AGP in CI not support "buildFeatures" which failed on last CI trigger.

    buildFeatures {
        aidl true
    }

@opencv-alalek we still need to add "aidl" part without that it will fail on version 8.0.0 and above.

# set android gradle java version in build.gradle and set aidl config
if(NOT (ANDROID_GRADLE_PLUGIN_VERSION VERSION_LESS "8.0.0"))
  # AGP-8.0 requires a minimum JDK version of JDK17
  ocv_update(ANDROID_GRADLE_JAVA_VERSION_INIT "17")
  # Enable aidl configuration for OpenCV compile with AGP-8.0
  ocv_update(ANDROID_GRADLE_BUILD_FEATURE_AIDL "buildFeatures { aidl true }")
else()
  ocv_update(ANDROID_GRADLE_JAVA_VERSION_INIT "1_8")
  ocv_update(ANDROID_GRADLE_BUILD_FEATURE_AIDL "")
endif()

@onuralpszr
Copy link
Contributor Author

LGTM +1

Thank you for reviews.

@0xMaty
Copy link

0xMaty commented May 7, 2023

The solution haven't quite worked for me. It worked after I modified gradle script as follows:

buildFeatures {
    aidl true
    buildConfig true
}

@onuralpszr
Copy link
Contributor Author

onuralpszr commented May 7, 2023

The solution haven't quite worked for me. It worked after I modified gradle script as follows:

buildFeatures {
    aidl true
    buildConfig true
}

For building SDK or using SDK ?

Because for usagei I made a template project for a while ago and I am using standard android project and you can see in this config and patch and you even try yourself it is works. I also made a CI so we can see it does build.

https://github.com/onuralpszr/CvCamera-Mobile/blob/main/patches/cv_build_gradle.patch
https://github.com/onuralpszr/CvCamera-Mobile/tree/main

Do you have a project shows you it is not builds ?

Another side note they gonna remove both them in version 9.0

https://cs.android.com/android-studio/platform/tools/base/+/0bc1c23297760643b03e8cfd8acc52c007a99cd6

@0xMaty
Copy link

0xMaty commented May 7, 2023

When I imported OpenCV and tried to build my project. It failed on missing BuildConfig class. Once I added the buildConfig true - my project compiled and I was able to successfully initialize OpenCV.

@asmorkalov asmorkalov mentioned this pull request May 31, 2023
@nando8082
Copy link

nando8082 commented Oct 29, 2023

When I imported OpeCV in my android studio project (Android Studio Giraffe | 2022.3.1 Patch 2) and I tried to build my project appear the next error
Plugin [id: 'com.android.application', version: '8.1.2', apply: false] was not found in any of the following sources:

thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
AGP 8.0 build.gradle namespace and aidl buildFeature requirement added opencv#23447 

Hello,

Android Gradle Plugin version 8.0 is asking for namespace. This is become mandatory and after I update my AGP to 8.0, I got this error 


```
Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

android {
    namespace 'com.example.namespace'
}

If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.
```

This change fix this future releases. However I am not sure how opencv wants to user namespace I used "org.opencv" if there is a different namespace please let me know so I can changed that too. Also should I add namepsace into "opencv/modules/java/android_sdk/android_gradle_lib/build.gradle" here ?

### Sources

Android developer link: https://developer.android.com/studio/preview/features#namespace-dsl
Issue Tracker Google: https://issuetracker.google.com/issues/191813691?pli=1#comment19

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
@BrokenSoul228
Copy link

Hi, I added module OpenCV to my android project on kotlin, try to invoke VideoCapture, but after build take issue "could not find "libopencv_java490.so"". I have found "libopencv_java4.so" in my project. Can anyone help me with my problem?

thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
AGP 8.0 build.gradle namespace and aidl buildFeature requirement added opencv#23447 

Hello,

Android Gradle Plugin version 8.0 is asking for namespace. This is become mandatory and after I update my AGP to 8.0, I got this error 


```
Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

android {
    namespace 'com.example.namespace'
}

If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.
```

This change fix this future releases. However I am not sure how opencv wants to user namespace I used "org.opencv" if there is a different namespace please let me know so I can changed that too. Also should I add namepsace into "opencv/modules/java/android_sdk/android_gradle_lib/build.gradle" here ?

### Sources

Android developer link: https://developer.android.com/studio/preview/features#namespace-dsl
Issue Tracker Google: https://issuetracker.google.com/issues/191813691?pli=1#comment19

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants